Simulate press key

Hi again guys, I’m trying to press keys on my keyboard via blueprint but I can’t find a way
to properly do it(I have also tried the widget interaction component with SendKey but it is not
what I need and does not work as I need)…

What I would need is the c++ function exposed in blueprint, but I don’t know how to code it,
a little c++ node called SENDINPUT or PRESSINPUT with the possibility to choose the key…

Can you enlight me?
(if you need money let me know the price)…

1 Like

Could you give a little more detail? Specifically, what you’re trying to do?

For instance, you say “simulate pressing keys on the keyboard.” If you mean doing so fairly literally, so that the whole computer sees the keypress, it’s easy enough to make a Blueprint node to call the general Windows SendKey function, but that’d be specific to Windows. (And given your app would presumably be in the foreground, it would still generally go to your app.)

But that function is finicky at the best of times, and doesn’t work with all methods of getting keyboard input on Windows.

If you’re trying to generate key presses as input inside your Unreal project specifically across all the platforms (including macOS, iOS, Android, etc.)—such as trying to do “press and hold W to move the character forward”—that approach obviously wouldn’t be auitable.

If you’re trying to generate key events just to interact with your widgets specifically, that’s yet another scenario.

Having a little more detail on what you’re trying to actually do will help folks know what it is you’re looking for (and if maybe there’s a better way to achieve it already in the engine).

1 Like

Yes man,this is what I need, exactly as the whole computer sees the keypress, but I do not know how to do this Blueprint node with the SendKey function (I don`t need to create inputs or move stuff inside Unreal, but I need to call a ‘‘general’’ keypress from Unreal)…

…when I move my mouse on the screen I get the pixel color where the mouse is, then based on that color I need to send some input and this has to be automated, inputs can come only from my keyboard…now when I do this manually it works because I press the key, but it has to be automated, I pass my color variable and the program press that key…this is needed to interact with the WEBBROWSER because as far as I know it is not compatible with the Widget Interaction component…(SendKeyChar or PressKey does not work with WEBBROWSER)

Is this specific to a particular operating system? I.e., just Windows? Or does it need to work on Windows, macOS, Linux, Android, etc.? Because every one of those systems will have its own way to generate a key press… and it’s worth noting, some have more than one way, not all of which will work with all programs.

(I’m not even sure you can generate a keypress system-wide on iOS without some really hacky solutions.)

Even just on Windows, using just “SendKey” on Windows is not enough for everything to see it; some read the keyboard in other ways. It’s the whole reason the venerable “AutoHotKey” hotkey scripting program has like five different ways to generate keypresses (SendKey, SendKey_Raw, PostKey, etc.).

If you just want specifically the SendInput Win32 API call on Windows, then yeah, it’s very easy to make a library that adds a node for that.

I was battling all yesterday trying to solve this. Still have no luck.

I’ve tried Rama’s Simulate Key Press but yeah does not simulate the actual keypress globally.

I also tried the Press Key (Widget Interaction Component) as it says "Press a key as if it had come from the keyboard. But no luck still.

I built a Macro in AutoHotkey that I am trying to fire from Unreal. All I want is Unreal to trigger this “L” hotkey from the keyboard :sweat_smile:

@Packetdancer could you please elaborate on this SendInput idea? Would love to try it.

I wrote up a very quick little Blueprint library that adds a “Generate Key Press Event”:

It’s a single header file to include in your project, and – as noted – will only work on Windows, though still exports the node on other platforms; it just does nothing and returns ‘false’ in all cases. It is not fancy (or, for that matter, super highly tested). You can grab it here; hopefully that helps.

2 Likes

Thanks for putting in the time to share this. I managed to set it all up and give it a whirl but sadly it still didn’t work. The print string went through, but my macro in my AutoHotKey file didn’t fire. After testing it I pressed L on my keyboard to make sure it was listening, and that worked fine.

I’m running Windows 10 and UE 4.27 - the versions shouldn’t matter yeah?

Got me really stumped. For something that ought to be so simple, it’s driving me insane lol

FWIW, it’ll always pass through the execution pin; you can check the boolean output to see whether or not the SendInput succeeded; true means that the number of keypresses that SendInput said it did is equal to the number of keypresses you gave it as input.

Beyond that…?

Like I said, not everything on Windows will read things via the same method; if you look at the AHK forums, you’ll see lots of threads about “Does anyone know what method uses to detect keypresses?!” because they’re pulling their hair out about which variant of things – posting events to an app’s window handler, injecting input events into the keyboard buffer, etc. – will work for a given program.

(And that’s without getting into anything using DirectInput to watch for scan codes…)

Unfortunately, I don’t honestly know offhand what method AHK uses to detect keypresses (and in fact, I think it uses about three different methods, depending on your configuration); I tried this out with a simple little program I wrote, and it did detect the keypress. That said, you might try using #InstallKeybdHook in your AHK script?

…y’know, I may have figured out why it worked for me and not you.

It would kinda help if I uploaded the correct version of the file to the gist. (It’s now been updated.)

(I went and wrote a super simple AHK script and it did indeed trigger… and I went to check if I’d somehow introduced a typo when copying it into the gist, and… yeah.)

hi again, I will give it a try just now and report, yes I need only for windows and not other operating systems, thank you man!!!

Legend, I will give it a go now too! I was getting real deep into SC codes with AutoKey then haha. Fingers crossed!

Can confirm that it works now! Thanks a ton @Packetdancer. This is great, now I can have UE trigger things in other software with hotkeys. So cool. Hope this helps you too @syq and anyone else out there looking for this solution!

1 Like

Glad it worked!

FWIW, some things will ignore keycodes that aren’t down for longer than a certain duration, or want like 0.25 seconds between a modifier key (Control) and a normal key (Q, F5, whatever). That’s why this has it split to generate Press and Release events, so that you can add whatever delays are necessary between the down and up events.

1 Like

I have not been able to test it yet because I do not know how to add it to my project, I tell you what I`m doing: I create a new c++ class and I choose Blueprint Function Library, then inside Visual Studio I copy your code and click Build project, and it reports me one error :

Error Expected an include at the top of the header: ‘#include “hotkeypress.generated.h”’ testbrow C:\Users\Criminal\Documents\Unreal Projects\testbrow\Source\testbrow\hotkeypress.h 91

from what I understood is that the name of my file is different than your and maybe this is the problem, is it possible to import directly your file?
Should I add something in the cpp file?..I`m lost…

Boss I have been able to do it!!! now I try the function but @Lord_Grym already told us that it works so I can`t be more happy thank you really!!!

1 Like

Glad you got it sorted! It was designed such that you can simply take the SimpleHotkeyLibrary.h file in that Gist and toss it into your project; the next build should automatically pick it up and do the rest. :slight_smile:

1 Like

Hell yeah awesome, happy days!

2 Likes

I didn’t understand how it was done, the project blueprint file was created, how can it be done without entering visual studio in any way, I tried this once and if I didn’t take a backup of the project, I almost lost my project, it said rebuild project and then it gave an error when I built it.
I downloaded the h. file and copied and pasted it into the project, but it doesn’t work.
Does this work in a non-cpp project?
Does it work on 5.2 engine version?

Belatedly, no, I’m afraid it cannot work in a Blueprint-only project. (It’s generally safe to assume as a default that anything in the C++ portion of the forums is probably not going to work in a pure-Blueprint project.)

Unfortunately, Blueprints can only access the functionality that has been deliberately exposed in C++ code; as Unreal itself has not exposed that functionality, new C++ code needs to be added to provide it.

When you add any C++ code to your project, the engine will try to convert it to a C++ project.