I'm making a program that will take keystrokes entered and change the output to form a message of my choosing. I'm thinking of using GetAsyncKeystate() to see if a key is down, but I'm not sure how to change the value of the key pressed.
Your best bet is a low-level keyboard hook. You don't get a ton of context, but you do get the raw keystrokes. If you need context, then you're probably looking at a text service via TSF, but that tends to get complex quickly.
Related
Is there a way of getting a list of all currently pressed keys in a Lanterna Terminal/Screen?
i.e. not getting the last pressed key, neither waiting for a key to be pressed. I'd like to get a list of all keys that are currently being held down.
I realize this could be a limitation with lanterna with terminals. If so, is there an alternative for implementing a text UI that support polling pressed keys?
I don't think the software interface to keyboards typically exposes a way to ask this question. As far as I know, the best approach is to build it yourself: watch for key-down and key-up events, and maintain a set of the keys which have gone down without a later up event.
I am trying to make a text box in allegro and need a way of getting the ascii keycodes from the key presses. The ev.type == ALLEGRO_EVENT_KEY_DOWN does not always work. I have tried getting the event to work faster but it is still slow.
If there is a way I could make this into a function that could give the Ascii char of what ever key is pressed, it would be great. (I have been looking but I cant find something easy and fast for the source code that I am using)
Perhaps you are looking for an ALLEGRO_EVENT_KEY_CHAR event type. These events are generated every time a character is typed on the keyboard, or auto-repeated because the key was held down long enough. In other words, while ALLEGRO_EVENT_KEY_UP/DOWN events correspond to the keyboard state, ALLEGRO_EVENT_KEY_CHAR events correspond to the character input buffer state.
I am writing a win32 application that needs to take hotkeys while not on focus(it runs in the background without drawing a window). I use RegisterHotKey to assing a few keys but that blocks the for every other process. For example I assign the 'c' key and when I press it in notepad nothing happens.
RegisterHotKey() registers global hotkeys. Hotkeys are processed before regular keyboard input processing, meaning that if you register a hotkey successfully, pressing that key will result in you getting your hotkey message rather than the app with focus getting the normal WM_KEYDOWN/WM_CHAR messages. You have effectively blocked other apps from seeing that key press.
This is by design.
Obviously the solution to avoid clashes like you describe is to not register a hotkey that other applications may use. If you register C without any qualifiers as a hotkey, then no other program will see the C key being pressed. Instead you should use qualifiers like Ctrl/Shift/Alt to prevent your hotkey from interfering with the normal use of the keyboard.
There is no way to register a hotkey that's global unless some other program is active. If you want to achieve the situation where, say, your hotkey works while the desktop is active but nothing else is, you could use a message hook to inject code into the desktop's process (via SetWindowsHookEx()) and intercept key presses that way. But you can't do it with RegisterHotKey().
I just tried UnregisterHotKey(), simulated the key with keybd_event(), then RegisterHotKey() again. I don't recommend it as a production code, it's probably better to use hooks for that, but as a quick hack I just wanted to say that it works.
GetAsyncKeyState()
can be used to determine if certain keys are pressed, even when the program is running in the background.
Or to clarify this question, how can I make Windows think I hit a key, when I really didn't? I know I could possibly use SendMessage and specify the input there, but then wouldn't only my application receive it? I'd like control to the extent of all applications receiving the "fake" input. Any advice?
What you describe, faking input, is implemented by the SendInput function. Input goes to the thread which has input focus.
You can SendMessage to whatever window you want, even on other processes. You can even use HWND_BROADCAST to send it to every to-level window on the system. But is that what you really want? If you're only interested in a specific program, you can get its window's handle using FindWindow, and then send the message only to that window.
Note that if all you want to do is a simple keystrokes injection into another process, then SendInput is indeed the way to go. If you'd like to send some global keyboard shortcut it doesn't matter who has the focus. If you'd like to send the same input to more than one window using SendInput, you'll have to loop over the list of windows, and for each window first set the focus and then send the input.
The down arrow key of my laptop is very loose and it does not seems to last very long.
Is it possible to write any programm(in any language but especially C++) hat simulates the down arrow key.say I made a programm such that when I press A,B,C on the key board it simulates down arrow key.
If not then,
Is there any software available to do this?
Use the On-screen keyboard
If you want to simulate input, use the SendInput API. This injects input at a fairly low level, windows automatically routes it to the appropriate thread based on who has focus. Call it twice, once to send the key down, and again to send the key up.
Perhaps the easiest thing to do is to write a simple app that calls RegisterHotkey for some combination like ctrl-alt-Z, and then calls SendInput for a keypress then keyrelease of the down arrow key.
You might need to wait a short time after receiving WM_HOTKEY to give you time to release the set of hotkeys so that the down arrow gets processed alone without those modifiers from your hotkey interfering with it. (...otherwise the focused app might think you typed in shift+alt+downarrow instead of plain downarrow!)
if you're using linux, xmodmap: http://www.xfree86.org/4.2.0/xmodmap.1.html
I think that you are actually looking for Sharpkeys www.randyrants.com/sharpkeys/
This works with the windows registry and can be used to change mappings of keys.
You can easily write a program that sends WM_KEYDOWN and WM_KEYUP messages to the window which has the current focus. Once you have this program, bind it to a function key in the properties for the .exe file.