如何读取 Bash 中的任意一个键?

2024-01-06

我可以得到read -n 1 KEY获取大多数键,但由多个字符表示的键除外。例如,如果我按向上箭头键:

$ read -n 1; echo
^[[A
$ [A

As you can see, read only takes the Esc and the [A is left over.

我希望能够在脚本中执行以下操作:

  1. Go through a list with the arrow keys and press Enter to do something with it
  2. 对于其他操作,请按不同的键。

你最好使用 jm666 提到的对话框,但还有其他方法来剥皮那只猫。

read -n 1 x; while read -n 1 -t .1 y; do x="$x$y"; done

基本上等到您读取一个字符,然后旋转消耗输入,直到 0.1 秒过去(无输入)。

警告,打字速度快的人可能会被惹恼。您可能需要调整该超时。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何读取 Bash 中的任意一个键? 的相关文章