我想捕获输入以控制屏幕截图功能。
vim myscreenshot.sh
screenshot(){
ffmpeg -video_size 1920x1080 -framerate 25 -f x11grab -i :0.0+0,0 /tmp/out.mp4
}
close(){
echo "screenshot is over"
exit
}
trap 'screenshot' ctrl+a #how to fix it?
trap 'close' ctrl+b #how to fix it?
命令 trap 'screenshot' SIGINT
可以捕获 ctrl+c
。
我的要求:输入ctrl+a
启动截图功能,ctrl+b
停止截图功能,后台运行bash myscreenshot.sh &
后退出。
最佳答案
Bash 陷阱不是处理键盘输入的方法;他们处理signals .它适用于 Ctrl+C,因为您的终端(不是 Bash)被配置为在按下该组合键时向 Bash 发送 SIGINT 信号(运行 stty -a
查看所有此类绑定(bind))。但信号并非设计为以这种方式使用。 trap ... SIGINT
应该 almost always be used to interrupt and terminate the program .
正如@MarkSetchell 建议的那样,bind
是配置键绑定(bind)的正确工具,如 Ctrl+A(符号:C-a)。该手册包含有关 existing bindings you can configure 的更多详细信息(具体请参阅“杂项命令”,它提供了一些有用的行为,如 Esc Ctrl+A (M-C-e ) 其中 expands all variables in the current pending command )。请注意 C-a 是 bound to beginning-of-line
by default , C-b 绑定(bind)到 backward-char
。 Readline manual进入更多细节,因为这是所有这些行为的实际原因。网上还有很多关于让 bind
做不同事情的文章和例子,所以简单地搜索“bind”而不是“trap”可能就足以让你开始了:)
以下是您可以尝试的几个示例:
bind "\C-a":display-shell-version
将导致 Ctrl+A 调用 readline 函数 display- shell-version
打印当前 Bash 版本,而不是将光标跳到行首。bind -x '"\C-b":"echo Hello World"'
(注意 -x
) 会导致 Ctrl+B 调用 shell 命令 echo Hello World
,而不是将光标向后移动一个字符。听起来您想配置任意键绑定(bind),由于涉及不同的层,您可能会发现这是一场艰苦的战斗。用“适当的”编程语言编写的专用 GUI 感知程序可以监视您的键盘,它更适合您所描述的内容。显然,这比与 Bash 搏斗要多得多,所以希望您能从 bind
中得到足够好的东西:)
https://stackoverflow.com/questions/61821625/
相关文章:
swiftui - 我怎样才能在 SwiftUI 中实现类似于 radiogroup 的一种可能选择
django - 将 Django 连接到 Microsoft SQL 数据库
javascript - 在 includes() 中使用 "OR"运算符来试验字符串中是否存在任何
ios - SecurityApplicationGroupIdentifier 的应用组返回 ni
javascript - 如何使用 nestfastify 编写来自 nestjs 异常过滤器的响应
scala - 通过方法类型参数分配给类型成员的值打破了类型等价
amazon-web-services - 如何替换 AWS Auto Scaling 组中的特定实
swift - 在 Vapor 中使用原始 sql 返回条目总数