Sendmessage WM_KeyDown and WM_KeyUp not working as expected

J

Jeremy Chapman

I am trying to send a shift and right arrow keypresses to a window in
another application.

If I use Microsoft Spy++ and Press shift + Right Arrow, the events get
captured as follows
WM_KeyDown nVirtKey:VK_SHIFT cRepeat:1 ScanCode:2A fExteded:0
fAltDown:0 fRepeat:0 fUp:0
WM_KeyDown nVirtKey:VK_RIGHT cRepeat:1 ScanCode:4D fExteded:1
fAltDown:0 fRepeat:0 fUp:0
WM_KeyDown nVirtKey:VK_RIGHT cRepeat:1 ScanCode:4D fExteded:1
fAltDown:0 fRepeat:1 fUp:1
WM_KeyDown nVirtKey:VK_SHIFT cRepeat:1 ScanCode:2A fExteded:0
fAltDown:0 fRepeat:1 fUp:1

So I am trying to mimic this using the PostMessage call in user32.dll

[System.Runtime.InteropServices.DllImport(strUSER32DLL,
CharSet=CharSet.Auto, SetLastError=true)]
public static extern int PostMessage(
IntPtr hWnd,
[MarshalAs(UnmanagedType.U4)] int iMsg,
int iWParam,
int iLParam);

My calls are as follows:

PostMessage(hWnd, WM_KEYDOWN, SHIFTKEY, 0);
PostMessage(hWnd, WM_KEYDOWN, RIGHT, 0);
PostMessage(hWnd, WM_KEYUP, RIGHT, 1);
PostMessage(hWnd, WM_KEYUP, SHIFTKEY, 1);

But the window receiving the messages does not behave the same way as it
would if the user typed SHIFT + RIGHT ARROW on the form.
When I capture the messages with SPY++ it shows:

WM_KeyDown nVirtKey:VK_SHIFT cRepeat:0 ScanCode:00 fExteded:0
fAltDown:0 fRepeat:0 fUp:0
WM_KeyDown nVirtKey:VK_RIGHT cRepeat:0 ScanCode:00 fExteded:0
fAltDown:0 fRepeat:0 fUp:0
WM_KeyDown nVirtKey:VK_RIGHT cRepeat:1 ScanCode:00 fExteded:0
fAltDown:0 fRepeat:1 fUp:0
WM_KeyDown nVirtKey:VK_SHIFT cRepeat:1 ScanCode:00 fExteded:0
fAltDown:0 fRepeat:1 fUp:0

My only guess is that the cRepeat, ScanCode, fExteded, fRepeat, fUp
properties of the message are not set correctly, but how do I specify the
values for these?
 
T

TC

Could it be something to do with the focus?

Maybe the form /has/ the focus when you click directly in it, but does
not when you issue the sendmessage calls, & that makes a difference to
the messages?

Just a guess, in the absence of any other replies.

HTH,
TC
 
Top