我想知道如何在 C 编程语言中表示像 Enter、Shift 等键。这些键有二进制值吗? 因为我可以借助二进制值 27 来表示 esc 键。 例如:
int main()
{
int x;
char ch;
while(1)
{
ch=getch();
if(ch==27)
{
printf("esc key pressed");
break;
}
else
{
printf("%c key pressed\n",ch);
}
}
}
在我的程序中,如果我按 esc 它将中断。现在,如果我想用任何其他键(例如 Enter、Shift、Alt)来中断,该怎么做?
最佳答案
其中大部分都依赖于平台。但是,可以使用 keyboard scan codes 检测某些按键。以及以下示例程序:
#include<stdio.h>
int main()
{
int ch;
int ascii[10];
int count=1;
ch=getchar();
ascii[0]=ch;
while (1) //Loop used for storing successive ASCII values
{
ascii[count]=getchar();
if (ascii[count]==10) break;
count++;
}
if (27==ascii[0] && 79==ascii[1] && 80==ascii[2]) printf("F1 is pressed");
if (27==ascii[0] && 79==ascii[1] && 81==ascii[2]) printf("F2 is pressed");
if (27==ascii[0] && 79==ascii[1] && 82==ascii[2]) printf("F3 is pressed");
if (27==ascii[0] && 91==ascii[1] && 72==ascii[2]) printf("HOME key is pressed");
return 0;
}
如果您想要一个更有用的示例,而不必在每次击键后按 ENTER,您可以 try disabling input buffering and enabling raw mode.
关于c - 如何在c中表示键(enter,shift,alt,space等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30439094/