28

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.

23

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.

  • No other answer here answers the question. – Philomath Jul 28 '11 at 22:48
  • 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 Dec 30 '12 at 22:11
  • Can I disable a "master pointer" device? – ed22 Aug 21 at 20:56
14

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
  • Check out my comment to Philomath's post. I've got the same problem for xinput --disable 9. – Emanuel Berg Dec 30 '12 at 22:13
6

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.

  • 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 May 16 '13 at 4:35
  • 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! – Sam Watkins May 16 '13 at 4:41
  • why do you not want root? why not suid the xl binary? – Danny Dulai Jun 3 '13 at 15:20
  • BTW, you can specify the password on the commandline or via a environment variable now. – Danny Dulai Feb 19 '14 at 23:16
  • Is it possible to eat X input events with a standard X toolset and a shell script? – Alexander Shcheblikin Sep 29 '14 at 22:38
2

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.

  • I have updated. – Philomath Jul 21 '11 at 23:53
2

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.

  • 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. – aexl Nov 13 '18 at 17:27
1

Depending on your hardware, you can remove the modules, controlling your hardware. I have such a script, touchpadtoggle, to enable and disable my touchpad.

lsmod | grep -q psmouse && rmmod psmouse || modprobe psmouse

But the keyboard doesn't seem to have a module associated, and the psmouse-module will only work occasionally.

1

If you are using a Desktop Environment or login manager, (GNOME,KDE,XFCE,LXDE) almost all of them have a lock screen function where you have to type in your password to get back to your programs.

However since that is really simple I sense your problem is more complex / different.

  • I have updated. – Philomath Jul 21 '11 at 23:53
1

I wrote this (in .zshrc, but should work in .bashrc as well) to do this, with the help of answers above. To do the corresponding with the keyboard, change the parameter in grep Mouse.

setmouse () {
 xinput \
  $1 \
  `xinput | grep Mouse | tr -d " " | tr "\t" " " | cut -d" " -f2 | cut -d"=" -f2`
}
offmouse () { setmouse disable }
onmouse  () { setmouse enable  }
  • 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. – Emanuel Berg Jan 24 '13 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.