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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
Method 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
Method 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.
Method 4
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.
Method 5
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.
Method 6
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.
Method 7
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.
Method 8
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 }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0