【问题标题】:Detect enter keypress from read in ZSH从 ZSH 中读取检测输入按键
【发布时间】:2017-05-09 00:48:58
【问题描述】:

如何使用内置的 read 在 ZSH 中检测回车键?也就是如何让下面的sn-p打印“Got enter”

# Read 1 char.
read -k 1 "REPLY?$Make fooBar? [Yn]: "

if [[ "$REPLY" == '\n' ]]; then
  print "Got enter"
else
  print "Got other char: '$REPLY'"
fi

上下文:我正在构建一个比 ZSH 中的 read -q 提供的更灵活的是 - 否提示器。

【问题讨论】:

    标签: shell zsh


    【解决方案1】:

    使用 ANSI 引用:

    if [[ $REPLY == $'\n' ]]; then
    

    $'...' 类似于单引号,但某些转义字符具有特殊含义:\n 是换行符,\t 是制表符,\\ 是文字反斜杠,\' 是文字单引号等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多