Linux C - Unix 中 Enter 键的 Ascii 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6790177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ascii value of Enter Key in C - Unix
提问by kiranbhatter
I would like to print "You have pressed Enter key" when ever user hits Enter Key. Please help me write this in C Program.
当用户按下 Enter 键时,我想打印“您已按下 Enter 键”。请帮我用C程序写这个。
Regards, Kiran.
问候,基兰。
回答by Gaurav Shah
Whenever you want to get an ascii value of anything do a "getche()" it will echo a characher pressed. So you run the program and press enter and get the ascii value.
每当你想获得任何东西的 ascii 值时,执行“getche()”,它会回显按下的字符。所以你运行程序并按回车键并获得 ascii 值。
For enter and escape its 27 & 13 .. don't remember which belongs to which
因为进入和逃脱它的 27 & 13 .. 不记得哪个属于哪个
回答by Gilbert
If you don't want to use a magic number you can use a character: C knows it as '\r'
如果你不想使用幻数,你可以使用一个字符:C 知道它是 '\r'
回答by Gilbert
Assuming you mean a CARRIAGE RETURN, it is ASCII 13or Hexadecimal 0x0D(hence my username 0A0D). If you mean LINE FEED, it is ASCII 10or Hexadecimal 0x0A. I will let you enhance your question more before I answer further.
假设您的意思是CARRIAGE RETURN,它是ASCII 13或十六进制 0x0D(因此我的用户名是0A0D)。如果您的意思是LINE FEED,则它是ASCII 10或十六进制 0x0A。在我进一步回答之前,我会让你进一步加强你的问题。
回答by marcelog
#include<stdio.h>
int
main(int argc, char *argv[])
{
int ch;
while(1)
{
ch = fgetc(stdin);
if (ch == 10) {
fprintf(stdout, "You have pressed the enter key\n");
}
}
}
notes: you may want to set your terminal to raw mode, and this vary from OS to OS. in this case, the terminal may be set by default to cooked mode, so the terminal driver from the os might actually not give you a character at the time until you actually press the enter key.
注意:您可能希望将终端设置为原始模式,这因操作系统而异。在这种情况下,终端可能默认设置为熟模式,因此操作系统的终端驱动程序实际上可能不会在当时为您提供字符,直到您实际按下回车键。
to change your terminal to raw mode you might need to check the ioctl()'s needed for your particular OS (if you specify which one, i might be able to help)
要将终端更改为原始模式,您可能需要检查特定操作系统所需的 ioctl()(如果您指定了哪一个,我可能会提供帮助)
回答by spraff
The enter key itself doesn't have an ASCII character, it has a scan code.
回车键本身没有 ASCII 字符,它有一个扫描码。
'\n'
is the ASCII line-ending for UNIX, "\r\n"
on Windows, and '\r'
on Mac.
'\n'
是 UNIX、"\r\n"
Windows 和'\r'
Mac上的 ASCII 行尾。
The scan code is a hardware-specific number which gets translated by the applicationinto a character.
扫描码是特定于硬件的数字,应用程序将其转换为字符。
Where do you capture the key press?
你在哪里捕获按键?
回答by ninjalj
It depends on your terminal input modes, particularly on ICRNL
. See the termios(3)manpage.
这取决于您的终端输入模式,尤其是ICRNL
. 请参阅termios(3)联机帮助页。