残培和箭codeS [英] getch and arrow codes

查看:106
本文介绍了残培和箭codeS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写的使用残培()扫描箭头键PROGRAMM。我的code到目前为止是:

I'm writing a programm that's using getch() to scan for arrow keys. My code so far is:

switch(getch()) {
    case 65:    // key up
        break;
    case 66:    // key down
        break;
    case 67:    // key right
        break;
    case 68:    // key left
        break;
}

问题是,当我preSS 'A''B''C''D'的code也将执行,因为 65 是十进制code为'A',等...

Problem is that when I press 'A', 'B', 'C' or 'D' the code will also executed, because 65 is the decimal code for 'A', etc...

有没有一种方法来检查方向键没有打电话给别人?

Is there a way to check for an arrow key without call others?

谢谢!

推荐答案

通过pressing一箭键残培将推三个值入缓冲区:

By pressing one arrow key getch will push three values into the buffer:


  • '\\ 033

  • '['

  • 'A''B''C''D'

  • '\033'
  • '['
  • 'A', 'B', 'C' or 'D'

所以,code将是这样的:

So the code will be something like this:

if (getch() == '\033') { // if the first value is esc
    getch(); // skip the [
    switch(getch()) { // the real value
        case 'A':
            // code for arrow up
            break;
        case 'B':
            // code for arrow down
            break;
        case 'C':
            // code for arrow right
            break;
        case 'D':
            // code for arrow left
            break;
    }
}

这篇关于残培和箭codeS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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