在 unix 上禁用键盘和鼠标输入(在 X 下)

Phi*_*ath 28 xorg keyboard input mouse

如何以编程方式暂时“冻结”键盘和鼠标,以便没有人干扰系统?

有几种可能是有用的。例如,我有一台笔记本电脑,我想确保在我离开时没有人使用它,即使有人知道密码或可以猜到它(例如妻子或孩子),以及压抑小偷的胃口(因为它似乎不- 运作)。或者我正在远程执行某些操作,因此我想确保计算机上的用户不会打扰。

Phi*_*ath 26

假设您的 GUI 是基于 X 的(几乎所有 UNIX GUI 都是如此),请使用xinput.

首先,列出您的设备:

$ xinput --list
? Virtual core pointer                          id=2    [master pointer  (3)]
?   ? Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
?   ? Windows mouse                             id=6    [slave  pointer  (2)]
? Virtual core keyboard                         id=3    [master keyboard (2)]
? Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
? Windows keyboard                          id=7    [slave  keyboard (3)]
Run Code Online (Sandbox Code Playgroud)

列出鼠标的详细信息(在我们的示例中为 id=6):

$ xinput --list-props 6
Device 'Windows mouse':
    Device Enabled (112):   1
    Coordinate Transformation Matrix (114): 1.000000, 0.000000, 0.000000, 0.000000,   1.000000, 0.000000, 0.000000, 0.000000, 1.000000
    Device Accel Profile (222):     0
    Device Accel Constant Deceleration (223):       1.000000
    Device Accel Adaptive Deceleration (224):       1.000000
    Device Accel Velocity Scaling (225):    10.000000
Run Code Online (Sandbox Code Playgroud)

现在禁用它:

$ export DISPLAY=:0
$ xinput set-int-prop 6 "Device Enabled" 8 0
Run Code Online (Sandbox Code Playgroud)

要启用它,请执行以下操作:

$ xinput set-int-prop 6 "Device Enabled" 8 1
Run Code Online (Sandbox Code Playgroud)

键盘也是如此,只需将 int-prop 编号替换为正确的 id。
在 cygwin 上进行了测试和工作。

当然,您必须事先计划如何再次启用您的设备。例如在 cron 上安排它,远程重新启用它,或者首先禁用其中一个。


小智 15

xinput --set-int-prop已弃用。你应该--set-prop改用。此外,xinput --enable [device]xinput --disable [device]可分别用于启用和禁用设备。

这是我用来启用、禁用和切换笔记本电脑触摸板的 shell 脚本:

#!/bin/bash
# Enables, disables, or toggles device

device='AlpsPS/2 ALPS GlidePoint'
if [[ $1 == -e ]] ; then
    # Enable
    #xinput --set-prop "$device" "Device Enabled" 1
    xinput --enable "$device"
elif [[ $1 == -d ]] ; then
    # Disable
    #xinput --set-prop "$device" "Device Enabled" 0
    xinput --disable "$device"
elif [[ $1 == -t ]] ; then
    # Toggle
    if [[ $(xinput list-props "$device" |
       grep "Device Enabled") == *:*1 ]] ; then
           #xinput --set-prop "$device" "Device Enabled" 0
           xinput --disable "$device"
    else
        #xinput --set-prop "$device" "Device Enabled" 1
        xinput --enable "$device"
    fi
else
    echo "usage: $0 [-edt]"
fi
Run Code Online (Sandbox Code Playgroud)


Dan*_*lai 6

使用 xinput 回答的问题是正确的,但如果您正在寻找一个简单的屏幕保护程序类型的锁,这里有一个快速的问题。我在 90 年代写了这个,它所做的就是吃掉 X 服务器的键盘和鼠标事件,直到你输入密码。除了在正确键入时退出之外,没有任何反馈。

http://ishiboo.com/~danny/Projects/xl/

我将它用作屏幕锁定,这正是您想要使用它的方式。