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?

RE: WM_KEYDOWN, WM_CHAR and WM_KEYUP by AlexKorchemniy

AlexKorchemniy
Mon Oct 18 16:37:02 CDT 2004

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

"Jeremy Chapman" wrote:

> 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?
>
>
>

Re: WM_KEYDOWN, WM_CHAR and WM_KEYUP by Stoitcho

Stoitcho
Mon Oct 18 17:25:05 CDT 2004

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" <AlexKorchemniy@discussions.microsoft.com> wrote in
message news:DF0FCA55-8038-49C2-92FD-CDB114EBDDCD@microsoft.com...
> 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
>
> "Jeremy Chapman" wrote:
>
>> 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?
>>
>>
>>



Re: WM_KEYDOWN, WM_CHAR and WM_KEYUP by Jeremy

Jeremy
Mon Oct 18 17:34:20 CDT 2004

nothing to send to a predefined window though....


"Stoitcho Goutsev (100) [C# MVP]" <100@100.com> wrote in message
news:eOtESFWtEHA.2536@TK2MSFTNGP11.phx.gbl...
> 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" <AlexKorchemniy@discussions.microsoft.com> wrote in
> message news:DF0FCA55-8038-49C2-92FD-CDB114EBDDCD@microsoft.com...
> > 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
> >
> > "Jeremy Chapman" wrote:
> >
> >> 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?
> >>
> >>
> >>
>
>



Re: WM_KEYDOWN, WM_CHAR and WM_KEYUP by AlexKorchemniy

AlexKorchemniy
Mon Oct 18 17:41:01 CDT 2004

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) [C# MVP]" wrote:

> 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" <AlexKorchemniy@discussions.microsoft.com> wrote in
> message news:DF0FCA55-8038-49C2-92FD-CDB114EBDDCD@microsoft.com...
> > 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
> >
> > "Jeremy Chapman" wrote:
> >
> >> 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?
> >>
> >>
> >>
>
>
>

Re: WM_KEYDOWN, WM_CHAR and WM_KEYUP by Stoitcho

Stoitcho
Mon Oct 18 18:33:48 CDT 2004

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.


--
HTH

Stoitcho Goutsev (100) [C# MVP]

"Jeremy Chapman" <NoSpam@Please.com> wrote in message
news:uDQcfLVtEHA.2596@TK2MSFTNGP15.phx.gbl...
>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?
>
>



Re: WM_KEYDOWN, WM_CHAR and WM_KEYUP by Stoitcho

Stoitcho
Mon Oct 18 19:22:12 CDT 2004

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" <AlexKorchemniy@discussions.microsoft.com> wrote in
message news:1F4FB85B-81A0-48FC-9317-1F9485F02EE8@microsoft.com...
> 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) [C# MVP]" wrote:
>
>> 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" <AlexKorchemniy@discussions.microsoft.com> wrote in
>> message news:DF0FCA55-8038-49C2-92FD-CDB114EBDDCD@microsoft.com...
>> > 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
>> >
>> > "Jeremy Chapman" wrote:
>> >
>> >> 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?
>> >>
>> >>
>> >>
>>
>>
>>