在我的C++控制台应用程序中检测箭头键按下时遇到问题.我已经尝试了我在这里和其他教程网站上找到的所有内容,但每当我按下箭头时,所有这些都给了我同样的东西:
Process returned 0 <0x0> execution time : 2.249 s
Press any key to continue.
Run Code Online (Sandbox Code Playgroud)
以下是检测我所尝试的按键的所有方法,所有方法都以相同的方式结束.这些是我的代码中剩下的两个,其他我尝试删除而不是注释掉.
方法一:
c1 = getch();
if(c1 == 0)
{
c2 = getch();
if(c2 == 72) {cout << endl << "Up Arrow" << endl;}
else if(c2 == 80) {cout << endl << "Down Arrow" << endl;}
else{cout << endl << "Incorrect Input" << endl;}
}
Run Code Online (Sandbox Code Playgroud)
方法二:
switch(getch()) {
case 65:
cout << endl << "Up" << endl;//key up
break;
case 66:
cout << endl << "Down" …Run Code Online (Sandbox Code Playgroud) 因此,对于键盘上的向上键,我得到27,令人惊讶的是,对于向下键我也得到27.我需要我的程序在向上和向下键上表现不同,我似乎无法弄明白.我正在使用Linux,需要它才能用于Linux.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
int c = getchar();
if(c==27)
{
printf("UP");
}
if(c==28)
{
printf("DOWN");
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的程序可以检测用户按下箭头键,但我有两个问题。但首先,这是代码:
#include <iostream>
#include <conio.h>
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 77
#define KEY_RIGHT 75
using namespace std;
int main()
{
while(1)
{
char c = getch();
cout << "Hello";
switch(c) {
case KEY_UP:
cout << endl << "Up" << endl;//key up
break;
case KEY_DOWN:
cout << endl << "Down" << endl; // key down
break;
case KEY_LEFT:
cout << endl << "Right" << endl; // key right
break;
case KEY_RIGHT:
cout << endl << "Left" << endl; …Run Code Online (Sandbox Code Playgroud)