Mercurial > hg > Members > masakoha > testcode
changeset 36:700212a95109
add
author | Masataka Kohagura <kohagura@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 20 Jan 2015 15:27:43 +0900 |
parents | a19e119d9f2b |
children | 0433a15ea8d2 |
files | c/realtime_input/main.cc |
diffstat | 1 files changed, 79 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/c/realtime_input/main.cc Fri Nov 21 12:18:24 2014 +0900 +++ b/c/realtime_input/main.cc Tue Jan 20 15:27:43 2015 +0900 @@ -1,5 +1,82 @@ #include <stdio.h> +#include <termios.h> +#include <term.h> +#include <curses.h> +#include <unistd.h> + +static struct termios initial_settings, new_settings; +static int peek_character = -1; + +void init_keyboard(); +void close_keyboard(); +int kbhit(); +int readch(); + +int main() +{ + int ch = 0; + + init_keyboard(); + while (ch != 'q') { + printf("looping\n"); + sleep(1); + if (kbhit()) { + ch = readch(); + printf("you hit %X : %c\n",ch,ch); + } + } + close_keyboard(); + _exit(0); +} + -int main(int argc, char *argv[]){ - printf("Hello World!\n"); +void init_keyboard() +{ + tcgetattr(0, &initial_settings); + new_settings = initial_settings; + new_settings.c_lflag &= ~ICANON; + new_settings.c_lflag &= ~ECHO; + new_settings.c_lflag &= ~ISIG; + new_settings.c_cc[VMIN] = 0; + new_settings.c_cc[VTIME] = 0; + tcsetattr(0, TCSANOW, &initial_settings); +} + +void close_keyboard() +{ + tcsetattr(0, TCSANOW, &initial_settings); } + +int kbhit() +{ + char ch; + int nread; + + if (peek_character != -1) + return 1; + new_settings.c_cc[VMIN]=0; + tcsetattr(0, TCSANOW, &new_settings); + nread = read(0, &ch, 1); + new_settings.c_cc[VMIN]=1; + tcsetattr(0, TCSANOW, &new_settings); + + if (nread == 1) { + peek_character = ch; + return 1; + } + return 0; +} + + +int readch() +{ + char ch; + + if (peek_character != -1) { + ch = peek_character; + peek_character = -1; + return ch; + } + read(0, &ch, 1); + return ch; +}