linux键盘锁定,python – 如何“锁定键盘”以防止在X11 / Linux / Gnome上发送更多的按键?...

本文介绍了一个使用xinput的shell脚本,该脚本能够轻松地切换键盘的状态。通过设置特定属性,脚本能够在激活和非激活状态之间切换指定的键盘设备。此外,文章还提到了如果无法安装xinput,可以通过获取其源代码并在Python环境中利用类似python-xlib的库来重新实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用xinput的shell脚本可以轻松完成此操作:

#!/bin/sh

do_it() {

# need error checking there. We should also restrict which device gets

# deactivated, by checking other properties.

keyboard_ids="$(xinput list | sed -rn 's/.*id=([0-9]+).*slave\s+keyboard.*/\1/p')"

for keyboard_id in $keyboard_ids; do

# 121 is "Device Active".

# use xinput watch-props $device_id to see some properties.

xinput set-int-prop $keyboard_id 121 8 $1;

done;

}

# you maybe don't want to exit in case of failure there.

do_it 0 ; sleep 5; do_it 1

这个逻辑很容易在Python中重写.如果安装xinput是有问题的,那么获取xinput的源并尝试使用像python-xlib这样的库在Python中重新实现它可能是个好主意.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值