I'm trying to send Key combinations like ⇧Shift+F8, etc to a specific window. I am able to send F8 by itself by posing a WM_KEYDOWN
, then a WM_KEYUP
and specifing (int)System.Windows.Forms.Keys.F8
as the wParam
, but can't figure out how to do it with the shift key. I've tried ORing it with System.Windows.Forms.Keys.SHIFT
as well as System.Windows.Forms.Keys.SHIFTK开发者_C百科EY
but that doesn't seem to work. Is there some other way?
Note that I can't use SendInput
or possibly because they don't take window handles, and my window may not be in the foreground. Any suggestions appreciated...
You must use keybd_event or SendInput
to send modifier keys to another application because of the way windows handles modifier keys (fun, yes?). I think both may require the other application to have focus to work correctly.
Just sending a series of keydowns / keyups as if you were pushing and holding the modifier keys doesn't (or, at least, didn't last I tried it) work since most applications poll for those keys separately instead of listening for messages about them. I suppose if you control the source for both applications this may work.
Send individual WM_KEYDOWN
messages, one for each key pressed.
WM_KEYDOWN -> Shift WM_KEYDOWN -> F8 WM_KEYUP -> F8 WM_KEYUP -> Shift
Here is a message capture from Spy++ of the message sequence when pressing ⇧Shift+F8 in notepad.
WM_KEYDOWN nVirtKey:VK_SHIFT cRepeat:1 ScanCode:36 fExtended:0 fAltDown:0 fRepeat:0 fUp:0 WM_KEYDOWN nVirtKey:VK_F8 cRepeat:1 ScanCode:42 fExtended:0 fAltDown:0 fRepeat:0 fUp:0 WM_KEYUP nVirtKey:VK_F8 cRepeat:1 ScanCode:42 fExtended:0 fAltDown:0 fRepeat:1 fUp:1 WM_KEYUP nVirtKey:VK_SHIFT cRepeat:1 ScanCode:36 fExtended:0 fAltDown:0 fRepeat:1 fUp:1
精彩评论