最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can i enable and disable the echo in windows terminal using c++ - Stack Overflow

programmeradmin2浏览0评论

Im trying to create a Menu in c++ that is capable of working in linux and ubuntu, the problem is i can't turn the echo back on in widows my current library to work with the terminal looks like this.

#ifndef TC_H
#define TC_H

#include <unistd.h>
#ifdef _WIN32

#include<windows.h>
#include<conio.h>

#define getchar _getch
#define TRAIL_CHR 224
#define ARROW_UP 72
#define ARROW_DOWN 80
#define ARROW_RIGHT 77
#define ARROW_LEFT 75
#define ENTR 13
DWORD mode;

void tc_echo_off() {
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    if (hStdin == INVALID_HANDLE_VALUE) {
        std::cerr << "Couldn't get the standard input handle" << std::endl;
    }
    DWORD mode;
    if (GetConsoleMode(hStdin, &mode)) {
        std::cerr << "Couldn't get the standard input handle" << std::endl;
    }
    mode &= ~ENABLE_ECHO_INPUT;  // Disable echo
    SetConsoleMode(hStdin, mode);
}

void tc_echo_on() {
    HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
    if (hStdin == INVALID_HANDLE_VALUE) {
        std::cerr << "Couldn't get the standard input handle" << std::endl;
    }
    DWORD mode;
    if (GetConsoleMode(hStdin, &mode) != 0) {
        std::cerr << "Failed to get console mode" << std::endl;
    }
    mode |= ENABLE_ECHO_INPUT;  // Enable echo
    SetConsoleMode(hStdin, mode);
}
#else
#include <sys/ioctl.h>
#include <termios.h>

#define TRAIL_CHR (c == 27 && getchar()==91)
#define ARROW_UP 65
#define ARROW_DOWN 66
#define ARROW_RIGHT 67
#define ARROW_LEFT 68
#define ENTR 10
void tc_echo_off(){
    struct termios term;
    tcgetattr(1, &term);
    term.c_lflag &= ~(ICANON|ECHO);
    tcsetattr(1, TCSANOW, &term);
}
void tc_echo_on(){
    struct termios term;
    tcgetattr(1, &term);
    term.c_lflag |= (ECHO|ICANON);
    tcsetattr(1, TCSANOW, &term);
}
#endif
#endif //TC_H

I tried to take the mode variable out of the tc_echo_off function but it didn't work.

发布评论

评论列表(0)

  1. 暂无评论