首先安装两个软件包:
sudo apt install xbindkeys
sudo apt install xautomation
然后在命令行输入xev | grep button
,这个是用来检测鼠标的键对应的编号,如下所示:
jia@jia:~$ xev | grep button
state 0x0, button 1, same_screen YES
state 0x100, button 1, same_screen YES
state 0x0, button 1, same_screen YES
state 0x100, button 1, same_screen YES
state 0x0, button 3, same_screen YES
state 0x400, button 3, same_screen YES
state 0x0, button 2, same_screen YES
state 0x200, button 2, same_screen YES
state 0x0, button 8, same_screen YES
state 0x0, button 8, same_screen YES
state 0x0, button 9, same_screen YES
state 0x0, button 9, same_screen YES
state 0x0, button 9, same_screen YES
state 0x0, button 9, same_screen YES
state 0x0, button 4, same_screen YES
state 0x800, button 4, same_screen YES
state 0x0, button 5, same_screen YES
state 0x1000, button 5, same_screen YES
state 0x0, button 5, same_screen YES
state 0x1000, button 5, same_screen YES
state 0x0, button 5, same_screen YES
state 0x1000, button 5, same_screen YES
state 0x0, button 2, same_screen YES
state 0x200, button 2, same_screen YES
state 0x0, button 2, same_screen YES
state 0x200, button 2, same_screen YES
state 0x0, button 3, same_screen YES
state 0x400, button 3, same_screen YES
^C
其中我的鼠标左键编号为1,右键为3,滚轮键为2。
输入xbindkeys --defaults > $HOME/.xbindkeysrc
,会在主目录下生成一个.xbindkeysrc
文件,需要在这里面进行鼠标键和命令的映射。映射的格式如下:
"命令"
关联的键
比如:
"xbindkeys_show"
control+shift + q
表示 按下 control+shift + q
键就会执行xbindkeys_show
命令。
在里面添加一个映射:
"xte 'key Return'"
b:2
其中b:2
就代表鼠标中键按下,2是刚才检测出来的鼠标中中键的编号,b代表鼠标(button)。也就是说按下中键的时候会执行xte 'key Return'
这个命令。xte
其实就是用来拟键盘和鼠标事件的工具,执行xte 'key Return'
命令就相当于按下了回车键并松开。xte
的命令如下:
Commands:
key k Press and release key k
keydown k Press key k down
keyup k Release key k
str string Do a bunch of key X events for each char in string
mouseclick i Click mouse button i
mousemove x y Move mouse to screen position x,y
mousermove x y Move mouse relative from current location by x,y
mousedown i Press mouse button i down
mouseup i Release mouse button i
sleep x Sleep x seconds
usleep x uSleep x microseconds
xte
一些常用的键盘按键:
Some useful keys (case sensitive)
Home
Left
Up
Right
Down
Page_Up
Page_Down
End
Return(即Enter键)
BackSpace
Tab
Escape
Delete
Shift_L
Shift_R
Control_L
Control_R
Meta_L
Meta_R
Alt_L
Alt_R
Multi_key
Depending on your keyboard layout, the "Windows" key may be one of the
Super_ keys or the Meta_ keys.
然后再执行xbindkeys -f .xbindkeysrc
即可生效。