I'm a beginner in C++.
I want to write a program that auto press a key. In this case, I mean send a key pressed event to the foreground window just like we press then release one key on the keyboard, not just send text of this key to the window.
So I write some code like this:
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, const char* argv[])
{
do
{
HWND hwnd = GetForegroundWindow();
SendMessage(hwnd, WM_KEYDOWN, 0x31, 0);//key 1
SendMessage(hwnd, WM_KEYUP, 0x31, 0);
Sleep(1000);
} while (true);
return 0;
}
But this code doesn't seem to work. Could you tell me what's wrong with my code.
Thanks a lot and excuse me for my bad English.