getch와 getchar 질문 드립니다
목화
질문 제목 : getch와 getchar 질문 드립니다질문 내용 : 리눅스에서는 conio.h헤더파일이 없기 때문에 getch를 쓰지 못합니다. 그래서 getchar를 써야 됩니다
getch는 문자를 입력받을때 터미널창에 반환하지 않고 getchar는 반환을 합니다.
저는 리눅스에서 getchar를 써야됩니다. 그런데 화면에 입력값을 반환하고 싶지 않은데 어떻게 해야 될까요#include stdio.h
#include stdlib.h
#include termios.h
#include unistd.h
#include fcntl.h
static struct termios initial_settings, new_settings;
static int peek_character = -1;void delay(int n)
{
volatile int i,j;
for(i=0;in;i++);
{
for(j=0;j600;j++);
}
}
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(stdin_fileno, &oldt);
newt = oldt;
newt.c_lflag &= ~(icanon | echo);
tcsetattr(stdin_fileno, tcsanow, &newt);
oldf = fcntl(stdin_fileno, f_getfl, 0);
fcntl(stdin_fileno, f_setfl, oldf | o_nonblock);
ch = getchar();
tcsetattr(stdin_fileno, tcsanow, &oldt);
fcntl(stdin_fileno, f_setfl, oldf);
if(eof != ch)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
int getch(void)
{
int ch;
struct termios buf;
struct termios save; tcgetattr(0, &save);
buf = save;
buf.c_lflag &= ~(icanon|echo);
buf.c_cc[vmin] = 1;
buf.c_cc[vtime] = 0;
tcsetattr(0, tcsaflush, &buf);
ch = getchar();
tcsetattr(0, tcsaflush, &save);
return ch;
}
void main()
{
int a;
while(1){
while(1){
if(kbhit()){
a=getch();
switch(a){
case 49 : printf(1번키 누름:%d\n,a); break;
case 224 :{
a=getch();
printf(↑키 누름:%d\n,a); break;
}
}
}
else{
puts(wait);
delay(100000000);
}
// fflush(stdin);
}
}
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2655576 | 구조체 선언 질문입니다. | 통꽃 | 2024-05-21 |
2655547 | 변수선언과 printf만 사용해서 수식 짜기 (1) | 피리 | 2024-05-20 |
2655523 | 문자열 사이에 문자열삽입 | 바람 | 2024-05-20 |
2655491 | 키보드 인식, 랜덤 함수 or 헤더 (2) | 별 | 2024-05-20 |
2655471 | 링 버퍼 구현된 소스 인데요.. 이해가 잘안되서요.. | 소심한녀자 | 2024-05-20 |
2655440 | test파일이 안 되요. (2) | 리라 | 2024-05-19 |
2655416 | 고수님들 도움이 절실합니다. (2) | 세리 | 2024-05-19 |
2655361 | 엔터 입력받을때까지 기다리는 getchar (1) | 큰모음 | 2024-05-19 |
2655328 | 함수에서 포인터 (3) | 모아 | 2024-05-18 |
2655299 | 아이콘바꾸는법..? | 소 | 2024-05-18 |
2655271 | scanf 입력받으면 쭉 실행되는.. (5) | ComeOn | 2024-05-18 |
2655184 | 이 세개 프로그램좀 해결해주실.... 고수님들???? (3) | 말달리자 | 2024-05-17 |
2655129 | 다차원 배열 원소들 주소계산하는거 질문이요~ | 터전 | 2024-05-17 |
2655103 | AVRstudio MoveAxis. MoveLine 함수질문 | 앨버트 | 2024-05-16 |
2655072 | 콘솔창의 위치와 크기를 정하고 싶습니다. | 딥레드 | 2024-05-16 |
2655044 | 자료구조랑 online judge site에 대해서 질문있습니다... (1) | 뽀송뽀송 | 2024-05-16 |
2655016 | 별찍기 문제 중 이해안되는 부분이 있어요... | 도전중 | 2024-05-16 |
2654986 | 입력받은 성적을 배열에 저장하기 (3) | 안녕나야 | 2024-05-15 |
2654959 | ===========-이런거 미리 지정하는법 (1) | 김자영 | 2024-05-15 |
2654930 | 도와주십시요 ㅜㅜ 자료구조 ㅜㅜ | 도널드 | 2024-05-15 |