How to emulate a physical key press?

Hello everyone,

I am trying to figure out how to emulate a key press on the keyboard without actually pressing the specific key. For example I would like to trigger an “enter” key press by pressing my mouse (this is just an example…)

The problem is that I have ABSOLUTELY no idea how I could do that. There is probably a simple solution to that but it is definitely not one that I know of… So, if anyone has any idea, please feel free to throw it at me…

All the best,


What should happen when you press something on the keyboard?
In example/events there are a couple of apps that could help you

Well, at first I just wanna “press” a key, meaning that I wanna have the same result as physically pressing a keyboard key. If for example notepad is open to see the letter typed, or if a web browser is open and “press” backspace to go to the previously loaded page, etc.

I didn’t know that there are such examples in events…, I will check them right away… thanks,


In the examples there is no app that emulates the keyword press event, I do not know if this is possible. My suggested solution is to recreate the effect that the key press should triggs, and bind it, for example, to the mouse pressed event

hhhmmmm… OK…, I don’t that this is the way I need to go…, I need to emulate a press in order to “control” external (outside my app) software… No worries though… I will look further into it…

Thanks for the help though, really appreciated.


Autohotkey is a program that lets you send keypresses and emulate mouse activity in Windows:

You could maybe control Autohotkey from OpenFrameworks or, since it’s GPL licensed, look at it’s source code and figure out how they do it.

Thanks for the info… I will try to look into the source code to see if there is a simple way to do it (if it’s too complicated and difficult to replicate I will probably look for another solution) and continue from there.

Thanks again for the help,

All the best,


Found it…!!! For Windows at least… (It’s more of a Windows native commands thing than a general purpose thing…)

Thanks for the help guys,


Would you share some info about how to do it? It might help others in the future :slight_smile:

Absolutely, and I apologize 'cause I should had thought of that without you having to ask…

So, the answer lies to the use of SendInput(), which is a function you can use to send to windows to input events/messages from keyboard or mouse (haven’t checked the whole functionality yet…)

Here is a link to the, somewhat limited, documentation of the function as well as some “extras” needed to use it (at least to a basic level) -> SendInput() function -> INPUT structure (used as argument in SendInput function)

These are two quite good examples from stackOverflow that I have checked -> Quite informative on the basics

This tackles an issue someone had regarding virtual keyboards (and probably the same on mouse) and “controlling” other software…

Unfortunately I cannot provide more than that at the moment because I haven’t gone through coding and testing it yet, but when I do I will post my code along with possible info and issues that I might had to solve during the process…

Hope this helps.



Last news…

I successfully “coded” (actually copied an example from here an example and it worked just fine… I did so outside of openFrameworks though, so I will have to test that too…

Probably (keep in mind though that it is not a good practice to speculate while programming ;):stuck_out_tongue: ) it will work just by including (along with any necessary links, etc) <Windows.h> library and running a similar code fragment to this

#include <Windows.h>

int main() {
	INPUT myInput;


	//Set up the INPUT structure
	myInput.type = INPUT_KEYBOARD; = 0; = 0; //We're doing scan codes instead = 0;

	//This let's you do a hardware scan instead of a virtual keypress = KEYEVENTF_SCANCODE; = 0x1E;  //Set a unicode character to use (A)

						 //Send the press
	SendInput(1, &myInput, sizeof(INPUT));

	//Prepare a keyup event = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
	SendInput(1, &myInput, sizeof(INPUT));

	return 0;

Again…, any news will be posted here… :slight_smile:

Yep… tested and found to work properly… the same example as above was inserted in the setup() function of an ofApp and worked equally fine… So I guess this is the way to go when you wanna emulate simple keyboard and/or mouse input to Windows (unfortunately on Mac will be different). I am not sure how fast or resource consuming this method is and through the little I have found online it is not something that should be abused.

Nevertheless it is a way to implement keyboard and mouse inputs, so I assume (again remember that speculating is not the wisest thing to do when programming) that this is quite harmless to use for simple use.

Hope this will prove to be helpful to some people in the future.

So, take care, have fun and enjoy coding.

All the best,


1 Like

Here is a solution to do it on a mac. I have not tested it, but in brain it works.

ofSystem("tell application \"System Events\" to keystroke \"s\"| osascript");


Hey @ZaellixA please see my latest blog post about[ automating keypress using shell script / terminal commands] ( Nice little library cliclick for automating that stuff on Mac only unfotunately, in line with what @thomasgeissl was suggesting but alternative maybe there is something similar on win out there.

For instance you could do something like this in one call:

ofSystem("cliclick kp:space");

Hope that helps!

Hey @michaelpromeo, thanks for the info. The library looks nice and quite useful. Unfortunately I don’t code on or for Mac. Nevertheless I am sure it will prove to be useful to people who use Macs for their projects.

Thanks again for the info, and keep up with the blog (seems interesting).

All the best,