如何在C代码中使用键F1至F10 [英] how to use the keys F1 to F10 in C code
问题描述
我想知道用户是否按下F1或F2或F3或F4或F5或ESC,然后执行操作,但是我不知道如何获取这些键。
有人可以帮忙吗?
I would like to know whether the user pressed F1 or F2 or F3 or F4 or F5 or ESC and perform an action after that but I don't know how to get these keys. Can anyone help?
推荐答案
对于Windows,有 _getch
函数,该函数返回密钥代码的字节,一一对应。您可以通过这种方式获取功能键,并通过以下某些代码的存在对其进行检测: 0x00
或 0xe0
[C\C ++]-如何使用getch()获得箭头键(正确)? 。当 _getch
返回其中之一时,下一个字节通常是虚拟键码。
For Windows, there is the _getch
function, which returns the bytes of a key code, one by one. You can get a function key that way, detecting it by the presence of certain codes: 0x00
or 0xe0
as shown in the example in [C\C++] - how get arrow keys(correctly) using getch()?. When _getch
returns one of those, the next byte is normally a virtual key code.
要使这些代码有意义,开始阅读的地方是键盘输入参考,查找虚拟键。 Microsoft在此处提供列表: 虚拟键码
For making sense of those codes, the place to start reading is the Keyboard Input Reference, looking for virtual keys. Microsoft provides a list here: Virtual-Key Codes
有些人可能建议从 getch
开始,但Microsoft仅提供 legacy 用途(由于与POSIX冲突,不建议在新程序中使用它,并提供 _getch
作为替代)。
Some people might advise starting with getch
, but Microsoft provides this only for legacy use (does not recommend its use in new programs due to conflict with POSIX, and provides _getch
as the alternative).
这篇关于如何在C代码中使用键F1至F10的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!