慢羊羊的空间

工作做不完了,300出,无瑕。

使用 _getch() 获得方向键和更多的功能键 铜牌收录

通常来说,_getch() 可以返回用户键入的字符。对于一些小游戏,希望用方向键控制的时候,怎么做呢?

先看看 MSDN 的解释:

When reading a function key or an arrow key, _getch and _getche must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.

简单来说,当 _getch() 返回 0 或 0xE0 时,就表示用户按了功能键,这时候需要再调用一次 _getch(),第二次 _getch() 返回的值表示功能键的键码,比如:

  • 72 表示 上
  • 80 表示 下
  • 75 表示 左
  • 77 表示 右

更多的键码,可以自己试验得出。

具体到程序中的应用,请参考“俄罗斯方块”的源代码:https://codebus.cn/zhaoh/a/tetris

添加评论