Linux에서 getch () 및 getche ()에 해당하는 것은 무엇입니까?
Linux에서 conio.h에 해당하는 헤더 파일을 찾을 수 없습니다.
Linux 에 getch()
& getche()
기능에 대한 옵션이 있습니까?
사용자가 하나의 키를 누르는 것만으로 옵션을 줄 수있는 케이스 전환베이스 메뉴를 만들고 싶습니다. 사용자가 자신의 선택을 누른 후 Enter 키를 누르는 것을 원하지 않습니다.
#include <termios.h>
#include <stdio.h>
static struct termios old, current;
/* Initialize new terminal i/o settings */
void initTermios(int echo)
{
tcgetattr(0, &old); /* grab old terminal i/o settings */
current = old; /* make new settings same as old settings */
current.c_lflag &= ~ICANON; /* disable buffered i/o */
if (echo) {
current.c_lflag |= ECHO; /* set echo mode */
} else {
current.c_lflag &= ~ECHO; /* set no echo mode */
}
tcsetattr(0, TCSANOW, ¤t); /* use these new terminal i/o settings now */
}
/* Restore old terminal i/o settings */
void resetTermios(void)
{
tcsetattr(0, TCSANOW, &old);
}
/* 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);
}
/* Let's test it out */
int main(void) {
char c;
printf("(getche example) please type a letter: ");
c = getche();
printf("\nYou typed: %c\n", c);
printf("(getch example) please type a letter...");
c = getch();
printf("\nYou typed: %c\n", c);
return 0;
}
산출:
(getche example) please type a letter: g
You typed: g
(getch example) please type a letter...
You typed: g
#include <unistd.h>
#include <termios.h>
char getch(void)
{
char buf = 0;
struct termios old = {0};
fflush(stdout);
if(tcgetattr(0, &old) < 0)
perror("tcsetattr()");
old.c_lflag &= ~ICANON;
old.c_lflag &= ~ECHO;
old.c_cc[VMIN] = 1;
old.c_cc[VTIME] = 0;
if(tcsetattr(0, TCSANOW, &old) < 0)
perror("tcsetattr ICANON");
if(read(0, &buf, 1) < 0)
perror("read()");
old.c_lflag |= ICANON;
old.c_lflag |= ECHO;
if(tcsetattr(0, TCSADRAIN, &old) < 0)
perror("tcsetattr ~ICANON");
printf("%c\n", buf);
return buf;
}
printf
문자를 표시하지 않으려면 마지막을 제거하십시오 .
curses.h 또는 ncurses.h를 사용하여 getch ()를 포함한 키보드 관리 루틴을 구현하는 것이 좋습니다. getch의 동작을 변경할 수있는 몇 가지 옵션이 있습니다 (예 : 키 누름 대기 여부).
ncurses 라이브러리에는 getch () 함수가 있습니다. ncurses-dev 패키지를 설치하여 얻을 수 있습니다.
You can use the curses.h
library in linux as mentioned in the other answer.
You can install it in Ubuntu by:
sudo apt-get update
sudo apt-get install ncurses-dev
I took the installation part from here.
As said above getch()
is in the ncurses
library. ncurses has to be initialized, see i.e. getchar() returns the same value (27) for up and down arrow keys for this
ReferenceURL : https://stackoverflow.com/questions/7469139/what-is-the-equivalent-to-getch-getche-in-linux
'Development Tip' 카테고리의 다른 글
try {} catch {}와 if {} else {}의 장점은 무엇입니까? (0) | 2020.12.15 |
---|---|
파이썬에서 2의 보수 (0) | 2020.12.15 |
Pylint의 파일 수준에서 "missing docstring"경고를 비활성화하려면 어떻게해야합니까? (0) | 2020.12.15 |
Node.js를 사용하여 파일 시스템의 디렉토리 구조를 JSON으로 변환 (0) | 2020.12.15 |
C # 보호 메서드 단위 테스트 (0) | 2020.12.15 |