41

I was wondering how can I simulate a key depression in C++. Such as having code that when I run the program it presses the letter "W" key. I don't want to be displaying it in a console window I just want it to display the "W" key every time I click on a text field. Thanks!

Note: I am not trying to make a spammer.

Joe
  • 41,484
  • 20
  • 104
  • 125
llk
  • 2,501
  • 7
  • 36
  • 43
  • 13
    Wait, you want to make the program physically press the key down? – Dair Apr 09 '11 at 20:58
  • Which platform, please? (For instance, Win32) – Jim Blackler Apr 09 '11 at 20:59
  • Windows32 and if it has to be between 7 and XP, I'd prefer 7. – llk Apr 09 '11 at 21:02
  • Depends on the context, a quick search on google will reveal everything you need to simulate a keypress, however if you are programming something yourself, then there's no need, just append the textfield with the letter. – ultifinitus Apr 09 '11 at 21:03
  • 1
    No, I want it to be as if a person pressed the key. Basically like an autotyper. – llk Apr 09 '11 at 21:03
  • I tried Google, I will try more but I am getting useless code snippets which don't work. – llk Apr 09 '11 at 21:04

3 Answers3

36

It looks like you want to use either SendInput() or keybd_event() (which is an older way of doing the same thing).

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 3
    `keybd_event` is deprecated, `SendInput` is *the* way. – Cat Plus Plus Apr 09 '11 at 21:07
  • That's true. I'll keep the link in there for historical interest. – Greg Hewgill Apr 09 '11 at 21:08
  • Thanks, I chose to use the keybd_event() option. I appreciated the quick response! – llk Apr 09 '11 at 21:10
  • 7
    @Shadowalker: Why, after the discussion that just took place about `keybd_event()` being deprecated (meaning "should not be used anymore) and replaced by `SendInput`, did you choose to use it anyway? – Cody Gray - on strike Apr 10 '11 at 07:17
  • 2
    @CodyGray `keybd_event()` is often considered easier to use then `SendInput()`. It should never be used in professional quality code, but I can understand why beginners would still use it in hobby projects. – Mast Jan 24 '15 at 11:45
  • Hi, I know I sort of late, but do you by any chance have an example of using SendInput to send "W" key press, thank you. – a_girl Sep 13 '20 at 23:18
7

First - find this answer on how to use sendinput function in C++.

Look at the code section:

// ...
    INPUT ip;
// ...
    // Set up a generic keyboard event.
    ip.type = INPUT_KEYBOARD;
    ip.ki.wScan = 0; // hardware scan code for key
    ip.ki.time = 0;
    ip.ki.dwExtraInfo = 0;

    // Press the "A" key
    ip.ki.wVk = 0x41; // virtual-key code for the "a" key
    ip.ki.dwFlags = 0; // 0 for key press
    SendInput(1, &ip, sizeof(INPUT));
// ...

I didn't understand where the magic number 0x41 came from.

Go to SendInput documentation page. Still don't understand where's the 0x41.

Go to INPUT documentation and from there to KEYBDINPUT documentation. Still no magic 0x41.

Finally go to Virtual-Key Codes page and understand that Microsoft has given the names for Ctrl (VK_CONTROL), Alt (VK_MENU), F1-F24 (VK_F1 - VK_F24, where are 13-24 is a mystery), but forgot to name characters. Actual characters have codes (0x41-0x5A), but don't have names like VK_A - VK_Z I was looking for in winuser.h header.

Dharman
  • 30,962
  • 25
  • 85
  • 135
jtheterrible
  • 171
  • 1
  • 4
0

My two cents in this. Character "A" in ASCII is 65 in decimal. But in HEX is 41. Hence, 0x41

Look here for the ASCII table http://www.asciitable.com/

Maybe you would be interested in the SCAN codes as well, not just in ASCII. SCAN codes can even tell (and simulate) when the key is downpressed, but also when that key is actually released. SCAN codes are usually followed as well by the ASCII codes in the "input buffer". Here, about the "input buffer" (which is actually a FIFO list in RAM): How to clear input buffer in C?

Here is a small example: http://csourcecodes.blogspot.com/2014/08/c-program-to-display-scan-and-ascii.html

If you're using Windows - possibly your best approach would be the "SendInput" mentioned earlier. Here is a small doc about that: https://batchloaf.wordpress.com/2012/04/17/simulating-a-keystroke-in-win32-c-or-c-using-sendinput/

Hope it helps you - at least a bit!... :)

success!...

  • 2
    This seems to be a comment to [this](https://stackoverflow.com/questions/5607849/how-to-simulate-a-key-press-in-c/63876522#63876522) answer – Michael Kotzjan Jun 18 '21 at 10:10