[英]Enter key detection in C
我有這個程序:
碼:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
main()
{
char menitem[3][32];
int i = 0, key;
strcpy(menitem[0], "This is the first option.");
strcpy(menitem[1], "And I'm the second option.");
strcpy(menitem[2], "Don't forget me: The third option!");
start:
system("cls");
printf("%s", menitem[i]);
ret:
key = getch();
if(i == 0)
{
switch(key)
{
case 80: i++; goto start;
case '\n': puts("Enter"); getch(); goto start;
default: goto ret;
}
}
else if(i == 2)
{
switch(key)
{
case 72: i--; goto start;
case '\n': puts("Enter"); getch(); goto start;
default: goto ret;
}
}
else
{
switch(key)
{
case 80: i++; goto start;
case 72: i--; goto start;
case '\n': puts("Enter"); getch(); goto start;
default: goto ret;
}
}
}
為什么不能檢測到Enter鍵? 我做錯了嗎?
我嘗試了所有我能找到的東西。
我嘗試使用值10(如ASCII碼中所述),但沒有任何反應。 有人可以告訴我為什么嗎?
沒關系,伙計們。 我發現了怎么樣。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
main()
{
char menitem[3][32];
int i = 0, key;
strcpy(menitem[0], "This is the first option.");
strcpy(menitem[1], "And I'm the second option.");
strcpy(menitem[2], "Don't forget me: The third option!");
start:
system("cls");
printf("%s", menitem[i]);
ret:
key = getch();
if(i == 0)
{
switch(key)
{
case 80: i++; goto start;
case 13: puts("Enter"); getch(); goto start;
default: goto ret;
}
}
else if(i == 2)
switch(key)
{
case 72: i--; goto start;
case 13: puts("Enter"); getch(); goto start;
default: goto ret;
}
else
{
switch(key)
{
case 80: i++; goto start;
case 72: i--; goto start;
case 13: puts("Enter"); getch(); goto start;
default: goto ret;
}
}
}
值為13。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.