【问题标题】:keyboad key codes table on LinuxLinux 上的键盘键码表
【发布时间】:2019-11-30 20:26:10
【问题描述】:

我正在学习 C++。

我正在测试哪个键按下了用户。我写了这段代码:

#include <signal.h>
#include <termios.h>
#include <stdio.h>
#include <math.h>

#define KEYCODE_L 0x44 // Left Arrow Key
#define KEYCODE_R 0x43 // Right Arrow Key
#define KEYCODE_U 0x41 // Up Arrow Key
#define KEYCODE_D 0x42 // Down Arrow Key
#define KEYCODE_Q 0x71 // Q Key

int main(int argc, char **argv)
{
  // get the console in raw mode
  tcgetattr(kfd, &cooked);
  memcpy(&raw, &cooked, sizeof(struct termios));
  raw.c_lflag &= ~(ICANON | ECHO);

  // Setting a new line, then end of file
  raw.c_cc[VEOL] = 1;
  raw.c_cc[VEOF] = 2;
  tcsetattr(kfd, TCSANOW, &raw);

  puts("Reading from keyboard");
  puts("---------------------------");
  puts("Use arrow keys to move the robot.");

  // get the next event from the keyboard
  if (read(kfd, &c, 1) < 0)
  {
    perror("read():");
     /**
     * Reset console to its original mode.
     */
     tcsetattr(kfd, TCSANOW, &cooked);
     exit(-1);
  }

  switch (c)
  {
   case KEYCODE_R:
     std::cout << "Right Arrow" << std::endl;
      break;
   case KEYCODE_L:
     std::cout << "Left Arrow" << std::endl;
      break;
    case KEYCODE_U:
     std::cout << "Up Arrow" << std::endl;
      break;
    case KEYCODE_D:
     std::cout << "Down Arrow" << std::endl;
      break;
  }
}

在哪里可以找到我称之为 KEYCODE_ 的所有值的表?

我正在寻找一个包含所有键值的表。我找到了一个 JavaScript 表,但值不匹配。

【问题讨论】:

  • 昨天不是问过同样的问题吗?
  • 没有。这是一个新的。

标签: c++ linux keyboard


【解决方案1】:

这些是终端提供给您的 Ascii 代码。您可以在 Linux 中使用showkey -a 命令查看它们。

有关此命令的更多信息可在手册页和在线获取,例如,https://linux.die.net/man/1/showkey

【讨论】:

  • 有没有类似的方法通过Windows获取ACSII代码?
  • @Cygnus 抱歉,不知道,问题显然是针对 Linux 的(&lt;termios.h&gt; 是 *nix 特定的标头)。
  • 没问题。虽然问题是指 Linux,但我只是想知道它是否适用于 Windows。
  • @Cygnus 你可能想问一个单独的问题,也许比我更了解 Windows 的人可以回答这个问题?
猜你喜欢
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
相关资源
最近更新 更多