如何阅读Bash中任意一键? [英] How do I read any one key in Bash?

查看:146
本文介绍了如何阅读Bash中任意一键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能获得读-n 1键来获得最关键的,除了这是通过重新多个字符psented $ P $键。例如,如果我preSS向上箭头键:

  $读-n 1;回声
^ [[A
$ [A

正如你所看到的,只需要在<大骨节病> ESC [A 剩下的。

我希望能够在脚本做的是:


  1. 通过列表,请使用箭头键和preSS <大骨节病>输入以用它做什么

  2. 对于其他操作,preSS不同的密钥。


解决方案

您最好使用提到的对话框,jm666,但也有其他的方式来皮肤猫。

 读-n 1个;而读-n 1 -t .1ÿ;做X =$ X $ Y; DONE

基本上等到你读一个字符,然后旋转消费输入直到0.1秒已通过W / O输入。

警告,快速打字员可能会感到恼火。您可能需要调整的超时时间。

I can get read -n 1 KEY to get most keys, except for keys which are represented by multiple characters. For example, if I press the up arrow key:

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

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

What I want to be able to do in a script is:

  1. Go through a list with the arrow keys and press Enter to do something with it
  2. For other actions, press different keys.

解决方案

You are better off using dialog as jm666 mentioned, but there are other ways to skin that cat.

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

Basically wait until you read a character and then spin consuming input until .1 seconds has passed w/o input.

Warning, fast typists could get annoyed. You might need to tweak that timeout.

这篇关于如何阅读Bash中任意一键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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