- UID
- 7909
- 精华
- 0
- 积分
- 62
- 威望
- 2 点
- 宅币
- 43 个
- 贡献
- 0 次
- 宅之契约
- 0 份
- 最后登录
- 2022-8-5
- 在线时间
- 4 小时
用户组: 小·技术宅
- UID
- 7909
- 精华
- 0
- 威望
- 2 点
- 宅币
- 43 个
- 贡献
- 0 次
- 宅之契约
- 0 份
- 在线时间
- 4 小时
- 注册时间
- 2022-6-12
|
不说了,直接上代码,下面解释:
- #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;
- }
复制代码 |
|