How To Lock The Keyboard And Mouse, But Not The Screen In Linux

Lock The Keyboard And Mouse

My 4-year-old niece is a curious-kid. She likes very much to watch toy story movie. But the problem is she often touches a key in the keyboard or click the mouse button when watching a movie. Sometimes, she accidentally close or pause the movie by pressing a key in the keyboard. So, I had to find a way to lock down both the keyboard and mouse, but not the screen. When searching for a solution, I came across a perfect solution in Ubuntu forum. So, if you don’t want your cat or puppy walking on your keyboard or your kid messing with the keyboard and mouse while you watching something on the screen, you need to try “xtrlock”. It is a simple, yet useful utility to lock the X display till the user enters their password at the keyboard. In this brief tutorial, I will show you how to lock the keyboard and mouse, but not the screen in Linux. This trick will work on all Linux operating systems.

Install xtrlock

xtrlock is available in the default repositories of most Linux operating systems. So, you can install it using your distribution’s package manager.

On Arch Linux and derivatives, run the following command to install it.

sudo pacman -S xtrlock

On Fedora:

sudo dnf install xtrlock

On RHEL, CentOS:

sudo yum install xtrlock


sudo zypper install xtrlock

On Debian, Ubuntu, Linux Mint:

sudo apt-get install xtrlock

Lock the Keyboard and Mouse, but not the Screen using xtrlock

Once xtrlock installed, create a keyboard shortcut. You need this to lock the keyboard and mouse using the key combination of your choice.

Create a new file called lockkbmouse in /usr/local/bin.

sudo vi /usr/local/bin/lockkbmouse

Add the following lines into it.

sleep 1 && xtrlock

Save the file and close the file.

Make it as executable using the following command:

sudo chmod a+x /usr/local/bin/lockkbmouse

Open Keyboard Shortcuts. In MATE desktop, it will be available under System -> Preferences -> Hardware -> keyboard Settings. In Ubuntu (Unity DE), go to System Settings -> Keyboard -> Shortcuts.

Click Add to create a new shortcut.

Enter the name for your shortcut and add the following line in the command box, and click Apply button.

bash -c "sleep 1 && xtrlock"

To assign the shortcut key, just select or double click on it and type the key combination of your choice. For example, I use Alt+k.

To clear the key combination, press BACKSPACE key. Now, close the Keyboard Settings window.

Once you press keyboard shortcut, the mouse pointer becomes a padlock. Now, the keyboard and mouse have been locked, so you can freely watch the movies. Even your kid or pet touches some key on the keyboard or clicks a mouse button, they won’t work.

To unlock the keyboard and mouse, simply type your password and hit “Enter”. You will not the password while you type it. Just type it anyway and hit ENTER key. The mouse and keyboard will start to work after you entered the correct password. If you entered an incorrect password, you will hear a bell sound. Press ESC key to clear the incorrect password and enter the correct password again. To remove one character of a partially typed password, press either BACKSPACE or DELETE keys.

For more details, refer man pages.

man xtrlock

And, that’s all for now. Hope this helps. If you find our guides useful, please spend a moment to share them social, professional networks and support OSTechNix.


Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...

  • César

    Thank you very much for this post SK. It is very useful.
    Only one suggestion. I think it is not necesary to write the command again in the custom shortcut command box.
    You have already created an executable file with the command inside, so if you select that file in the custom shortcut box (resulting in “/usr/local/bin/lockkbmouse”), it will work as well.