getch()从一个箭头键读取两个字符 [英] getch() reads two characters from one arrow key press

查看:79
本文介绍了getch()从一个箭头键读取两个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我正在测试按键在C ++中的工作方式,并对其进行了简单的循环,发现getch()出于某种原因复制了自身,或者发现发生了什么事情,请看一下:

  #include< iostream>#include< windows.h>#include< conio.h>#定义VK_H 0x48使用命名空间std;int main(){int n = 1;int total = 0;bool theEnd = true;while(theEnd){cout< total<< endl;getch();如果(GetAsyncKeyState(VK_LEFT)){总计-= n;}否则if(GetAsyncKeyState(VK_RIGHT)){总数+ = n;}否则if(GetAsyncKeyState(VK_LSHIFT)&& GetAsyncKeyState(VK_F1)){总数= 0;} else if(GetAsyncKeyState(VK_ESCAPE)){休息;}}cout<合计<< endl;} 

非常简单.程序从一个循环开始,在该循环中不断输出变量"total"的值,然后在按向左/向右按钮"total"后将其减1.

当我使用system("pause")时,它工作得非常完美.睡眠(毫秒);cin.get(); (但这一次假设每次都按回车键,所以不正确),所有这些都在每次按下按钮后在屏幕上输出正确的值.getch();它似乎以某种方式在循环的每两个周期中只有一次.

所以我得到的结果是这样的:我向右按下按钮-当前循环工作正常,但是下一个工作就像没有getch()一样,在没有我的命令的情况下增加并输出了更多时间...

我已经选址和思考了几个小时,尝试在google中找到任何答案,但这里什么也没有...

在不使用getch()或其他东西的情况下PS停止循环直到下一次按下-单次按下它不会为总和增加+1(我需要),但是数百次(平均按下键将执行150-300次循环大声笑)).

解决方案

来自MS文档

备注

_getch和_getwch函数从控制台,而不回显字符.这些功能都不能用于读取CTRL + C.读取功能键或箭头键时,每个该函数必须被调用两次;第一次调用返回0或0xE0,并且第二个调用将返回实际的密钥代码.

当您按箭头键时,输入的是2个字符

Today i was testing how key pressing might work in C++ and made simple loop for it,and found that getch() duplicate itself for some reason or idk what is going on honestly,just look at that:

#include <iostream>
#include <windows.h>
#include <conio.h>

#define VK_H 0x48
using namespace std;

int main()
{
int n=1;
int total=0;
bool theEnd=true;

while(theEnd)
{
    cout<<total<<endl;
    getch();

    if(GetAsyncKeyState(VK_LEFT))
    {
        total -=n;
    }else if(GetAsyncKeyState(VK_RIGHT))
    {
        total +=n;
    }else if(GetAsyncKeyState(VK_LSHIFT) && GetAsyncKeyState(VK_F1))
    {
        total = 0;
    } else if(GetAsyncKeyState(VK_ESCAPE))
    {
        break;
    }
}
cout<<total<<endl;
}

Its pretty simple.Program starts with a loop,where endlessly outputs value of variable "total",and after pressing left/right buttons "total" decrements/increments by 1.

Its was worked fine and perfect when i was using system("pause"); Sleep(milliseconds); cin.get();(but this one assume pressing enter each time,so it is not proper one) ,all that output right value on the screen after each pressing on the buttons.But in case with getch(); it somehow appear to working like only one time per/two cycles of the loop.

So the result i've get is like this: i'm pressing button right - current loop working fine,but next one working like without getch(),incrementing and outputting one more time without my command...

I've siting and thinking couple hours,trying find any answers in google and here but nothing...

P.S.without using getch() or other things for purpose stoping loop till next pressing - it will add not +1 to total by single pressing(as i need),but hundreds(average pressing key will do 150-300 loops lol).

解决方案

From MS documentation

Remarks

The _getch and_getwch functions read a single character from the console without echoing the character. None of these functions can be used to read CTRL+C. When reading a function key or an arrow key, each function must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.

When you press arrow keys, the input is 2 chars

这篇关于getch()从一个箭头键读取两个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆