- UID
- 7909
- 精华
- 积分
- 62
- 威望
- 点
- 宅币
- 个
- 贡献
- 次
- 宅之契约
- 份
- 最后登录
- 1970-1-1
- 在线时间
- 小时
|
不说了,直接上代码,下面解释:
- #include <stdio.h>
- #include <Windows.h>
- int main()
- {
- HANDLE in_handle = GetStdHandle(STD_INPUT_HANDLE);
- HANDLE out_handle = GetStdHandle(STD_OUTPUT_HANDLE);
- INPUT_RECORD what_key;
- DWORD save_key;
- bool have_press_esc = false;
- while (true)
- {
- ReadConsoleInput(in_handle, &what_key, 1, &save_key);
- if (what_key.EventType == KEY_EVENT && what_key.Event.KeyEvent.bKeyDown == TRUE)
- {
- if (what_key.Event.KeyEvent.wVirtualKeyCode == 0x41) //0x41 is A's virtual key code. It's not a ASCII code.
- {
- printf("You pressed the A key.\n");
- }
- else if (what_key.Event.KeyEvent.wVirtualKeyCode == 0x1B) //0x1B is ESC's virtual key code. VK_ESCAPE == 0x1B
- {
- if (have_press_esc == false)
- {
- have_press_esc = true;
- printf("Press the ESC key again to exit.");
- }
- else if (have_press_esc == true)
- {
- break;
- }
- }
- }
- }
- CloseHandle(in_handle);
- CloseHandle(out_handle);
- return 0;
- }
复制代码 |
|