How to Simulate a Key Press in C++

How to simulate a key press in C++

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

Simulate keypress in a Linux C console application

I assume you mean the "X11 application" - it is not entirely clear from your description what you are planning to do. The below code snippet will send the "pause" keycode to the application that currently has the keyboard input focus under X11 using XTest extension - from what I've read this is the most compatible way to "fake" the keyboard events. See if you might apply this to your scenario (no error check on whether the XOpenDisplay succeeded, to make it simpler).

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
...
Display *display;
unsigned int keycode;
display = XOpenDisplay(NULL);
...
keycode = XKeysymToKeycode(display, XK_Pause);
XTestFakeKeyEvent(display, keycode, True, 0);
XTestFakeKeyEvent(display, keycode, False, 0);
XFlush(display);

You will need to link with the -lX11 -lXtst.

Obviously the firefox would need to have focus at that time.

However, I would be curious to know what is the bigger task that you are trying to accomplish - I suspect there should be a more elegant solution than spoofing the keypress events.

How to simulate multimedia key press (in C)?

Use the SendInput Windows API, if you are talking about programming under Win32.

You need to build INPUT structures, setting the type member to INPUT_KEYBOARD. In the ki member (KEYBDINPUT type), you can set vk (virtual key) to your desired VK code (for example, VK_MEDIA_NEXT_TRACK, VK_MEDIA_STOP).

Virtual key codes: http://msdn.microsoft.com/en-us/library/dd375731(v=VS.85).aspx

SendInput Function: http://msdn.microsoft.com/en-us/library/ms646310(v=VS.85).aspx

I've not tested the following, but should be like this:

KEYBDINPUT kbi;
kbi.wVk = VK_MEDIA_STOP; // Provide your own
kbi.wScan = 0;
kbi.dwFlags = 0; // See docs for flags (mm keys may need Extended key flag)
kbi.time = 0;
kbi.dwExtraInfo = (ULONG_PTR) GetMessageExtraInfo();

INPUT input;
input.type = INPUT_KEYBOARD;
input.ki = kbi;

SendInput(1, &input, sizeof(INPUT));

How to Simulate Holding Down a Key in C++

SOLUTION: Okay, through further experience with this, it seems the problem with my original code was the lack of KEYEVENTF_SCANCODE in my dwFlags. There seems to be a underlying relationship that requires the KEYEVENTF_SCANCODE flag in order to hold down an arrow key at least in Chrome, maybe systemwide. So here is the bare-bones solution:

#include <windows.h>

void keyPress(WORD keyCode)
{
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.wScan = keyCode;
input.ki.dwFlags = KEYEVENTF_SCANCODE;

SendInput(1, &input, sizeof(INPUT));
}

void keyRelease(WORD keyCode)
{
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.wScan = keyCode;
input.ki.dwFlags = KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP;

SendInput(1, &input, sizeof(INPUT));
}

int main()
{
Sleep(5000); // pause time to open browser tab
keyPress(0x24); // press key
Sleep(1000); // hold key for x milliseconds
keyRelease(0x24); // release key
return 0;
}

I'm confused why the scan codes arent lining up with the ones provided by microsoft ("0x25" is down arrow instead of "0x28"?) so if someones got some insight for that, then that'd be great.

SOLUTION EDIT: The reason why my keycodes weren't lining up was because I need to use scancodes. I had some dumb luck in that the game I am botting also uses IJKL as arrow keys so it appeared as though the codes were off set by a few numbers when in actuality it was pressing letter keys the whole time. So basically all I need to do is use these scan codes to move: 0x17="I" 0x24="J" 0x25="K" 0x26="L"

I hope this helps someone as lost as I was (:

also, pro tip, code macro bots in python, its much less of a headache. I abandoned my c++ code and got my code running on python in 2 evenings



Related Topics



Leave a reply



Submit