[ubuntu-uk] Lock keyboard/mouse without locking screen?

Lambros Lambrou lambros at lambrou.net
Thu Jun 26 20:01:26 BST 2008

Interesting question!  Offhand, I don't know of any tools that can do
this (though I didn't look very far).  So I knocked together a tiny
little program that might be useful to you.  I'm afraid it's written in
C, so if you're new to Ubuntu and GNU/Linux, compiling and running it
may be a little bit of a challenge, sorry about that! :)  If anyone has
any better ideas, I'd be interested to know (maybe port it to Python?).
I've put the listing at the end, between the '//////' markers, in case
this mailing-list doesn't like attachments.

Brief instructions:
Save the listing to a file named lockdisplay.c in your Home directory.
Install the needed development tools:
    sudo apt-get install build-essential libx11-dev
Compile the program:
    gcc -lX11 lockdisplay.c -o lockdisplay
Run it with:

When it's running, it will lock the keyboard and mouse, but programs
(such as a video player) will continue to run on the screen.  Type in
the password "ubuntu" to stop the program and release the lock.

Unfortunately, it won't trap any special X server key-sequences (such as
Ctrl-Alt-F2 or Ctrl-Alt-Backspace).  I don't know how to trap these, but
I think it must be possible, because VMWare Server manages to do it.

You can do whatever you want with this code - it works for me, but
please don't hold me responsible for anything that breaks! :)

Lambros Lambrou


// Compile this with
//   gcc -lX11 lockdisplay.c -o lockdisplay

#include <X11/Xlib.h>
#include <stdio.h>

int main()
    Display *dpy;
    XEvent xevent;
    Window w;
    const char *password = "ubuntu";
    int correct_keys = 0;
    dpy = XOpenDisplay(NULL);
    if (!dpy) {
        fprintf(stderr, "Couldn't open X display %s\n",
        return 1;
    // Lock keyboard and mouse
    w = RootWindow(dpy, DefaultScreen(dpy));
    XGrabKeyboard(dpy, w, False,
        GrabModeAsync, GrabModeAsync, CurrentTime);
    XGrabPointer(dpy, w, False, 0,
        GrabModeAsync, GrabModeAsync,
        None, None, CurrentTime);

    // X Event-handling loop.
    // Look at keypress events and compare them with the password,
    // one letter at a time.
    while (1) {
        XNextEvent(dpy, &xevent);
        if (xevent.type == KeyPress) {
            XKeyEvent *ke = (XKeyEvent *)&xevent;
            KeySym keysym;
            XLookupString(ke, NULL, 0, &keysym, NULL);
            //printf("Got keysym %d, '%c'\n", keysym, keysym);
            if (keysym == password[correct_keys]) {
                //printf("Correct key pressed\n");
                if (password[correct_keys] == '\0') {
                    //printf("Password fully typed, exiting.\n");
            } else {
                correct_keys = 0;
                //printf("Wrong key, reset to beginning\n");
    return 0;


On Wed, 2008-06-25 at 17:24 +0100, ubuntu-uk-request at lists.ubuntu.com
> Message: 3
> Date: Wed, 25 Jun 2008 16:38:43 +0100
> From: Andrew Oakley <andrew at aoakley.com>
> Subject: [ubuntu-uk] Lock keyboard/mouse without locking screen?
> To: British Ubuntu Talk <ubuntu-uk at lists.ubuntu.com>
> Message-ID: <48626683.3020700 at aoakley.com>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
> I have a two-year old daughter and an Ubuntu laptop (Hardy on a Dell 
> Inspiron 1520).
> How do I lock the keyboard and mouse without locking the screen? I 
> realise this may require installing new packages.
> For example, I want to be able to play a video on the laptop, whilst
> my 
> daughter to bashes the keys and clicks the mouse. Ideally the 
> keyboard/mouse would only be unlocked once a particularly difficult
> key 
> combination or sequence is pressed.
> I don't intend to leave my daughter alone with the laptop (result
> would 
> probably be Ribena spilt over it), but I would like to be able to sit
> on 
> the other side of the room and not have to constantly pull her away
> from it.
> -- 
> Andrew Oakley

More information about the ubuntu-uk mailing list