C - Ctrl + 문자에 대한 입력 테스트
-
14-11-2019 - |
문제
는 char 배열에 저장된 입력을 테스트 할 수있는 방법을 궁금합니다 ...
char input[INPUT_SIZE];
.
및 를 사용합니다
fgets(input,INPUT_SIZE,stdin);
.
사용자의 입력을 얻으려면 사용자 입력이 예를 들어 왔는지 테스트 할 수있는 명령문을 사용할 수있는 방법을 궁금합니다. ctrl + d 또는 ctrl + anykey?
이와 같이 ASCCI 값을 사용하여 시도했습니다. Ctrl D를 테스트하는 예제
if(result = 'EOT') {printf("EOT");}
.
결과는 char 배열입니다.
해결책
You can only test Ctrl+d as your read returning EOF
, see the manual of your read to have more info on this, but generally it returns 0. Same goes for Ctrl+c, as both are sending signals to your program.
For other Ctrl+key combinations, it highly depends on your system.
On linux Ctrl+a and Ctrl+e in a shell or emacs will move you to the beginning or the end / beginning of the line respectively.
The easiest to get what you want is to write a small program using read, unbuffered (see ioctl), with a 8-bytes buffer, and dump your read bytes each time you exit the read.
int nbr;
int i;
char buf[8];
nbr = 42;
while (nbr > 0)
{
nbr = read(0, buf, 8);
i = 0;
while (i < nbr)
printf("%x ", buf[i++]);
printf("\n");
}
You will have the hex version of the ctrl+key received sequences. Likely to begin with \ESC or \033 (the escape character sequence). For example the arrow-up key looks like \033[A