如何在不使用第三方库的情况下检测C语言中的键盘事件?我应该使用信号处理吗?
yzxexxkh1#
没有一个标准的方法,但这些应该让你开始。窗口:
getch();
字符串Unix:使用W. Richard Stevens的《Unix编程》一书中的代码将终端设置为raw模式,然后使用read()。
static struct termios save_termios;static int term_saved;int tty_raw(int fd) { /* RAW! mode */ struct termios buf; if (tcgetattr(fd, &save_termios) < 0) /* get the original state */ return -1; buf = save_termios; buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); /* echo off, canonical mode off, extended input processing off, signal chars off */ buf.c_iflag &= ~(BRKINT | ICRNL | ISTRIP | IXON); /* no SIGINT on BREAK, CR-toNL off, input parity check off, don't strip the 8th bit on input, ouput flow control off */ buf.c_cflag &= ~(CSIZE | PARENB); /* clear size bits, parity checking off */ buf.c_cflag |= CS8; /* set 8 bits/char */ buf.c_oflag &= ~(OPOST); /* output processing off */ buf.c_cc[VMIN] = 1; /* 1 byte at a time */ buf.c_cc[VTIME] = 0; /* no timer on input */ if (tcsetattr(fd, TCSAFLUSH, &buf) < 0) return -1; term_saved = 1; return 0;}int tty_reset(int fd) { /* set it to normal! */ if (term_saved) if (tcsetattr(fd, TCSAFLUSH, &save_termios) < 0) return -1; return 0;}
static struct termios save_termios;
static int term_saved;
int tty_raw(int fd) { /* RAW! mode */
struct termios buf;
if (tcgetattr(fd, &save_termios) < 0) /* get the original state */
return -1;
buf = save_termios;
buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
/* echo off, canonical mode off, extended input
processing off, signal chars off */
buf.c_iflag &= ~(BRKINT | ICRNL | ISTRIP | IXON);
/* no SIGINT on BREAK, CR-toNL off, input parity
check off, don't strip the 8th bit on input,
ouput flow control off */
buf.c_cflag &= ~(CSIZE | PARENB);
/* clear size bits, parity checking off */
buf.c_cflag |= CS8;
/* set 8 bits/char */
buf.c_oflag &= ~(OPOST);
/* output processing off */
buf.c_cc[VMIN] = 1; /* 1 byte at a time */
buf.c_cc[VTIME] = 0; /* no timer on input */
if (tcsetattr(fd, TCSAFLUSH, &buf) < 0)
term_saved = 1;
return 0;
}
int tty_reset(int fd) { /* set it to normal! */
if (term_saved)
if (tcsetattr(fd, TCSAFLUSH, &save_termios) < 0)
型
wi3ka0sx2#
那么好的老kbhit呢?如果我正确理解了这个问题,这将起作用。Here是Linux上的kbhit实现。
8ljdwjyq3#
不幸的是,标准C没有任何检测键盘事件的工具。你必须依赖于特定于平台的扩展。信号处理对你没有帮助。
zwghvu4y4#
你真的应该使用第三方库。在ANSI C中绝对没有独立于平台的方法来做这件事。信号处理不是方法。
yizd12fk5#
我有点迟了,但是:其基本思想是将终端设置为非规范模式,以便您可以单独读取每个按键。
#include <stdio.h>#include <termios.h>#include <unistd.h>void setNonCanonicalMode() { struct termios term; tcgetattr(STDIN_FILENO, &term); term.c_lflag &= ~(ICANON | ECHO); tcsetattr(STDIN_FILENO, TCSANOW, &term);}void resetCanonicalMode() { struct termios term; tcgetattr(STDIN_FILENO, &term); term.c_lflag |= ICANON | ECHO; tcsetattr(STDIN_FILENO, TCSANOW, &term);}
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
void setNonCanonicalMode() {
struct termios term;
tcgetattr(STDIN_FILENO, &term);
term.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &term);
void resetCanonicalMode() {
term.c_lflag |= ICANON | ECHO;
字符串这应该足够了。
5条答案
按热度按时间yzxexxkh1#
没有一个标准的方法,但这些应该让你开始。
窗口:
字符串
Unix:
使用W. Richard Stevens的《Unix编程》一书中的代码将终端设置为raw模式,然后使用read()。
型
wi3ka0sx2#
那么好的老kbhit呢?如果我正确理解了这个问题,这将起作用。Here是Linux上的kbhit实现。
8ljdwjyq3#
不幸的是,标准C没有任何检测键盘事件的工具。你必须依赖于特定于平台的扩展。信号处理对你没有帮助。
zwghvu4y4#
你真的应该使用第三方库。在ANSI C中绝对没有独立于平台的方法来做这件事。信号处理不是方法。
yizd12fk5#
我有点迟了,但是:
其基本思想是将终端设置为非规范模式,以便您可以单独读取每个按键。
字符串
这应该足够了。