linux getch getche

前言

写个发syslog的测试程序,用到无回显,不等待回车的取单个字符api getch.
但是linux中没这个函数.
查资料,stackoverflow上有人解决了。
写个测试程序试试,以后用到的时候在整理。

实验

还是将可复用的功能性代码放在单独的文件中好。
以后用到的时候再整理.

// @file main.cpp

// view date time
// date "+DATE: %m/%d/%y%nTIME: %H:%M:%S"

// set date time
// date -s "2018-3-13 14:25:00"

// https://stackoverflow.com/questions/7469139/what-is-equivalent-to-getch-getche-in-linux

#include <stdlib.h>
#include <stdio.h>
// #include <string.h>
#include <string>

#include <termios.h>

#include <syslog.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <vector>

#include "my_syslog.h"
#include "ls_regex.h"

typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long ulong;
typedef unsigned long long ull;

#define LINE80 "--------------------------------------------------------------------------------"

#ifndef SAFE_DELETE
#define SAFE_DELETE(p) \
    if (NULL != (p)) { \
        delete (p); \
        (p) = NULL; \
    }
#endif // #ifndef SAFE_DELETE

static struct termios old_tos;
static struct termios new_tos;

void init();
void uninit();

int fn_test(int argc, char** argv);

/* Initialize new terminal i/o settings */
void initTermios(int echo) 
{
  tcgetattr(0, &old_tos); /* grab old terminal i/o settings */
  new_tos = old_tos; /* make new settings same as old settings */
  new_tos.c_lflag &= ~ICANON; /* disable buffered i/o */
  if (echo) {
      new_tos.c_lflag |= ECHO; /* set echo mode */
  } else {
      new_tos.c_lflag &= ~ECHO; /* set no echo mode */
  }
  tcsetattr(0, TCSANOW, &new_tos); /* use these new terminal i/o settings now */
}

/* Restore old terminal i/o settings */
void resetTermios(void) 
{
  tcsetattr(0, TCSANOW, &old_tos);
}

/* Read 1 character - echo defines echo mode */
char getch_(int echo) 
{
  char ch;
  initTermios(echo);
  ch = getchar();
  resetTermios();
  return ch;
}

/* Read 1 character without echo */
char getch(void) 
{
  return getch_(0);
}

/* Read 1 character with echo */
char getche(void) 
{
  return getch_(1);
}

int main(int argc, char** argv)
{
    system("clear");
    init();

    fn_test(argc, argv);
    uninit();

    return EXIT_SUCCESS;
}

void init()
{
    ns_syslog::open_syslog("test_new");

    ns_syslog::g_log_condition.b_EMERG = false;
    ns_syslog::g_log_condition.b_CRIT = true;
    ns_syslog::g_log_condition.b_ALERT = true;
    ns_syslog::g_log_condition.b_ERR = true;
    ns_syslog::g_log_condition.b_WARNING = true;
    ns_syslog::g_log_condition.b_NOTICE = true;
    ns_syslog::g_log_condition.b_INFO = true;
    ns_syslog::g_log_condition.b_DEBUG = true;

    ns_syslog::set_log_level(
        ns_syslog::g_log_condition.b_EMERG, 
        ns_syslog::g_log_condition.b_ALERT,
        ns_syslog::g_log_condition.b_CRIT,
        ns_syslog::g_log_condition.b_ERR,
        ns_syslog::g_log_condition.b_WARNING,
        ns_syslog::g_log_condition.b_NOTICE,
        ns_syslog::g_log_condition.b_INFO,
        ns_syslog::g_log_condition.b_DEBUG);
}

void uninit()
{
    ns_syslog::close_syslog();
}

int fn_test(int argc, char** argv)
{
    char c_input = '\0';
    long l_send_cnt = 0;

    do {
        printf("if need quit, press 'q', other key to send local syslog\n");
        c_input = getch(); // 无回显,不等待回车
        if ('q' == c_input) {
            break;
        }

        MYLOG_D("%ld test send local syslog\n", l_send_cnt++);

    } while (1);
    return EXIT_SUCCESS;
}

### 回答1: 在 Linux ,可以使用 `ncurses` 库提供的 `getch()` 函数来获取单个字符的输入,该函数可以读取终端输入的单个字符(包括特殊键码),并返回该字符的 ASCII 码值。以下是一个简单的使用示例: ```c #include <ncurses.h> int main() { // 初始化 ncurses 库 initscr(); // 不显示光标 curs_set(0); // 禁用缓冲 cbreak(); // 不显示输入字符 noecho(); // 等待用户输入 int ch = getch(); // 打印用户输入的 ASCII 码值 printw("You pressed key with ASCII code %d.", ch); // 刷新输出缓冲 refresh(); // 等待用户按下任意键 getch(); // 恢复终端设置并退出 ncurses 库 endwin(); return 0; } ``` 在程序,使用 `initscr()` 函数初始化 ncurses 库,使用 `curs_set()` 函数设置光标的可见性,使用 `cbreak()` 函数禁用行缓冲,使用 `noecho()` 函数禁止显示输入字符。然后使用 `getch()` 函数等待用户输入,读取用户输入的单个字符,使用 `printw()` 函数输出该字符的 ASCII 码值,使用 `refresh()` 函数刷新输出缓冲。最后使用 `getch()` 函数等待用户按下任意键,使用 `endwin()` 函数恢复终端设置并退出 ncurses 库。 ### 回答2: 在Linux,没有直接可用的getch()函数,这是因为Linux采用了标准输入/输出库stdio.h提供的缓冲输入。然而,我们可以使用其他函数来模拟实现getch()的功能。 一种方式是使用无缓冲输入函数tcgetattr()和tcsetattr(),这两个函数可用于设置终端的输入模式。我们可以将终端设置为非缓冲模式,这样键盘敲击的输入就会立即被程序接收。然后,我们可以使用read()函数来接收用户敲击键盘的字符,并将字符返回给调用者,实现类似getch()的功能。 另外一种方式是使用curses库,curses库提供了更高级的终端控制和用户界面功能,包括无缓冲输入。我们可以使用curses库的函数来获取单个字符,例如getch()函数。使用curses库的好处是它提供了更多的终端控制功能,比如光标控制、文本颜色改变等,这对于需要编写交互式控制台程序的开发者来说非常便利。 综上所述,要在Linux实现类似getch()的功能,我们可以使用tcsetattr()和tcgetattr()函数来设置终端的输入模式,或者使用curses库的getch()函数。这样,我们就可以读取并返回用户从键盘输入的单个字符,实现类似于getch()的操作。 ### 回答3: getch是一个用于获取用户输入的函数,主要用于在命令行界面实现键盘控制。在Linux系统,可以使用ncurses库来实现getch函数的功能。 ncurses是一个基于文本的图形库,提供了许多用于在终端上进行图形界面开发的函数。其包含了实现getch函数所需的功能。可以通过在代码引入ncurses库,并调用其相应函数来实现getch的功能。 例如,可以使用initscr函数来初始化终端屏幕,并调用raw函数来禁用终端行缓冲的特性。然后,可以使用getch函数来获取用户按下的键,getch函数会一直等待用户输入直到有按键被按下。 在获取到用户的输入后,可以根据不同的键对其进行处理。例如,可以使用switch语句来判断用户输入的是哪个键,并执行相应的操作。 总之,Linux的getch函数可以通过使用ncurses库来实现,可以用于在命令行界面获取用户的键盘输入,并根据用户的输入执行相应的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值