Jeremy
Jeremy

Reputation: 46440

sendmessage with shift, control, alt key combinations

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.SHIFTKEY 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...

Upvotes: 1

Views: 7939

Answers (3)

IInspectable
IInspectable

Reputation: 51511

You can't simulate keyboard input with PostMessage. The only supported way to inject input is via SendInput(). The system does not offer a way to inject input for a specific thread or window.

Since there is no way to generate input given the additional requirements, faking input is a dead-end. That leaves you with two options:

  1. Communicate through a dedicated automation interface if the application has one (MS Office applications, for example, provide an automation interface).
  2. Use UI Automation as a system-supplied virtualization of the UI with the ability to automate it.

And those are the only supported ways for automating a UI. Everything else is just going to be a hack.

Upvotes: 0

LOST
LOST

Reputation: 3284

As suggested in the comment by @jnm2, injected SetKeyboardState worked for me for an inactive window.

Upvotes: 0

Donnie
Donnie

Reputation: 46943

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.

Upvotes: 1

Related Questions