WM_KEYDOWN, WM_CHAR and WM_KEYUP

  • Thread starter Thread starter Jeremy Chapman
  • Start date Start date
J

Jeremy Chapman

I am using SendMessage() to send characters to a window. After using
Microsoft Spy++ to look at window messages when I click a key, I noticed
that the enter key sends WM_KeyDown WM_Char and WM_KeyUp but the home key
only sends WM_KeyDown and WM_KeyUp. Is there some rule I can use to tell if
I should send the WM_Char or not?
 
SendKeys class. Check out this managed way of sending keys:
msdn.microsoft.com/library/en-us/cpref/
html/frlrfsystemwindowsformssendkeysclasstopic.asp

It works for send keys to the current window. I've ran Reflector and it
looks like you can possible create a reflections hack to invoke a private
method and use that to send the keys :)
 
Hi Alex,

SendKeys simulates keystrokes typed on the keyboard. It has no target
window. Whatever is the active focused window it gets the keys. It is the
same when you type.
 
nothing to send to a predefined window though....


Stoitcho Goutsev (100) said:
Hi Alex,

SendKeys simulates keystrokes typed on the keyboard. It has no target
window. Whatever is the active focused window it gets the keys. It is the
same when you type.


--
Stoitcho Goutsev (100) [C# MVP]

Alex Korchemniy said:
SendKeys class. Check out this managed way of sending keys:
msdn.microsoft.com/library/en-us/cpref/
html/frlrfsystemwindowsformssendkeysclasstopic.asp

It works for send keys to the current window. I've ran Reflector and it
looks like you can possible create a reflections hack to invoke a private
method and use that to send the keys :)
 
Yes I know that.

But it is possible to use reflection to call the private methods of the
class. Its not too 'safe' (private methods could change) but possible.

If you use Reflector you can see methods like:
- private static bool AddSimpleKey(char character, int repeat, IntPtr hwnd,
int[] haveKeys, bool fStartNewChar, int cGrp)
- private static void AddMsgsForVK(int vk, int repeat, bool altnoctrldown,
IntPtr hwnd)
- private static void Send(string keys, Control control, bool wait)

---
This posting is provided "AS IS" with no warranties, and confers no rights.
Alex Korchemniy

Stoitcho Goutsev (100) said:
Hi Alex,

SendKeys simulates keystrokes typed on the keyboard. It has no target
window. Whatever is the active focused window it gets the keys. It is the
same when you type.


--
Stoitcho Goutsev (100) [C# MVP]

Alex Korchemniy said:
SendKeys class. Check out this managed way of sending keys:
msdn.microsoft.com/library/en-us/cpref/
html/frlrfsystemwindowsformssendkeysclasstopic.asp

It works for send keys to the current window. I've ran Reflector and it
looks like you can possible create a reflections hack to invoke a private
method and use that to send the keys :)
 
Hi Jeremy,
WM_KEYUP and WM_KEYDOWN are generated as a result of pressing and releasing
a key (any key). It provides the vietual key code and the scan code of the
key, which is language regardless of the language, keyboard layout, etc
On the other key some of the keys produce character code (unicode) the same
key may produce different character code depending on the state of some
other keys (SHIFT, CAPS LOCK). Obviously these codes are language dependent.
WM_CHAR messages are post by the TranslateMessage API if called in the
message loop. Look at the docs for TranslateMessage function.
TranslateMessage produces WM_CHAR only for keys that the keyboard driver
maps to character codes. Which those keys are is culture dependent.

Message translation may result on WM_DEADCHAR to be posted as well. Consult
the docs for more info.

At the end I'd like to worn you that sending those messages is not like
simulating typing. WM_KEYUP and WM_KEYDOWN are notifications for key state
changes, which means that sending messages doesn't change the state of the
keyboard. Some programs uses API like GetKeyState and GetAsyncKeyState. Such
a programs won't work correctly.

The rigth way to do that is to use SendKeys class, but unfortunately with
this class you cannot provide explicitly the target window. Other possible
solution is to use PInvoke and set the keys state via AttachThreadInput and
SetKeyboardState.

Frankly, I haven't tried to attach a thread and set the key states and the
docs doesn't say anything about it but I think it should work.
 
Hi Alex,

Private means that the method is not supposed to be called even from the
derived classes. Those methods are not documented and are subject to change.
Such methods dont't have to be called.

Looking on the code of these private methods I can see that they use the
idea, that I threw in my previous post, to use SetKeyboardState. So it is
possible to be implemented even without using such a hack of calling private
methods. Anyways usign PInvoke has the drawback that the caller must have
enough permissions. The reflection has the same problem though.


--
Stoitcho Goutsev (100) [C# MVP]
Alex Korchemniy said:
Yes I know that.

But it is possible to use reflection to call the private methods of the
class. Its not too 'safe' (private methods could change) but possible.

If you use Reflector you can see methods like:
- private static bool AddSimpleKey(char character, int repeat, IntPtr
hwnd,
int[] haveKeys, bool fStartNewChar, int cGrp)
- private static void AddMsgsForVK(int vk, int repeat, bool altnoctrldown,
IntPtr hwnd)
- private static void Send(string keys, Control control, bool wait)

---
This posting is provided "AS IS" with no warranties, and confers no
rights.
Alex Korchemniy

Stoitcho Goutsev (100) said:
Hi Alex,

SendKeys simulates keystrokes typed on the keyboard. It has no target
window. Whatever is the active focused window it gets the keys. It is the
same when you type.


--
Stoitcho Goutsev (100) [C# MVP]

Alex Korchemniy said:
SendKeys class. Check out this managed way of sending keys:
msdn.microsoft.com/library/en-us/cpref/
html/frlrfsystemwindowsformssendkeysclasstopic.asp

It works for send keys to the current window. I've ran Reflector and
it
looks like you can possible create a reflections hack to invoke a
private
method and use that to send the keys :)

---
This posting is provided "AS IS" with no warranties, and confers no
rights.
Alex Korchemniy

:

I am using SendMessage() to send characters to a window. After using
Microsoft Spy++ to look at window messages when I click a key, I
noticed
that the enter key sends WM_KeyDown WM_Char and WM_KeyUp but the home
key
only sends WM_KeyDown and WM_KeyUp. Is there some rule I can use to
tell
if
I should send the WM_Char or not?
 
Back
Top