当我按下 Bamboo Ink Pen 上的顶部键时,我一直在尝试运行命令。当我按下按钮并停止连接时,当我松开按钮时,我意识到它是通过蓝牙连接的。
我绊倒了连接蓝牙设备时运行脚本当我跑步时
udevadm monitor --environment --udev --kernel --property
并按一次按钮,我得到以下输出:
KERNEL[5118.647193] add /devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/bluetooth/hci0/hci0:3585 (bluetooth)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/bluetooth/hci0/hci0:3585
SUBSYSTEM=bluetooth
DEVTYPE=link
SEQNUM=4094
UDEV [5118.657098] add /devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/bluetooth/hci0/hci0:3585 (bluetooth)
ACTION=add
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/bluetooth/hci0/hci0:3585
SUBSYSTEM=bluetooth
DEVTYPE=link
SEQNUM=4094
USEC_INITIALIZED=5118654305
SYSTEMD_ALIAS=/sys/subsystem/bluetooth/devices/hci0:3585
SYSTEMD_WANTS=bluetooth.target
SYSTEMD_USER_WANTS=bluetooth.target
TAGS=:systemd:
KERNEL[5119.311809] remove /devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/bluetooth/hci0/hci0:3585 (bluetooth)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/bluetooth/hci0/hci0:3585
SUBSYSTEM=bluetooth
DEVTYPE=link
SEQNUM=4095
UDEV [5119.317304] remove /devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/bluetooth/hci0/hci0:3585 (bluetooth)
ACTION=remove
DEVPATH=/devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/bluetooth/hci0/hci0:3585
SUBSYSTEM=bluetooth
DEVTYPE=link
SEQNUM=4095
USEC_INITIALIZED=5118654305
SYSTEMD_ALIAS=/sys/subsystem/bluetooth/devices/hci0:3585
SYSTEMD_WANTS=bluetooth.target
SYSTEMD_USER_WANTS=bluetooth.target
TAGS=:systemd:
不幸的是没有 idVendor 或 idProduct。
当我跑步时
sudo tail -f /var/log/syslog
但是,它抱怨该文件不存在。
udev rules
到目前为止我的样子是这样的。
# Run a program when my Bamboo Ink is connected
ACTION=="add" , SUBSYSTEM=="bluetooth", ATTR{idVendor}=="xxx", ATTR{idProduct}=="yyy", ATTRS{model}=="Bamboo Ink", RUN+="xournalpp"
猜测型号是“Bamboo Ink”,因为我在中找到了这一行journalctl -b
Feb 26 14:57:35 X380-Yoga kernel: wacom 0005:056A:035F.000C: Unknown device_type for 'Bamboo Ink'. Ignoring.
所以我需要找到 idVendor、idProduct 和型号。还有其他方法可以做到这一点吗?
答案1
我是这样做的:
ACTION=="add" , SUBSYSTEM=="bluetooth", DEVPATH=="/devices/pci0000:00/0000:00:14.0/usb1/1-10/1-10:1.0/bluetooth/hci0/hci0:3585", RUN+="/home/gvb/bin/run-pen-state.sh"
我想要做的是在 Xournal 中在笔和橡皮擦之间切换。下面显示了所需的两个脚本。它们需要两个小实用程序:wmctrl 和 xdotool...
“run-pen-state.sh”的内容是
#!/bin/csh
sudo -u gvb -i /home/gvb/bin/pen-state
而“笔状态”本身是
#!/usr/bin/perl -w
$home=$ENV{'HOME'};
$store=`grep "#state: " ~/bin/pen-state | grep -v store`;
chomp($store);
$command=`export DISPLAY=:0; export XAUTHORITY=$home/.Xauthority; wmctrl -l`;
foreach $line (split(/\n/,$command)){
if (($line=~ /Xournal/)&&($line=~ /$filename/)){
$winid=(split(/ /,$line))[0];
}
}
if($winid){
system("export DISPLAY=:0; export XAUTHORITY=$home/.Xauthority; wmctrl -i -a $winid");
if($store =~ /pen/){
$now="state: eraser";
system("export DISPLAY=:0; export XAUTHORITY=$home/.Xauthority; xdotool key shift+ctrl+e");
}else{
$now="state: pen";
system("export DISPLAY=:0; export XAUTHORITY=$home/.Xauthority; xdotool key shift+ctrl+p");
}
$now="#$now";
print "$store\n";
print "$now\n";
open(SELF,"$home/bin/pen-state");
read(SELF,$self,-s "$home/bin/pen-state",0);
close(SELF);
$self=~ s/$store/$now/;
open(SELF,">$home/bin/pen-state");
print SELF $self;
close(SELF);
}
#state: pen
答案2
2022 年 2 月更新:
我刚刚注意到,在 Ubuntu 22.04、Kernel 5.16.8 和 Gnome 41.3 中,它可以开箱即用。顶部按钮被检测为Super+AudioMicMute组合,您可以使用它在 Gnome 设置中分配任何快捷方式。但不知道什么时候开始起作用。
原答案:
我的方法与纪尧姆之前的回答几乎相同。但他的答案对我来说的问题是,我找不到办法区分哪个设备触发了蓝牙子系统的添加操作,因此当我通过蓝牙连接任何其他设备时,纪尧姆的简单规则也匹配。但使用我的 Lenovo Active Pen 2,我注意到每当我按下顶部按钮时,就会发生以下两种情况之一。
情况 1) 笔在一两秒内连接和断开。udevadm monitor
仅显示添加和删除操作。
情况 2) 在 udev 的添加操作之后,笔被识别为名为 HID联想 Active Pen2 键盘。然后大约需要10秒的时间才能将其移除。
ACTION=="add", SUBSYSTEM=="bluetooth", DEVPATH=="/devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/bluetooth/hci0/hci0:3585", RUN+="/usr/local/bin/bluetooth_event_detected.sh add"
ACTION=="remove", SUBSYSTEM=="bluetooth", DEVPATH=="/devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.0/bluetooth/hci0/hci0:3585", RUN+="/usr/local/bin/bluetooth_event_detected.sh remove"
ACTION=="add", SUBSYSTEM=="hid", DEVPATH=="/devices/virtual/misc/uhid/0005:17EF:60A8.*", RUN+="/usr/local/bin/bluetooth_event_detected.sh force"
因此,我在 udev 规则中监视添加和删除操作,这会触发一个脚本,该脚本会计算添加事件和下一个删除事件之间所需的时间。如果花费的时间少于 2 秒,则假定这是笔并在 下设置一个标志/tmp/stylus
。当笔被识别为 HID 时,情况二也会发生同样的情况。
#!/bin/bash
# PATH is not provided in an udev environment
PATH=/bin:/usr/bin:/usr/local/bin
DIRECTORY=/tmp/stylus
TSFILE=$DIRECTORY/bluetooth_timestamp
BTN_PRESSED_FLAG=$DIRECTORY/stylus_button_pressed
TIME_DELTA="2"
ACTION=$1
NOW=$(date +%s)
mkdir -p $DIRECTORY
chmod 777 $DIRECTORY
if [ "$ACTION" = "add" ]; then
echo $NOW > $TSFILE
elif [ "$ACTION" = "remove" ]; then
ADDED_TS=$(cat $TSFILE)
DIFF=$(($NOW-$ADDED_TS))
echo diff: $DIFF
# if the time between adding and removing is
# 2 seconds or less, we assume the stylus button
# was pressed
if [ "$DIFF" -le "$TIME_DELTA" ]; then
touch $BTN_PRESSED_FLAG
chmod 666 $BTN_PRESSED_FLAG
fi
elif [ "$ACTION" = "force" ]; then
touch $BTN_PRESSED_FLAG
chmod 666 $BTN_PRESSED_FLAG
fi
最后,当我登录桌面环境时,我有一个自动启动的简单脚本,它会持续监视/tmp/stylus
并执行gnome-screenshot -i
.
#!/bin/bash
FILE=/tmp/stylus/stylus_button_pressed
CMD="gnome-screenshot -i"
while true; do
if [ -f $FILE ]; then
echo running command
$CMD
rm $FILE
fi
sleep 0.1
done
不优雅,但工作。