zoukankan      html  css  js  c++  java
  • c 终端控制

    #include <stdio.h>
    #include <termios.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <assert.h>
    #include <string.h>
    
    char my_getch()
    {
            int c=0;
            struct termios org_opts, new_opts;
            int res=0;
    
            res=tcgetattr(STDIN_FILENO, &org_opts);
            assert(res==0);
    
            memcpy(&new_opts, &org_opts, sizeof(new_opts));
            new_opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
            tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
    
            c=getchar();
            res=tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
            assert(res==0);
            return c;
    }

    键盘输入不回显处理

    /×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××/

    #include <stdio.h>
    #include <stdlib.h>
    #include <termios.h>
    #include <curses.h>
    #include <term.h>
    #include <unistd.h>
    
    static int peek_character = -1; 
    
    int kbhit()
    {
        char ch;int x;
        int nread;
        struct termios old_settings,new_settings;
    
        tcgetattr(STDIN_FILENO,&old_settings);
        new_settings=old_settings;
        new_settings.c_lflag &=~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ECHOPRT|ECHOKE|ICRNL)  ;
        new_settings.c_cc [VMIN]=1  ;
        new_settings.c_cc [VTIME]=0  ;
        tcsetattr(STDIN_FILENO,TCSANOW,&new_settings);
    
        if(peek_character!=-1)
            x= 1;
        new_settings.c_cc [VMIN]=0  ;
        tcsetattr(STDIN_FILENO,TCSANOW,&new_settings);
    
        nread=read(0,&ch,1);
        new_settings.c_cc [VMIN]=1  ;
        tcsetattr(STDIN_FILENO,TCSANOW,&new_settings);
    
        if(nread==1) {
            peek_character=ch;
            x=1;
        }   
        else x=0;
        tcsetattr(STDIN_FILENO,TCSANOW,&old_settings);
        return x;
    }
    int readch()
    {
        char ch; 
    
        if(peek_character!=1) {
            ch=peek_character;
            peek_character=-1;
            return ch; 
        }   
        read(0,&ch,1);
        return ch; 
    }

    检测有键盘输入并返回键值

  • 相关阅读:
    Vertica 业务用户指定资源池加载数据
    Vertica 数据库知识汇总篇
    Oracle OCP 1Z0-053 Exam Topics
    Oracle 11gR2 RAC修改监听默认端口
    Oracle 11.2.0.4 RAC安装最新PSU补丁
    Openfiler配置RAC共享存储
    ORACLE 11gR2 DG(Physical Standby)日常维护01
    oracle 存储过程 返回结果集
    exception javax.crypto.BadPaddingException: Given final block not properly padded
    plsql 连接oracle数据库的2种方式
  • 原文地址:https://www.cnblogs.com/chencesc/p/4287755.html
Copyright © 2011-2022 走看看