Is there a way to capture keys pressed, even when the C++ program is not the active form? I mean that the program is running but is in the background. So can a person be using Word and there still be a way to extract the keys they pressed from the buffer? I tried with kbhit() and getch() but I was not succesfull.
Here's something I rigged together using <windows.h>'s GetKeyState() function:
1 2 3 4 5 6 7 8 9 10 11
int getKey(){
int buttonpressed = 0;
while (!buttonpressed){
for (int i = 0; i < 255; i++){
if (GetKeyState(i) < 0){buttonpressed = i;}
}
}
while (GetKeyState(buttonpressed) < 0){;}
return buttonpressed;
}
It doesn't play nicely with cin, it only gets one key at a time, and it probably eats up a lot of cycles. It's worse than a lot of alternatives, but it's better than nothing.