在Mac终端中-如何从stdin获取箭头键? [英] In the mac terminal - how can I get arrow keystrokes from stdin?
问题描述
我想用箭头键(例如bash'dialog')浏览终端对话框菜单
I'd like to browse a terminal dialog menu with the arrow keys (like bash 'dialog')
我希望使用ruby解决方案,但是bash/python可以工作.
I would prefer ruby solution, but bash/python could work.
read -n1 input # is not good enough, cause the arrow keys are not regular chars.
此外,mac术语中的读取"不支持小于1秒的超时.
Also, the 'read' in mac term doesn't support smaller timeout than 1 second.
有什么事吗?
谢谢
推荐答案
我不确定您在寻找什么-
I am not sure what you are looking for -
- 模拟对应用程序的按键操作的方法,或
- 一种生成简单对话框的方法,或
- 从键盘读取字符的方法...
但是,这些可能会给您一些想法:
However, these may give you some ideas:
对于1 :您可能需要查看Automator和Applescript
For 1: You would probably need to look at the Automator and Applescript
tell application "System Events" to tell process "Finder"
click menu item "New Finder Window" of menu 1 of menu bar item "File" of menu bar 1
end tell
对于2 :您可以查看Platypus,以在脚本周围生成对话框和包装-在此处可用
For 2: You could look at Platypus for generating dialog boxes and wrappers around scripts - available here
对于3 :以下内容可能会做您想要的事情
For 3: The following may do something like you want
#!/bin/bash
#
# Read a key in cbreak mode
stty cbreak -echo; KEY=$(dd bs=1 count=1 2>/dev/null); stty -cbreak echo
#
# If ESCAPE, read next part
if [ $KEY = $'' ]; then
stty cbreak -echo; KEY=$(dd bs=1 count=1 2>/dev/null); stty -cbreak echo
stty cbreak -echo; KEY=$(dd bs=1 count=1 2>/dev/null); stty -cbreak echo
[ $KEY = "A" ] && echo UP
[ $KEY = 'B' ] && echo DOWN
[ $KEY = 'C' ] && echo RIGHT
[ $KEY = 'D' ] && echo LEFT
exit
fi
echo $KEY
我应该解释if [ $KEY
行需要输入
if [ $KEY = $'CONTROL-V ESCAPE' ]
即输入这5件事
$
single quote
Control V
Escape
single quote
这篇关于在Mac终端中-如何从stdin获取箭头键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!