C
C 언어에서 키 입력만 체크하기, _kbhit()
수알치
2017. 2. 8. 19:22
/* Visual Studio C 프로그래밍 */
#include <stdio.h>
#include <conio.h> /* _kbhit() */
#include <ctype.h> /* toupper() */
/* 키가 입력될 때까지 기다린다. 실제로 키를 입력받지는 않는다. */
wait_key()
{
while (!_kbhit());
}
/* a, s, d, w 키를 누를 때 이동 메시지를 출력하는 예제 */
int main()
{
int work = 1;
while (1) {
if (_kbhit()) // 키가 눌렸으면
{
switch(toupper(getch())) { /* 키를 읽어서 대문자로 변환한 뒤 switch 문장 처리 */
case 'A': printf("왼쪽으로 이동합니다.\n");
break;
case 'D': printf("오른쪽으로 이동합니다.\n");
break;
case 'W': printf("위로 이동합니다.\n");
break;
case 'S': printf("아래로 이동합니다.\n");
}
work = 1;
}
else if(work) {
printf("A, D, W, S키를 누르세요.\n");
work = 0;
}
}
}
반응형