美好的生活需要用心记录
今天我把笔记本接了一个外置键盘,现在操作起来比用笔记本自带的内置键盘舒服多了,可惜的是kubuntu无法自动禁用内置键盘,每次手动禁用启用内置键盘是件非常麻烦的事,于是准备弄个自动检测是否连接外置键盘来自动关闭开启内置键盘的脚本,后来选项其实我需要的功能用udev更简单。于是网上找了半天udev教程终于写好了udev规则。
首先我们创建rules文件:
sudo touch /etc/udev/rules.d/60-keyboard.rules
####匹配全部外界键盘## ACTION=="add", SUBSYSTEM=="usb", ATTRS{product}=="*Keyboard", ENV{XAUTHORITY}="/home/xuan/.Xauthority", ENV{DISPLAY}=":0", RUN+="/usr/bin/xinput set-prop 'AT Translated Set 2 keyboard' 'Device Enabled' 0" ACTION=="remove", SUBSYSTEM=="usb", ATTRS{product}=="*Keyboard", ENV{XAUTHORITY}="/home/xuan/.Xauthority", ENV{DISPLAY}=":0", RUN+="/usr/bin/xinput set-prop 'AT Translated Set 2 keyboard' 'Device Enabled' 1" #指定机械键盘 ACTION=="add", SUBSYSTEM=="usb", ATTRS{interface}=="Game_Keyboard", ENV{XAUTHORITY}="/home/xuan/.Xauthority", ENV{DISPLAY}=":0", RUN+="/usr/bin/xinput set-prop 'AT Translated Set 2 keyboard' 'Device Enabled' 0" ACTION=="remove", SUBSYSTEM=="usb", ATTRS{idVendor}=="04b4", ENV{XAUTHORITY}="/home/xuan/.Xauthority", ENV{DISPLAY}=":0", RUN+="/usr/bin/xinput set-prop 'AT Translated Set 2 keyboard' 'Device Enabled' 1"
保存完毕后我们重新加载udev规则:
sudo udevadm control --reload
不过这样还有一个问题那就是如果键盘在系统开机之前就已经接入电脑,那么登录后内置键盘依旧处于开启状态,所以我们要创建一个检测脚本用于登录是检测是否连接外置键盘。
创建keyboard.sh文件,把下面内容写入此文件中:
#!/bin/bash export DISPLAY=:0 if lsusb | grep Keyboard > /dev/null then xinput set-prop "AT Translated Set 2 keyboard" "Device Enabled" 0 elif lsusb | grep 04b4:2018 > /dev/null then xinput set-prop "AT Translated Set 2 keyboard" "Device Enabled" 0 elif lsusb | grep 5c0a:8502 > /dev/null then xinput set-prop "AT Translated Set 2 keyboard" "Device Enabled" 0 else exit fi
保存后赋予可执行权限,然后设置成开机启动就可在登录后自动根据键盘状态关闭内置键盘了。
如果你想通过systemd来进行开机检测是否介入外接键盘要这样。
创建开机脚本:
touch ~/.local/share/systemd/user/keyboard.service
[Unit] Description=off keyboard [Service] ExecStart=/home/xxxx/Public/keyboard.sh [Install] WantedBy=default.target
systemctl --user enable keyboard
还有一个休眠唤醒后再次检测的服务,创建脚本
touch ~/.local/share/systemd/user/keyboard-sleep.service
[Unit] Description=off Keyboard on sleep/resume Before=sleep.target StopWhenUnneeded=yes [Service] ExecStart=/bin/true ExecStop=/home/xuan/Public/keyboard.sh [Install] WantedBy=sleep.target
然后让他自动启动
systemctl --user enable keyboard-sleep.service
udevadm info -a -n /dev/input/by-id/xxxx
#指定机械键盘 ACTION=="add", SUBSYSTEM=="usb", ATTRS{interface}=="Game_Keyboard", ENV{XAUTHORITY}="/home/xxxx/.Xauthority", ENV{DISPLAY}=":0", RUN+="/usr/bin/xinput set-prop 'AT Translated Set 2 keyboard' 'Device Enabled' 0" ACTION=="remove", SUBSYSTEM=="usb", ATTRS{idVendor}=="04b4", ENV{XAUTHORITY}="/home/xxxx/.Xauthority", ENV{DISPLAY}=":0", RUN+="/usr/bin/xinput set-prop 'AT Translated Set 2 keyboard' 'Device Enabled' 1"
ps:手动开启关闭请看此页面的第112条:禁用笔记本键盘下面的内容。
发表评论: