Linux 如何在 bash 中获得 MouseMove 和 MouseClick?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5966903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get MouseMove and MouseClick in bash?
提问by iani
I'm wondering how to get the MouseClick and MouseMove events in bash scripting for my own simple OS events.
我想知道如何在 bash 脚本中为我自己的简单操作系统事件获取 MouseClick 和 MouseMove 事件。
Please tell me how to get that events.
请告诉我如何获得这些事件。
回答by The Bndr
the bash doesn't know anything about an mouse or mouse-clicks. By default there is no cursor or something like that.
bash 对鼠标或鼠标点击一无所知。默认情况下没有光标或类似的东西。
You could install the "General Purpose Mouse Server". Look at this: http://www.linuxfromscratch.org/blfs/view/6.3/general/gpm.htmlfor example to use copy and paste inside an console. Maybe you can use the tools to work with your bash - script.
您可以安装“通用鼠标服务器”。看看这个:http: //www.linuxfromscratch.org/blfs/view/6.3/general/gpm.html例如在控制台中使用复制和粘贴。也许您可以使用这些工具来处理您的 bash - 脚本。
...but at all: there is no native mouse support for the bash-shell
...但根本没有:bash-shell 没有本地鼠标支持
回答by ninjalj
The xterm terminal emulator defines some control sequences to do mouse tracking, you can learn more about them in the section Mouse Trackingin the document ctlseqsfor the xterm distribution. If you have xterm installed, you'll probably have a copy at /usr/share/doc/xterm/ctlseqs.txt.gz
or a similar path.
xterm 终端仿真器定义了一些控制序列来进行鼠标跟踪,您可以在 xterm 发行版的文档ctlseqs中的鼠标跟踪部分中了解更多关于它们的信息。如果您安装了 xterm,您可能会在或类似路径上有一个副本。/usr/share/doc/xterm/ctlseqs.txt.gz
Most terminal emulators running on the X Window System (e.g: Konsole, gnome-terminal, eterm, ...) understand at least some of these control sequences. If you want to use them directly on one of Linux's virtual terminals, you'll probably have to run gpm(8)
.
大多数在 X Window 系统上运行的终端仿真器(例如:Konsole、gnome-terminal、eterm 等)至少理解其中一些控制序列。如果您想直接在 Linux 的虚拟终端之一上使用它们,您可能必须运行gpm(8)
.
There are several control sequences for enabling and disabling mouse movement reporting:
有多种控制序列可用于启用和禁用鼠标移动报告:
- 9 -> X10 mouse reporting, for compatibility with X10's xterm, reports on button press.
- 1000 -> X11 mouse reporting, reports on button press and release.
- 1001 -> highlight reporting, useful for reporting mouse highlights.
- 1002 -> button movement reporting, reports movement when a button is pressed.
- 1003 -> all movement reporting, reports all movements.
- 9 -> X10 鼠标报告,为了与 X10 的 xterm 兼容,报告按钮按下。
- 1000 -> X11 鼠标报告,关于按钮按下和释放的报告。
- 1001 -> 高亮报告,用于报告鼠标高亮显示。
- 1002 -> 按钮移动报告,按下按钮时报告移动。
- 1003 -> 全部运动报告,报告所有运动。
The control sequence is CSI ? number h
for enabling and CSI ? number l
for disabling. CSI is either ESC [
or character 0x9b
. So, you could use them as follows:
控制序列CSI ? number h
用于启用和CSI ? number l
禁用。CSI 要么 要么ESC [
character 0x9b
。因此,您可以按如下方式使用它们:
echo -e "\e[?1000h"
Then, you'll get a bunch of characters on button press, see ctlseqsor console_codes(4)
for details. Then, you can disable mouse tracking with:
然后,您将在按下按钮时获得一堆字符,请参阅ctlseqs或console_codes(4)
有关详细信息。然后,您可以使用以下命令禁用鼠标跟踪:
echo -e "\e[?1000l"
Unfortunately, the previous mouse reporting modes can only handle coordinates up to 223 (255 - 32), or in some situations 95 (127 - 32). So there are some new switches to change the format in which mouse coordinates are reported:
不幸的是,以前的鼠标报告模式最多只能处理 223 (255 - 32) 或在某些情况下 95 (127 - 32) 的坐标。所以有一些新的开关来改变报告鼠标坐标的格式:
- 1006 -> report back as decimal values (xterm, many other terminal emulators, but not urxvt)
- 1015 -> report back as decimal values (urxvt, xterm, other terminal emulators, some applications find it complex to parse)
- 1005 -> report back encoded as utf-8 (xterm, urxvt, broken in several ways)
- 1006 -> 报告为十进制值(xterm,许多其他终端模拟器,但不是 urxvt)
- 1015 -> 报告为十进制值(urxvt、xterm、其他终端模拟器,一些应用程序发现解析起来很复杂)
- 1005 -> 报告返回编码为 utf-8(xterm、urxvt,以多种方式损坏)
A good strategy for an application would be to enable mouse reporting, then (optionally request urxvt 1015 mode and then) request SGR 1006 mode. The application should handle both the new and legacy mouse reporting responses, to continue working on terminal emulators without support for the new modes.
应用程序的一个好策略是启用鼠标报告,然后(可选地请求 urxvt 1015 模式,然后)请求 SGR 1006 模式。应用程序应处理新的和旧的鼠标报告响应,以在不支持新模式的情况下继续在终端仿真器上工作。
More information on the new reporting modes at:
有关新报告模式的更多信息,请访问:
回答by sampath
you can use xdotool for mousemove and mouseclick events. xdotool is a tool which fakes keyboard and mouse. install it by typing, sudo apt-get install xdotool. By using xdotool you can automate almost everything you do using keyboard and mouse.
您可以将 xdotool 用于 mousemove 和 mouseclick 事件。xdotool 是一种假冒键盘和鼠标的工具。通过键入 sudo apt-get install xdotool 来安装它。通过使用 xdotool,您几乎可以自动化使用键盘和鼠标所做的一切。
回答by Omidreza Bagheri
You can use the command 'xte' from package 'xautomation'.
您可以使用“xautomation”包中的“xte”命令。
apt-get install xautomation
As an example, the following command can be noted:
例如,可以注意到以下命令:
xte 'mousemove 200 300'
So, the mouse pointer moves to the width 200 and height 300 of the screen. As an other example, we have:
因此,鼠标指针移动到屏幕的宽度 200 和高度 300。作为另一个例子,我们有:
xte 'mouseclick 3'
that click the right button of mouse (1: left click, 2: middle click, 3: right click). Moreover you can press the keys on keyboard via shell:
单击鼠标右键(1:左键单击,2:中键单击,3:右键单击)。此外,您可以通过 shell 按下键盘上的键:
xte 'keydown Control_L' 'key c' 'keyup Control_L'
This example send ctrl+c to shell.
本示例将 ctrl+c 发送到 shell。
回答by Tinmarino
Short way
捷径
I made a script to make the command line cursor move on mouse click:
我制作了一个脚本来使命令行光标在鼠标单击时移动:
- Enable xterm mouse tracking reporting
- Set readline bindings to consume the escape sequence generated by clicks
- 启用 xterm 鼠标跟踪报告
- 设置 readline 绑定以使用点击生成的转义序列
It can be found here : https://github.com/tinmarino/mouse_xterm/blob/master/mouse.sh
可以在这里找到:https: //github.com/tinmarino/mouse_xterm/blob/master/mouse.sh
Long way
很长的路要走
Xterm have a mouse tracking feature
Xterm 具有鼠标跟踪功能
echo -e "\e[?1000;1006;1015h" # Enable tracking
echo -e "\e[?1000;1006;1015l" # Disable tracking
- Mouse click looks like
\e[<0;3;21M
and a release\e[<0;3;21
. Where2
is x (from left) and22
is y (from top) - Mouse whell up :
\e[<64;3;21M
- Mouse whell down :
\e[<65;3;21M
- Press
C-v
after enabling the mouse tracking to see that
- 鼠标点击的样子
\e[<0;3;21M
和一个释放\e[<0;3;21
。2
x(从左)和22
y(从上) 在哪里 - 鼠标滚起来:
\e[<64;3;21M
- 鼠标滚落:
\e[<65;3;21M
C-v
启用鼠标跟踪后按以查看
Readline can trigger a bash callback
Readline 可以触发 bash 回调
bind -x '"\e[<64;": mouse_void_cb' # Cannot be put in .inputrc
bind '"\C-h" : "$(date) \e\C-e\ef\ef\ef\ef\ef"' #Can be put in .inputrc
Readline can call multiple functions
Readline 可以调用多个函数
# Mouse cursor to begining-of-line before calling click callback
bind '"\C-98" : beginning-of-line'
bind -x '"\C-99" : mouse_0_cb'
bind '"\e[<0;": "\C-98\C-99"'
Readline callback can change cursor (point) position with READLINE_POINT
environment variable
Readline 回调可以使用READLINE_POINT
环境变量更改光标(点)位置
bind -x '"\C-h" : xterm_test'
function xterm_test {
echo "line is $READLINE_LINE and point $READLINE_POINT"
READLINE_POINT=24 # The cursor position (0 for begining of command)
READLINE_LINE='coco' # The command line current content
}
Links
链接
- Xterm control sequences
- Ctrl keys as used in vim source
- zsh script for mouse tracking: the same but in zsh (not bash)
- Xterm 控制序列
- vim 源中使用的 Ctrl 键
- 用于鼠标跟踪的 zsh 脚本:相同,但在 zsh 中(不是 bash)
回答by NVRM
Based on the precious informations given here, and after a bit of digging around.
基于这里给出的宝贵信息,并经过一些挖掘。
We can catch mouse downs and releases, the wheel movement and side, the middle click (wheel click), and positions. No right click.
我们可以捕捉鼠标按下和释放、滚轮移动和侧面、中间点击(滚轮点击)和位置。没有右键。
The following is only an example in php, used as cli. It hide the movements printing on the terminal, and set it back properly when quiting.
以下只是php中的一个例子,作为cli使用。它隐藏了终端上打印的动作,并在退出时正确设置。
It is verbose enough to be adapted in any programming langs able to read the STDINand print to STDOUT, so surely a big list of them!
它足够冗长,可以适应任何能够读取STDIN并打印到STDOUT 的编程语言,所以肯定是一个很大的列表!
#!/usr/bin/php
<?php
system("stty -icanon"); // Enable shell input
system("stty -echo"); // Disable characters printing
echo "\e[?1003h\e[?1015h\e[?1006h"; // Mouse trap all, urxvt, SGR1006
function shutdown(){ // Cleaning before quiting
echo "\e[?1000l"; // Disable mouse trap
system("stty echo"); // Enable back characters printing
exit; // Cleaned, quit
}
register_shutdown_function("shutdown"); // Handle regular END of script
declare(ticks = 1); // Allow posix signal handling
pcntl_signal(SIGINT,"shutdown"); // Catch SIGINT (CTRL+C)
$KEY = "";
while ($KEY = fread(STDIN,16)) {
$e = explode(";",explode("<",$KEY)[1]);
if ($e[0] === "0" && substr($e[2],-1) === "M"){
echo "BUTTON DOWN, LINE ".substr($e[2],0,-1)." COLUMN ".$e[1]."\n";
}
if ($e[0] === "0" && substr($e[2],-1) === "m"){
echo "BUTTON UP, LINE ".substr($e[2],0,-1)." COLUMN ".$e[1]."\n";
}
if ($e[0] === "64"){
echo "WHEEL SCROLL UP, LINE ".substr($e[2],0,-1)." COLUMN ".$e[1]."\n";
}
if ($e[0] === "65"){
echo "WHEEL SCROLL DOWN, LINE ".substr($e[2],0,-1)." COLUMN ".$e[1]."\n";
}
if ($e[0] === "1" && substr($e[2],-1) === "M"){
echo "WHEEL BUTTON DOWN, LINE ".substr($e[2],0,-1)." COLUMN ".$e[1]."\n";
}
if ($e[0] === "1" && substr($e[2],-1) === "m"){
echo "WHEEL BUTTON UP, LINE ".substr($e[2],0,-1)." COLUMN ".$e[1]."\n";
}
if ($e[0] === "35"){
echo "MOUSE MOVE, LINE ".substr($e[2],0,-1)." COLUMN ".$e[1]."\n";
}
}