Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何检测箭头键_C++_Console Application_Visual C++ 2010_Conio - Fatal编程技术网

C++ 如何检测箭头键

C++ 如何检测箭头键,c++,console-application,visual-c++-2010,conio,C++,Console Application,Visual C++ 2010,Conio,我已经创建了一个应用程序来检测键盘上的上下键 但按下这些键后不会打印任何内容 我使用Visual C++ 2010 < /P> #include <iostream> #include <conio.h> using namespace std; void main() { char x; while(1) { x = getch(); i

我已经创建了一个应用程序来检测键盘上的上下键 但按下这些键后不会打印任何内容

我使用Visual C++ 2010 < /P>

    #include <iostream>
    #include <conio.h>
    using namespace std;

void main()
    {
        char x;

        while(1)
        {

            x = getch();
            if(x==0 || x==224)
            {
                x=getch();
                if(x==80)
                {
                    cout << "down"<<endl;
                }


                else if(x==72)
                {
                    cout << "up"<<endl;
                }
            }//if x==0 || x=224
        }//while1
    }//main
#包括
#包括
使用名称空间std;
void main()
{
字符x;
而(1)
{
x=getch();
如果(x==0 | | x==224)
{
x=getch();
如果(x==80)
{

coutUs kbhit()获取键盘箭头键的步骤

Us kbhit()获取键盘箭头键的步骤

仅回答其不起作用的原因:您正试图将用户的输入用作无符号输入。您的字符变量是有符号的,因此其值与预期值不同。无符号224是有符号的-32

就你的循环而言,我建议把事情改成这个

void main()
    {
        char x;

        while(true)
        {
            while(!kbhit()){}
            x = getch();

            if(x==0 || x==-32)
            {
                x=getch();
                if(x==80)
                {
                    cout << "down"<<endl;
                }


                else if(x==72)
                {
                    cout << "up"<<endl;
                }
            }//if x==0 || x=224
        }//while1
    }//main
void main()
{
字符x;
while(true)
{
而(!kbhit()){}
x=getch();
如果(x==0 | | x==32)
{
x=getch();
如果(x==80)
{

cout仅回答其不起作用的原因:您正试图将用户的输入用作无符号。您的字符变量是有符号的,因此其值与您期望的值不同。无符号224是有符号的-32

就你的循环而言,我建议把事情改成这个

void main()
    {
        char x;

        while(true)
        {
            while(!kbhit()){}
            x = getch();

            if(x==0 || x==-32)
            {
                x=getch();
                if(x==80)
                {
                    cout << "down"<<endl;
                }


                else if(x==72)
                {
                    cout << "up"<<endl;
                }
            }//if x==0 || x=224
        }//while1
    }//main
void main()
{
字符x;
while(true)
{
而(!kbhit()){}
x=getch();
如果(x==0 | | x==32)
{
x=getch();
如果(x==80)
{
你可以使用图书馆。阅读他们的指南,从那里应该很容易。
使用
getch()
(将输入存储到int而不是char)进行输入后,可以使用定义的键验证它是否是箭头键之一。只需确保使用了
键盘(stdscr,TRUE)
之前,让程序能够识别箭头键。

您可以使用该库。阅读他们的指南,从那里应该很容易。
使用
getch()
(将输入存储到int而不是char)进行输入后,可以使用定义的键验证它是否是箭头键之一。只需确保之前使用了
小键盘(stdscr,TRUE)
,程序就能够识别箭头键。

void main
getch()不是标准C++,所以你可能想告诉我们更多关于你的环境的信息,比如你用的是什么库?你尝试打印<代码> x <代码>的值吗?你已经收到了?@ PyrPaMH Prad编辑,所有的信息都被提供了…@仇恨引擎我现在尝试了,它似乎程序永远不会进入(x==0×x=224)。…<代码>空主/ <代码>和<代码> GETCHEL()>代码>不是标准的C++,所以你可能想告诉我们更多关于你的环境的信息,比如你用的是什么库,你试图打印<代码> x<代码>的值?你收到了?@ PrasMah Prad编辑,所有的信息都被提供了……恨我现在尝试了它,程序似乎永远不会进入(x==0 | | x==224)…应用程序如何理解是按向下键还是按向上键?编辑While(1)到While(kbhit())并保持其余代码的不推荐状态。应用程序如何理解是按向下键还是向上键?编辑While(1)到While(kbhit())并保持其余代码的不推荐状态。