views:

76

answers:

7

I have a string of values, and I want to simulate key press events in a window for each character.

I plan on sending WM_KEYDOWN, WM_CHAR, and WM_KEYUP events to the window (as that is what seems to happen whan a key is manually pressed).

Those messages require an int be sent in the wParam based on a table of virtual key codes. I can loop through the string and get each character, but how do I take that character and convert it to a value that corresponds to the virtual key code? Convert.ToInt32() does not work.

A: 

It looks like it takes the ASCII character and turns it into hex. For example, 'A' in hex is 41. According to your chart, A is 0x41, which is right (the 0x detonates hex).

DMan
Kaboom! Love it.
Hans Passant
*'the 0x detonates hex'* Ah, the perils of low level code. This zen tidbit does explain a lot.
Andreas Magnusson
A: 

Generally speaking, instead of sending WM_KEYDOWN, WM_CHAR and WM_KEYUP messages directly, you should use SendInput (preferred) or possibly keybd_event (deprecated).

Jerry Coffin
Can't use SendInput or keybd_event, as it doesn't appear I can specify a window handle. I need to be able to send the message even if the window doesn't have focus
Jeremy
A: 

You can use Input Simulator library which provides a high level api for simulating key presses and handles all the low level stuff.

Giorgi
I don't beleive you can specify a window handle with Input Similator. I want to send messages even if the window doesn't have focus.
Jeremy
+2  A: 

Sending WM_KEYDOWN/UP is troublesome. The application itself already translates the WM_KEYDOWN message into WM_CHAR, using the state of the modifier keys (Shift, Alt, Ctrl) and the keyboard layout. Neither of which you can control, you'll get the wrong character, randomly.

Just send WM_CHAR messages, set the wparam to the character code. No need to worry about lparam, few apps ever use it.

Hans Passant
It does seem to work, at least with this application. Thanks.
Jeremy
True, this answer is probably the best way to go, unless you are trying to __do something else__ with each keydown/keyup, there is no need to send the `WM_KEYDOWN` or `WM_KEYUP` messages -- nothing magical happens.
bobobobo
A: 

System.Windows.Forms.SendKeys is a WinForms class with static methods for simulating keyboard input on the active window.

The catch is that .NET has no way of focusing windows in another application (the SendKeys docs talk about how to get around that).

R. Bemrose
A: 

The general solution is to use the SendMessage WinAPI function. This link describes SendMessage's signature and provides a sample import.

Oh and, to map VK codes you should use MapVirtualKey - its best to assume the mapping is arbitrary and not logical.

bobobobo
A: 

VkKeyScanEx anyone? According to MSDN it:

"Translates a character to the corresponding virtual-key code and shift state."

(You could possibly also use VkKeyScan but beware that it has been superseded by VkKeyScanEx.)

Andreas Magnusson