Skip to content

Instantly share code, notes, and snippets.

@srayuws
Created March 6, 2020 09:01
Show Gist options
  • Save srayuws/b2ad70d719a0963b93bdd94e5eac888d to your computer and use it in GitHub Desktop.
Save srayuws/b2ad70d719a0963b93bdd94e5eac888d to your computer and use it in GitHub Desktop.
Code to reproduce.
#include <stdio.h>
#include <windows.h>
int main()
{
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);;
INPUT_RECORD irInBuf[128];
DWORD i,cNumRead;
bool exit = false;
printf("Try to press some key\n");
while (!exit) {
ReadConsoleInput(hStdin, irInBuf, 128, &cNumRead);
for (i = 0; i < cNumRead; i++) {
switch (irInBuf[i].EventType)
{
case KEY_EVENT: // keyboard input
KEY_EVENT_RECORD keyEvent = irInBuf[i].Event.KeyEvent;
if (keyEvent.bKeyDown) {
printf("KEY_EVENT_RECORD: \n");
printf("\twRepeatCount: 0x%x\n", keyEvent.wRepeatCount);
printf("\twVirtualKeyCode: 0x%x\n",keyEvent.wVirtualKeyCode);
printf("\twVirtualScanCode: 0x%x\n",keyEvent.wVirtualScanCode);
printf("\tuChar.UnicodeChar: 0x%x\n",keyEvent.uChar.UnicodeChar);
printf("\tuChar.AsciiChar: 0x%x\n",keyEvent.uChar.AsciiChar);
printf("\tdwControlKeyState: 0x%x\n",keyEvent.dwControlKeyState);
printf("\n");
}
if (keyEvent.wVirtualKeyCode == 0x51) { // press Q to exit
exit = true;
}
break;
default:
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment