Disable Keyboard & Mouse input on unix (under X)

23,796

Solution 1

Assuming your GUI is X-based (as almost all UNIX GUIs are), use xinput.

First, list your devices:

$ 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)]

List the details for your mouse (id=6 in our example):

$ 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

Now disable it:

$ export DISPLAY=:0
$ xinput set-int-prop 6 "Device Enabled" 8 0

To enable it do:

$ xinput set-int-prop 6 "Device Enabled" 8 1

The same goes for the keyboard, just replace the int-prop number with the proper id.
Tested and worked on cygwin.

Of course, you have to plan beforehand how will you enable your devices again. such as schedule it on cron, re-enable it remotely, or disable just one of them in first place.

Solution 2

xinput --set-int-prop is deprecated. You should use --set-prop instead. Also, xinput --enable [device] and xinput --disable [device] can be used to enable and disable devices respectively.

Here is a shell script I use to enable, disable, and toggle my laptop's touchpad:

#!/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

Solution 3

The answered question using xinput is the right one, but here is a quick one if all you are looking for is a simple screensaver type lock. I wrote this back in the '90s, and all it does is eat the X server's keyboard and mouse events, until you type the password. No feedback at all other than exiting when you type it correctly.

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

I use it as a screen-lock, exactly how you want to use it.

Solution 4

Your answer is probably best for your second use case (doing something remotely), but probably not for your first (being away from keyboard). How would you run xinput again to restore access when you return?

The standard solution to locking the system while away from it is XScreenSaver, which is installed by default in most distros. If configured to lock the keyboard, it will prompt for your password before unlocking it.

Solution 5

At least on Debian-based systems such as Ubuntu there is a utility called xtrlock (1) available through the package repositories.

This utility locks the keyboard and mouse until the password is entered while leaving windows visible. I find it useful for computers running information displays and the like.

Share:
23,796

Related videos on Youtube

Philomath
Author by

Philomath

No computer runs windows, it walks. --Philomath Using Cygwin now, (what a shame...). In process of converting to *nix.

Updated on September 18, 2022

Comments

  • Philomath
    Philomath over 1 year

    How can one programmatically "freeze" the Keyboard & Mouse temporarily, so that no one could mess with the system?

    There are several possibilities where this is useful. For instance, I have a laptop and I want to make sure no one uses it while I leave, even if somebody knows the password or can guess it (like wife or children), as well as depressing thieves' appetite (as it seems dis-functioning). or I'm doing something remotely so I want to make sure the user at the computer doesn't disturb.

  • Philomath
    Philomath almost 13 years
    I have updated.
  • Philomath
    Philomath almost 13 years
    I have updated.
  • Philomath
    Philomath almost 13 years
    No other answer here answers the question.
  • Emanuel Berg
    Emanuel Berg over 11 years
    In my case, the command to disable the mouse looks like this: xinput set-int-prop 9 "Device Enabled" 8 0 - only, 1) I'd like to hide the mouse pointer as well, and 2) if I go from X to a tty (Linux console/VTs), and then back to X, this is reset (xinput list-props 9 confirms this).
  • Emanuel Berg
    Emanuel Berg over 11 years
    Check out my comment to Philomath's post. I've got the same problem for xinput --disable 9.
  • Emanuel Berg
    Emanuel Berg over 11 years
    Check out my answer to a similar question on SO - it does the same, but can be invoked from a tty, and, it also (using a workaround) hides the pointer.
  • Sam Watkins
    Sam Watkins almost 11 years
    I voted for this as it is the kind of solution I want, but it doesn't work for me on Linux with shadow passwords. A process has to be root to access /etc/shadow and associated functions, but I want to run xl as a non-root user. My use case: when we watch shows on the computer, my baby daughter likes to bash on the keyboard! xscreensaver is not helpful for this
  • Sam Watkins
    Sam Watkins almost 11 years
    Your answer is a great example of "suckless" or unix programming "do one thing and do it well" ... except that it doesn't work for me (with shadow passwords!) For my use case I'm happy to hard-code a password, so that's okay. Now teach me how to disable the power button, that's my baby's favourite button!
  • Virendra
    Virendra almost 11 years
    why do you not want root? why not suid the xl binary?
  • Virendra
    Virendra about 10 years
    BTW, you can specify the password on the commandline or via a environment variable now.
  • Ez0r
    Ez0r over 9 years
    Is it possible to eat X input events with a standard X toolset and a shell script?
  • mehov
    mehov over 5 years
    This seems like the most simple and lightweight option, and it's been around since mid-90s too. One problem is I don't think it's possible to hide the locked cursor image, which makes this unusable for signage, unfortunately.
  • ed22
    ed22 over 4 years
    Can I disable a "master pointer" device?