如何使用C检测特殊键 [英] How to detect special keys using C
本文介绍了如何使用C检测特殊键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hii,
我需要使用C代码来检测F1键的按下情况
我尝试了此代码,但不适用于F1
Hii,
I need to detect the press of F1 key using C code
I tried this code, but it wont work for F1
printf("Press F1 key to continue\n");
while(getch() == 0x3b)
{
printf("\n F1 Pressed");
getch();
}
推荐答案
MSDN 说getch()
函数已被弃用,建议使用 ^ ]代替.其文档指出:
_getch和_getwch函数从控制台读取单个字符,而不回显该字符.这些功能均不能用于读取CTRL + C.
读取功能键或箭头键时,每个功能必须调用两次;第一次调用返回0或0xE0,第二次调用返回实际的密钥代码.
这些函数锁定了调用线程,因此是线程安全的.有关非锁定版本,请参见_getch_nolock和_getwch_nolock.
我认为这应该可以正常工作:
The MSDN says that thegetch()
function is deprecated and suggest to use the _getch() function[^] instead. Its documentation states:
The _getch and_getwch functions read a single character from the console without echoing the character. None of these functions can be used to read CTRL+C.
When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.
These functions lock the calling thread and are therefore thread-safe. For non-locking versions, see _getch_nolock, _getwch_nolock.
I think that this should work properly:
printf("Press F1 key to continue.\n");
while (true)
{
int c = _getch();
if (c != 0x00 && c != 0xE0) continue;
if (_getch() == 0x3B) break;
}
这篇关于如何使用C检测特殊键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文