【问题标题】:Read a key combination with bash script使用 bash 脚本读取组合键
【发布时间】:2016-06-21 15:05:08
【问题描述】:

对于下面的代码,我希望能够按住一个按钮并按下另一个按钮来执行脚本,比如“Ctrl”+“upArrow”组合键。我猜是某种“key-down”和'key-up' 功能是我需要的。有什么想法吗?

#!/bin/bash
    read -rsn1 input
    while [ "$input" = "a" ]; do
        read -rsn1 input2
        if [ "$input" = "b" ]; then #spaces are important for control flow in bash-script
            echo "Rest in pepperoni memeroni";
        fi

    done

【问题讨论】:

  • 本例中可以使用功能键:

标签: bash


【解决方案1】:

要使用快捷键,请使用 bash bind 命令。

示例:使用F6功能键清屏

bind '"\e[17~":"clear\n"'

当然,您可以将 clear 替换为您的脚本。

要弄清楚F6 符号是什么,只需输入read 命令并按F6

注意:屏幕截图上的^[ 是代码中的\e

【讨论】:

  • 我已经编辑了我的帖子,我特别想使用控制按钮和箭头键来激活代码序列。 bind 也能解决这个问题吗?
  • 是的,它会起作用的。我测试了 CNTL-Left_Arrow,它是代码 ^[OD,我上面的例子仍然有效。所以你会做bind '"\eOD":"your_script\n"'
  • 以防万一有人询问,当您的登录会话结束时,这些“绑定”将被删除。要使它们在会话间保持永久不变,请将 bind 命令放入 bash 终端会话/登录脚本(例如 .bashrc 脚本)中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-26
  • 1970-01-01
  • 1970-01-01
  • 2012-10-14
  • 1970-01-01
相关资源
最近更新 更多