我试图编写一个GUI工具来限制指向特定监视器的指针(例如,一个触摸屏指针应该映射到它自己的屏幕上,而不是所有监视器的联合)。该工具使用Python (使用pygtk)。
对于UI,我需要列出所有的指针,这样您就可以选择所指的指针,然后调用xinput map-to-output pointer_id monitor_id。如果该命令被赋予非指针设备的id,则会引发一个错误,因此我希望避免将这些id作为选项。
xinput list的输出如下所示:
⎡ Virtual core pointer id=2 [master pointer (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎜ ↳ ELAN Touchscreen id=18 [slave pointer (2)]
⎜ ↳ SynPS/2 Synaptics TouchPad id=21 [slave pointer (2)]
⎜ ↳ TPPS/2 IBM TrackPoint id=22 [slave pointer (2)]
⎜ ↳ Cherry USB Optical Mouse Consumer Control id=10 [slave pointer (2)]
⎜ ↳ Cherry USB Optical Mouse id=12 [slave pointer (2)]
⎜ ↳ HID 04b4:3003 Consumer Control id=14 [slave pointer (2)]
⎜ ↳ HID 04b4:3003 Mouse id=24 [slave pointer (2)]
⎜ ↳ WALTOP Graphics Tablet Pen (0) id=26 [slave pointer (2)]
⎣ Virtual core keyboard id=3 [master keyboard (2)]
↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
↳ Power Button id=6 [slave keyboard (3)]
↳ Video Bus id=7 [slave keyboard (3)]
↳ Sleep Button id=8 [slave keyboard (3)]
↳ Integrated Camera: Integrated C id=19 [slave keyboard (3)]
↳ AT Translated Set 2 keyboard id=20 [slave keyboard (3)]
↳ ThinkPad Extra Buttons id=23 [slave keyboard (3)]
↳ Cherry USB Optical Mouse System Control id=9 [slave keyboard (3)]
↳ Cherry USB Optical Mouse Consumer Control id=11 [slave keyboard (3)]
↳ HID 04b4:3003 System Control id=13 [slave keyboard (3)]
↳ HID 04b4:3003 Consumer Control id=15 [slave keyboard (3)]
↳ HID 04b4:3003 Keyboard id=16 [slave keyboard (3)]
↳ HID 04b4:3003 id=17 [slave keyboard (3)]
↳ WALTOP Graphics Tablet id=25 [slave keyboard (3)]要构建菜单,我需要获得所有指针的名称和id (我猜从指针,我不知道如果我选择Virtual指针会发生什么)。一方面,xinput list --id-only和xinput list --name-only给出了我需要的确切信息,但我需要过滤出不是指针的ids和名称。另一方面,我可以使用xinput list | grep pointer来获得相关的行,但是结果看起来不太好解析(有多余的括号和奇怪的↳箭头字符)。我试着在man xinput中寻找选项,要么进行一些筛选,要么简化输出,但什么也找不到。
我的项目是基于ptxconf的,他们的解决方案如下。我希望能找到更优雅的东西。
def getPenTouchIds(self):
"""Returns a list of input id/name pairs for all available pen/tablet xinput devices"""
retval = subprocess.Popen("xinput list", shell=True, stdout=subprocess.PIPE).stdout.read()
ids = {}
for line in retval.split("]"):
if "pointer" in line.lower() and "master" not in line.lower():
id = int(line.split("id=")[1].split("[")[0].strip())
name = line.split("id=")[0].split("\xb3",1)[1].strip()
if self.getPointerDeviceMode(id) == "absolute":
ids[name+"(%d)"%id]={"id":id}
return ids发布于 2022-04-27 11:27:30
我遇到了同样的问题,试图在xinput列表中匹配我的鼠标,我习惯于使用sed来处理这类问题,下面是基于sed的方法:
MOUSE_G903_ID=$(xinput --list | sed -rn '/^.*Virtual core pointer/,/^.*Virtual core keyboard/ s/^.*Logitech G903 LS[[:blank:]]+id=([[:digit:]]+).*/\1/p')-n开关使sed抑制所有输出,因此必须使用p命令显式地打印所需的内容,或者在本例中使用S命令的p标志。
有一个单独的sed表达式(单引号),它首先定义要操作的范围:两个“头行”(虚拟核心指针、虚拟核心键盘)之间的行范围。
在范围之后,一个S命令匹配与我的鼠标对应的行,捕获数字id并打印它。
https://unix.stackexchange.com/questions/627804
复制相似问题