how to properly simulate keyboard events once a hotkey combination is activated - c#

I have a program that registers a certain hotkey combination for activation. For the purpose of this question, let's say this hotkey is Ctrl + Alt + D. Once the combination is pressed, my program will then simulate some other keyboard events to the active window using SendInput, for example, it could simulate Ctrl + A. My issue is that the simulation of Ctrl + A doesn't work properly since (I think) the Ctrl key is still pressed from the hotkey combination. One of the solutions I'm thinking to do is to send Key Up events for every key in my hotkey combination and then simulate Ctrl + A, however I wanted to check if this is the proper way of handling it?

First, understand that questions asking how to send keyboard messages are asked very often. I assume you have not invested much time looking for previous answers. Second, understand that beginners often mistakenly think that sending keyboard messages is the easiest and most effective solution. The truth is that it is usualy not the easiest and is not the most effective and not the most reliable.
If you are going to work with Windows messages like that, then learn to use Spy++. If you do not know what that is, then please invest a minute by familiarizing yourself with the tools available in the VS Tools menu.
A likely easier, more efective and reliable solution usually is to use the WM_GETTEXT and WM_SETTEXT messages. And anticipating future questions, to push a button in another application send a BN_CLICKED notification to the parent of the button. You can use Spy++ to get many more answers to questions of messages.
Using SendInput might be the best solution for you, but there is not enough of your requirements provided to say for sure. In my opinion, SendInput should be the last resort and used only when the other possibilities are not possible. Please spend some time reading some of the numerous articles and previous answers relevant to the question.

Related

How do I reliably invoke clipboard copy/paste actions in other apps without messing with input queue

A while ago I wrote a simple app, which surrounds selected text in any input field in any application with some unicode symbols when user presses some hotkey. Basically, app's logic is as follows:
Register global hotkey.
Hotkey fired, now set clipboard monitor and invoke clipboard copy to see if some text was selected.
If clipboard has changed and now contains text, surround said text with symbols and then invoke clipboard paste, so input field will be updated with modified text.
The problem is, I can't get copy/paste functionality from other apps in a reliable way. What I have tried to date:
If I send WM_COPY/WM_PASTE, it's more often ignored than not, depending on the application.
If I use SendInput, keybd_event or any other keyboard-messing stuff to press/unpress usual clipboard hotkeys, it will often tamper with user-pressed keys: copy/paste uses control or shift, which are also quite popular for all generic hotkeys in all apps, my app included.
If I use Journal Hook to directly inject keyboard messages into system input queue, sometimes it will work fine, and sometimes weird glitches will occur. Also, other applications may be using JournalHook for themselves, and that will mess my app badly. Not to mention that default security policies make journal hook hard to use.
I've been trying to get/set text in the input fields using Windows UI automation instead of clipboard magic, but it rarely works.
So, if you know other ways to make other applications reliably use copy/paste functionality, or can even devise entirely different approach, I'd really appreciate you telling me :)
I think your approach isn't that bad, maybe you can start with WM_DRAWCLIPBOARD so you can monitor the clipboard. A nice sample code can be found here.
Next I'd take a look at SendKeys class - Attention: Flush, Buffer, etc. Use this to send ctrl+c/v instead of a windows message! You should get a notification from your monitor if it worked.
Now I'd use the Cliboard class to maniplulate the Data and paste it back.
Oh, it seems that I could only find more or less reliable way of invoking copy-paste without SendInput or journal hook, but I have to deal with it :-) So here it is, in case someone find it useful:
Remember and reset keyboard modifiers via AttachThreadInput(yourAppThreadId, targetAppThreadId) + SetKeyboardState(keyStateWithoutKeyboardModifiers).
Set Ctrl key modifier via SetKeyboardState(keyStateWithControl) for Ctrl+C/V hotkey.
Then PostMessage to focused control handle with WM_KEYDOWN message for C or V key, whether you want it to copy or paste.
Call Thread.CurrentThread.Join(20) to let other app process messages — and that's the most retarded moment in the whole deal, since I couldn't find the sure way to know when other app will have empty message queue.
Restore remembered keyboard modifiers.
Also, every time you do SetKeyboardState, call SetForegroundWindow(focusedControlHandle) and SetFocus(focusedControlHandle) afterwards.

Cancel Key Stroke after GetAsyncKeyState match?

I'm creating a utility for my mother-in-law in order to remap CTRL-C and CTRL-V commands to one signal button on her keyboard to assist with at home work. On the first press it will commit a COPY command. Next press will be a PASTE command. Since my little application won't be in focus I'll need the use of GetAsyncKeyState. Right now I'm attempting to map this all to the tilde key. How do I kill the tilde key from actually passing to the field selected and overwriting the data or appending itself to the pasted text? Could either use C# in any .NET framework or VB6 for this little applet.
I understand the answer I'm looking for could assist in creating malicious software so if anyone should feel the need to not share or delete this question then by all means do so. I'm looking for an answer to a real issue or situation. I know I could map to a none typing key like Scroll Lock or Caps Lock like some other utilities out there but both of these keys are used at this point in time and not quite an option.
You going to have to do a global keyboard hook (see VBAccelerator for some details on global hooks in VB6), and then not pass on the message once you have received CTRL-V or CTRL-C and then act on that.

Key combinations for a Hot key

We are developing a hot key for one of our application.
A key combination that is
easy to remember
easy to press (especially for people
with small fingers)
certainly not ctrl-alt-del ;)
Which key combination do you suggest for a hot-key?
EDIT: Please suggest any specific character instead of generic ones.
How about Ctrl + ?
The Ctrl key is right next to the ? key. Also the question mark makes sense since none of us has any idea what the hot key is supposed to do.
It should depend on the keyboard layout, qwerty or dvorak and language configuration, of your main user. It also depends on the usage of the application when the user should trigger it. Is s/he using the mouse or the keyboard for instance.
On the topic of remembering a hotkey, using Ctrl+S for a save action or Ctrl+O for open is pretty much standard for GUI applications nowadays, so users would remember those. If the shortcut is used many times or is important in the application the user will remember it, even if it's as silly as pressing Shift 5 times in a row.
If it's not used often, it should be presented to the user in a way that it does not interrupt the work flow, as a shortcut is often displayed in a menu item for instance.
And as a final note, don't make a keyboard shortcut to remove your changes and place it next to your Save shortcut. :-)
What does the hotkey do?
Any combination with Ctrl or Alt will do, excluding the ones used by the windows UI( Ctrl+A for select-all, Ctrl+S for save, etc)
Give us more info.
Scroll Lock - it's not being used for anything else. But seriously, it will only be easy to remember if it has something to do with the application or people use it all the time.
Jay Riggs said:
How about Control + ?
The Ctrl key is right next to the ? key. [...]
That's how it is on a American/British (I don't know how these layouts are called.) keyboard. But on German/French etc keyboards ? is right next to 0. So it's not the best choice when you are planning to publish your application in several countries.
I think it's not that easy to decide, what a good hot key is as long you doesn't give us a some more details.

app in background read keys pressed by user

i want to do a application which works in background and read all keys pressed by user and save it in file.
i start to write it in Win forms and use keys Pressed event but it works when form is focused:/
It is another way to do it? i hear about Windows Service, but i never use it :/
You can do it with help of these:
DirectX look into MDX
KeyLogger Application for C#
Creating a simple keylogger in C#
I don't know your reasons, but I just hope they are "good".
After doing a lot of research looking for a good code to use to achieve this, I've decided to create my own C# Keylogger API. It's very simple and clean:
api.CreateKeyboardHook((character) => { Console.Write(character); });
You just need to pass a callback and the API will return the key pressed by the user, among other things like: the screen the user was in that moment. Obviously, it works in background.
More details here: https://github.com/fabriciorissetto/KeystrokeAPI
I'm assuming you want to intercept all key events to all windows:
I wouldn't recommend C# for this. You'll need to drop to the Win32/64 API, which can be done, but you'd be better off using Visual C++ / something without training wheels.
There are several ways to do this. The easiest, by far, is to register a hook for keyboard events. Implement a basic service and handle the key-press events in your main loop / event handler. You can also just brute-force the keyboard state in a tight polling loop, but this will make the CPU cry.
There's 1,000,001 ways to install your own keyboard driver without admin privileges, but you're going to need to find one yourself, assuming you go down that road.
I seriously doubt that anyone on stackoverflow will help you create a key logger. You're going to need to dive into the bowels of the Win32 API and, likely, figure it out yourself. It isn't difficult if you have a solid C background. Windows "security" is little more than smoke & mirrors.

Is there a way to receive key input to an out-focus form?

I was wondering if it is possible to receive key input to a form that is out of focus. For example, I have my C# form to be top-most of all windows; I want to get all the keys I type on other applications like for example browser to kinda get a history of all the keys I typed while my computer was on.?
Is there a way to do this?
You will need to create a global keyboard hook although be advised that this is not necessarily considered a good practice, and might cause issues with Anti-virus software.
Have a look here to see my answer in this keyboard hooking business here...in the link this OP was trying to find out how to hook the keyboard when running a VMWare session...In my answer I gave a couple of links pointing to the direction of CodeProject's articles on keyboard hooking.
Hope this helps,
Best regards,
Tom.

Categories