Keyboard detection under Linux

Source: Internet
Author: User

In other words, it's just a keyboard test that's good trouble. I still know too little ...

#include  <stdio.h> #include  <stdlib.h> #include  <unistd.h> #include  < errno.h> #include  <fcntl.h> #include  <stdio.h> #include  <termios.h> #include  <unistd.h> #include  <string.h> #define  key_esc 27void init_keyboard (); void  close_keyboard (); Int kbhit (); Int readch (); /*  correlation function Declaration  */static struct  termios initial_settings, new_settings;static int peek_character = -1;          /*  a function to test if a key is pressed  *//*  detects keyboard keys  */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) ;} The/*  is used to receive the pressed key and peek_character = -1 the recovery status  */int readch () {    char  ch;    if  ( peek_character != -1 )     {         ch = peek_character;         peek_character = -1;        return (CH);     }     read ( 0, &ch, 1 );     return (ch);} /*  Configuration terminal function  */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] = 1;    new_settings.c_cc[vtime] = 0;     Tcsetattr ( 0, TCSANOW, &new_settings );} Void close_keyboard () {    tcsetattr ( 0, tcsanow, &initial_settings  );} Int main (int argc, char const *argv[]) {    int ch =  0;    init_keyboard ();     printf (  "you can put esc to quit!\n"  );     while   ( ch != 27 )     {         if  ( kbhit ()  )         {     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CH&NBSP;=&NBSP;READCH ();             if  ( ch != 27 )                  printf (  "you put %c !  only put esc can quit! \n ", ch );         }    }    close_keyboard ();     return  0;}


Reference:

    1. http://blog.163.com/liang_w_yan/blog/static/210657088201332444016981/

Keyboard detection under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.