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…
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 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.
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…)
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…
Probably (keep in mind though that it is not a good practice to speculate while programming ;) ) 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;
Sleep(5000);
//Set up the INPUT structure
myInput.type = INPUT_KEYBOARD;
myInput.ki.time = 0;
myInput.ki.wVk = 0; //We're doing scan codes instead
myInput.ki.dwExtraInfo = 0;
//This let's you do a hardware scan instead of a virtual keypress
myInput.ki.dwFlags = KEYEVENTF_SCANCODE;
myInput.ki.wScan = 0x1E; //Set a unicode character to use (A)
//Send the press
SendInput(1, &myInput, sizeof(INPUT));
//Prepare a keyup event
myInput.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;
SendInput(1, &myInput, sizeof(INPUT));
return 0;
}
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.
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).