1

I'm very new to C++ and can't find how to simulate a keypress. I want to make it like this:

if(GetAsyncKeyState(0x52))
    {
        //(Make program simulate the press of number "3" key (0x33) 
    }

When I click the "R" key on my keyboard i want the program to simlate a keypress number 3 (0x33)

Gurk
  • 27
  • 4
  • Simulate in what way? Be much more clear about what you want to happen, when, and why, and what went wrong when you tried it. – Lightness Races in Orbit Jan 12 '20 at 20:07
  • I want the program to press the "3" key for me. As I said in the description i cant find a way to make it happend so nothing went wrong – Gurk Jan 12 '20 at 20:11
  • Okay, that didn't really clarify anything. You haven't explained the effect you want, or what you intend for "press" to mean in this context, or "when" you want the program to do this and why. I'm afraid, unless you can explain your requirements in detail, we cannot help you. Also you are expected to research/try something first. – Lightness Races in Orbit Jan 12 '20 at 20:12
  • I edited the post idk how to explain it better. – Gurk Jan 12 '20 at 20:16
  • You just repeated the same couple of words. Explain what you're trying to accomplish. What is your goal? What do you want to see on screen? What will the simulated keypress do? Give _details_!! If you don't know what your program is supposed to do, then how can we know? – Lightness Races in Orbit Jan 12 '20 at 20:20
  • Alright Im gonna be as specific as possible... If Im playing a game that requires me to press the "3" for something to happend inside the game, I want to be able to instead of pressing down the button "3" on my keyboard I instead want to press "R". – Gurk Jan 12 '20 at 20:26
  • Better! Now we are homing in on some understanding, and consequently a solution. So, simulating a keypress seems like the wrong thing to do. If you call function `doThing()` when the user presses `"R"`, you can also call `doThing()` at some other time, so why not do that? And keep keypresses out of it? Because you don't _really_ want to simulate a keypress... you want to trigger `doThing()`. That's the actual thing that you wish to accomplish. That's your goal. – Lightness Races in Orbit Jan 12 '20 at 20:32
  • Don't worry about simulating input just yet. You are already failing to establish the condition under which you want this to happen. See the documentation for [GetAsyncKeyState](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getasynckeystate). – IInspectable Jan 13 '20 at 07:09

1 Answers1

0

You can use SendInput() to generate key stroke events, as if typed on the keyboard. You can find more practical infos in this SO question.

You may also try SetKeyboardState() but this only affects the current thread. You can find more in this SO question and its answers.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • I saw that post about SendInput but still didnt understand how to use it :I – Gurk Jan 12 '20 at 20:21
  • @Gurk what's the problem exactly ? It creates an empty event, then generates a press key event, and then a release key event. Exactly as if you'd press on the key on your keyboard. Try with `vkCode = 0x33;` – Christophe Jan 12 '20 at 21:39
  • Except, it does it [wrong](https://stackoverflow.com/q/46744894/1889329). – IInspectable Jan 13 '20 at 06:58
  • @IInspectable and setkeyboardstate in that case? – Christophe Jan 13 '20 at 07:21
  • `SetKeyboardState` modifies the synchronous, per-thread keyboard input-state table. Unless you are running external code on that thread, you are just prank-calling yourself. You could accomplish the latter a lot easier, without even bothering the system, simply by using a variable, like `bool pretendMagicKeyIsPressed{};`. – IInspectable Jan 13 '20 at 07:52
  • @IInspectable Yes. But isn’t this what OP wants ? I understood it was for the own process (e.g. to to some testing of UI), not for another one. Then no wonder that SendInput doesn’t work without the appropriate privileges! – Christophe Jan 13 '20 at 08:42
  • `SendInput` doesn't require any particular privileges. It simply injects input events into the raw input queue. It doesn't allow you to specify a recipient. For that you'd commonly have to call `SetForegroundWindow`. Foreground activation permission is highly restricted. I don't know, what the OP is really trying to accomplish. My previous comment hinted, that [this answer](https://stackoverflow.com/a/22419083/1889329) doesn't call `SendInput` [the way it's meant to be called](https://stackoverflow.com/q/46744894/1889329). – IInspectable Jan 13 '20 at 09:34
  • @IInspectable just reading the reference: “This application is subject to UIPI. Applications are permitted ... inject ... into applications that are at an equal or lesser integrity level”. Sounds like privileges to me. – Christophe Jan 13 '20 at 12:05
  • That doesn't mean, that you need any particular permissions to call the API. The payload may not make it to your intended target, if that would imply crossing integrity levels upstream. Contrast that with `SetForegroundWindow`, which can (and usually will) fail, even if you aren't crossing any integrity or privilege boundary. – IInspectable Jan 13 '20 at 12:33