Is there a way to read user input until the ESC key(or any other key) is pressed? I've seen forums about it but they've all been for C++. I need to make one that works for C. Thanks
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Let's check 'esc' character in ascii table:
$ man ascii | grep -i ESC
033 27 1B ESC (escape)
$
Therefore, it's ascii value is:
- '033' - Octal Value
- '27' - Integer Value
- '1B' - Hexadecimal Value
- 'ESC' - Character Value
A sample program using integer value of 'ESC':
#include <stdio.h>
int main (void)
{
int c;
while (1) {
c = getchar(); // Get one character from the input
if (c == 27) { break; } // Exit the loop if we receive ESC
putchar(c); // Put the character to the output
}
return 0;
}
Hope that helps!