linux下的鍵盤檢測

來源:互聯網
上載者:User

標籤:

話說,僅僅是一個鍵盤檢測就好麻煩。我還是懂的太少了。。。

#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(); /* 相關函式宣告 */static struct termios initial_settings, new_settings;static int peek_character = -1;         /* 用於測試一個按鍵是否被按下 *//* 檢測鍵盤按鍵的函數 */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);}/* 用來接收按下的按鍵,並peek_character = -1恢複狀態 */int readch(){    char ch;    if ( peek_character != -1 )    {        ch = peek_character;        peek_character = -1;        return(ch);    }    read( 0, &ch, 1 );    return(ch);}/* 配置終端函數 */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() )        {            ch = readch();            if ( ch != 27 )                printf( "You put %c ! Only put ESC can quit! \n", ch );        }    }    close_keyboard();    return 0;}


參考:

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

linux下的鍵盤檢測

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.