Simulate a key press for a window

Posts 1–5 of 5 · Page 1 of 1
Simulate a key press for a window
Hello , can someone help me to find a function that can simulate a key press for a window ? For exemple , if I want to simulate the keyboard letter "X" I have to stay in the window to make it work , but I was wondering how to make it so it will simulate the key "X" for a window even if im not on it ? So I can watch a youtube video , and the program still spamming the letter 'X' in my notepad ! Thank you for help !
Quote Originally Posted by Void View Post
Thanks !! Can you make an exemple please ? A sample code that write the letter 'x' in notepad window ?
This will send key strokes to an inactive window

Code:
#define VK_ENTER 0x0D  // 0x0D is a hex value for the key Enter
#define VK_E 0x45  // Same as above
#define VK_K 0x4B  // Same as above
#define VK_L 0x4C  // Same as above
#define VK_Y 0x59  // Same as above

void main()
{
HWND Notepad = FindWindow(TEXT("Notepad"), NULL);  // Looks for the window titled Notepad
HWND edit = FindWindowEx(Notepad, NULL, TEXT("Edit"), NULL);  // In the window there is a class called Edit
PostMessage(edit, WM_KEYDOWN, VK_K, 1);  // Posts a message to the edit window "K"
PostMessage(edit, WM_KEYDOWN, VK_Y, 1);  // "Y"
PostMessage(edit, WM_KEYDOWN, VK_L, 1);  // "L"
PostMessage(edit, WM_KEYDOWN, VK_E, 1);  // "E"
PostMessage(edit, WM_KEYDOWN, VK_ENTER, 1);   // Sends enter
}
The only thing about this, yes it will work for some inactive windows, but not all. For instance emulated games (flash games or emulators) this won't work.... I've tried. It will actually send the key strokes to the window but it won't actually be there UNTIL you make the window active.
@ @mic44 Read the solution above and tell us the result.
Posts 1–5 of 5 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?