Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send Ctrl/Shift/Alt + Key combinations to an application window? (via SendMessage)

Tags:

c#

sendmessage

I can successfully send any single key message to an application, but I don't know how to send combinations of keys (like Ctrl+F12, Shift+F1, Ctrl+R, etc..)

Tried doing it this way:

SendMessage(handle, WM_KEYDOWN, Keys.Control, 0);
SendMessage(handle, WM_KEYDOWN, Keys.F12, 0);
SendMessage(handle, WM_KEYUP, Keys.F12, 0);
SendMessage(handle, WM_KEYUP, Keys.Control, 0);

but this does not seems to work (application acts like only F12 is pressed, not Ctrl+F12).

Any ideas how to make this work?

like image 464
user1792042 Avatar asked Nov 02 '12 17:11

user1792042


3 Answers

You would probably find that using SendInput (documentation here) works a lot better. You will need to P/Invoke it from C#, example here. You can provide arrays of data with keys down and up and properly set the other message parameters, for example whether left or right Ctrl/Shift/Alt were pressed.

You can also use the SendKeys class (documentation here). This allows you to specify keys by name, e.g., {^F12} for Ctrl+F12.

Edit: The OP is now saying he needs to send input to minimized applications without activating them. This is not possible to do reliably in any way, including even with specialized hardware. I've worked in automation. It just isn't possible. The OP needs to use FindWindow/SetForegroundWindow to toggle the target app on, and then he can toggle back to his application.

like image 173
Ed Bayiates Avatar answered Nov 05 '22 04:11

Ed Bayiates


The OP is now saying he needs to send input to minimized applications without activating them. This is not possible to do reliably in any way, including even with specialized hardware. I've worked in automation. It just isn't possible.

Sending input to minimized applications is, in fact, possible, not sure about mouse input, but keyboard input works just fine. Taking idea in a previous answer here's what I was able to accomplish (code is in Delphi, but it's pretty simple, so you can translate it to your required language):

procedure SendKeys(const Win : HWND; const Key,sKey: Cardinal);
var
  thrID : Cardinal;
  KB : TKeyBoardState;
begin
  if sKey <> 0 then
  begin
    thrID := GetWindowThreadProcessId(win,nil);
    GetKeyboardState(KB);
    AttachThreadInput(GetCurrentThreadID, thrID, True);
    KB[sKey] := KB[sKey] or $80;
    SetKeyboardState(KB);
  end;
  SendMessage(Win,WM_KEYDOWN,Key,0);
  SendMessage(Win,WM_KEYUP,Key,0);
  if sKey <> 0 then
  begin
    KB[sKey] := 0;
    SetKeyBoardState(KB);
    AttachThreadInput(GetCurrentThreadId, thrID, False);
  end;
end;

[Win] must be the control to receive the input, not its parent form etc. [Key] is a key to be pressed; [sKey] is an alternative key to be pressed while pressing [Key] such as CTRL/SHIFT (ALT is transferred through message itself, see MSDN WM_KEYDOWN reference for details).

Sending a sole keystroke is fairly simple, you just do the sendmessage and it's done, but if you need to something like CTRL+SPACE here's where it gets complicated. Each thread has its own KeyboardState, altering KeyboardState in your own app will not affect another unless you join their thread inputs by AttachThreadInput function. When application processes WM_KEYDOWN message it also tests current shift states (CTRL/SHIFT) by calling GetKeyboardState function (ALT key can be sent through the additional parameter of WM_KEYDOWN message) and that's when attached thread input comes into play.

like image 4
Rusty Avatar answered Nov 05 '22 03:11

Rusty


I already tried the method with GetKeyboardState and SetKeyboardState (preceded by attaching the window thread and ended with detaching from window thread). I doesn't work for combinations of keys like Ctrl+Something or combinations using Alt or Shift also. Control, Alt and Shift keys are not seen as pressed. It seems like the maximum you can get when the window is minimized is pressing individual keys using PostMessage with WM_KEYDOWN messages. Another thing I noticed is that if you Post WM_KEYDOWN and WM_KEYUP (for the same key) the key will be pressed twice. So only use WM_KEYDOWN one time. This is not an 100% accurate method but when window is minimized there are some limitations.

The same situation happens when screen is locked.

like image 3
daniel Avatar answered Nov 05 '22 04:11

daniel