1

I need to simulate UP arrow key, so i used sendinput, i saw on https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx UP arrow key is 0x26, but the problem is that the program simulate the press of "L" and no up arrow key, why? Here is the code:

INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.time = 0;
ip.ki.wVk = 0;
ip.ki.dwExtraInfo = 0;
ip.ki.dwFlags = KEYEVENTF_SCANCODE;
ip.ki.wScan =0x26; //UP ARROW key
SendInput(1, &ip, sizeof(INPUT));
ip.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
secon25
  • 70
  • 9
  • What does `SendInput()` returns? What does [`getLastError()`](https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms679360%28v=vs.85%29.aspx) says? – YSC Mar 27 '17 at 13:25
  • 1
    It is always a mistake to send events one by one. Create an array and inject them in one call to `SendInput`. This is explained in the documentation. I don't believe you have read it carefully enough. – David Heffernan Mar 27 '17 at 14:03
  • And for what it is worth, there's no point grubbing around with scancodes here. Remove `KEYEVENTF_SCANCODE` and use the virtual key code, `VK_UP` in this case. – David Heffernan Mar 27 '17 at 15:30
  • It doens't work – secon25 Mar 27 '17 at 17:54
  • Yes it does. You are doing it wrong. – David Heffernan Mar 27 '17 at 18:45
  • The problem is that i want to simulate press key in a game http://stackoverflow.com/questions/18647053/sendinput-not-equal-to-pressing-key-manually-on-keyboard-in-c and as you said don't work – secon25 Mar 27 '17 at 18:50
  • That's not what is written in the question. I'm just looking at the question that was asked. If you asked the wrong question, that's really your problem. – David Heffernan Mar 27 '17 at 19:45

1 Answers1

1

You are using the virtual key code as the scancode, not the actual scancode.

According to this scancode table the correct value is 0x48.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • ip.ki.wScan =0X48 This simulate 8 – secon25 Mar 27 '17 at 14:14
  • 1
    Scancodes are hardware-dependent. The correct way to get a scancode for a virtual key is to use [`MapVirtualKey()`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646306.aspx)/[`MapVirtualKeyEx()`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646307.aspx) with the `uMapType` parameter set to `MAPVK_VK_TO_VSC` or `MAPVK_VK_TO_VSC_EX`. – Remy Lebeau Mar 27 '17 at 15:57
  • The problem is that i want to simulate press key in a game http://stackoverflow.com/questions/18647053/sendinput-not-equal-to-pressing-key-manually-on-keyboard-in-c so i must use ip.ki.wScant to move – secon25 Mar 27 '17 at 17:56
  • @secon25 Did you follow the links Remy posted in his comment and read about those functions? – Some programmer dude Mar 27 '17 at 18:00