0

i'm using PostMessage to send windows message(s) to an application. Now, this code works fine when sending any key except ARROW keys (VK_RIGHT or VK_LEFT).

procedure SendKey(key: Variant);
var
  lParam: integer;
  scancode: integer;
begin
  if (VarType(key) = varUString) then
  begin
    scancode := MapVirtualKey(Ord(VarToStr(key)[1]), MAPVK_VK_TO_VSC);
    lParam := scancode shl 16;
    PostMessage(_hWindow, WM_KEYDOWN, scancode, lParam);
    PostMessage(_hWindow, WM_KEYUP, scancode, lParam);
  end else
  begin
    lParam := MapVirtualKey(key, MAPVK_VK_TO_VSC) shl 16;
    PostMessage(_hWindow, WM_KEYDOWN, key, lParam);
    PostMessage(_hWindow, WM_KEYUP, key, lParam);
  end;
end;

I installed a keyboard hook to monitor the WM_KEYDOWN/UP messages for VK_LEFT/RIGHT to see how the lParam looks like, and i encountered some weird values, here's the DebugView output when pressing RIGHT Arrow Key (VK_RIGHT).

[2776] wParam: 39, lParam: 21823489
[2776] wParam: 39, lParam: -1051918335

if i try to send the messages with these values hardcoded, nothing happens too, any idea what's going on ? thanks.

  • 2
    [You can't simulate keyboard input with PostMessage](http://blogs.msdn.com/b/oldnewthing/archive/2005/05/30/423202.aspx) – Remy Lebeau Dec 01 '15 at 19:21
  • Read the documentation for [`WM_KEYDOWN`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646280.aspx) and [`WM_KEYUP`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646281.aspx), they tell you what the `wParam` and `lParam` values represent... – Remy Lebeau Dec 01 '15 at 19:34
  • 1
    `wParam=39` is `VK_RIGHT`. `lParam=21823489` is 0x14D0001, which means a repeat count of 1, a scan code of 0x4D, key is extended, context code is 0, previous state is 0 (up), and transition state is 0. `lParam=-1051918335` is 0xC14D0001, which means a repeat count of 1, a scan code of 0x4D, key is extended, context code is 0, previous state is 1 (down), and transition state is 1. – Remy Lebeau Dec 01 '15 at 19:34
  • You need to use [`SendInput()`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310.aspx) to simulate keyboard input correctly. But you cannot control which application will receive the input, it will go to whichever window has the input focus at the time, just like when the user types keystrokes on the physical keyboard (`SendInput()` inserts into the same input queue that the keyboard driver inserts into). – Remy Lebeau Dec 01 '15 at 19:37
  • @RemyLebeau that's what i'm trying to avoid, i don't want the window to be in the foreground, i want to be able to do my work while i send those keyboard input, but thanks anyways for advising me. –  Dec 02 '15 at 06:57

1 Answers1

1

It depends on how the application handles input. Sometimes the application handles this directly from the message loop rather than the window procedure. Sometimes applications use raw input instead. Presumably your target application is of this nature.

Generally these questions get asked by people trying to fake input to programs that don't want to accept fake input. It's plausible that you won't be able to fake input to your program. Or you might be able to use SendInput. It all depends on the target application

If your target application is prepared to accept automation you should use the accepted method for that, UI Automation.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Can you give me more details about UI Automation ? –  Dec 01 '15 at 19:05
  • It is documented in MSDN – David Heffernan Dec 01 '15 at 19:07
  • Will i be able to send background keystrokes to the application with UI Automation ? –  Dec 01 '15 at 19:08
  • UI Automation is not about keystrokes, it is about enumerating parent/child objects and triggering actions on them. Depending on what the app does when receiving arrow keystrokes, you might be able to just trigger those actions directly instead. – Remy Lebeau Dec 01 '15 at 19:24
  • That is possible if the app is prepared to accept. Usually people ask these questions when trying to fake input to games or online poker. And in that case the target app, aka the victim, might not want to go along with the scam. – David Heffernan Dec 01 '15 at 19:24
  • I just checked the application with ranorex spy utility, and it does not expose objects that could be used for UI Automation. –  Dec 02 '15 at 13:21
  • Perhaps this mysterious application, whatever it is, doesn't want to be automated. – David Heffernan Dec 02 '15 at 13:33
  • "shouldn't be any actual elements since it's all just rendered assets". –  Dec 02 '15 at 13:54