Remote keyboard and mouse for your RaspberryPi

Warning – technical post ahead…

My shiny new RaspberryPi came through the door this week, and the first thing I noticed was that it was the first computer I’d ever received where the postman didn’t need to ring the bell to deliver it.

The next thing I discovered, because it came sooner than expected, was that I was missing some of the key bits needed to play with it. A power supply was no problem – I have a selection of those from old phones and things, and I found a USB-to-micro-USB cable. Nor was a network connection tricky – I have about as many ethernet switches as I have rooms in the house.

A monitor was a bit more challenging – I have lots of them about, but not with HDMI inputs, and I didn’t have an HDMI-DVI adaptor. But then I remembered that I do have an HDMI monitor – a little 7″ Lilliput – which has proved to be handy on all sorts of occasions.

The next hiccup was an SD card. You need a 2GB or larger one on which to load an image of the standard Debian operating system. I had one of those, or at least I thought I did. But it turned out that my no-name generic 2GB card was in fact something like 1.999GB, so the image didn’t quite fit. And a truncated filesystem is not the best place to start. But once replaced with a Kingston one – thanks, Richard – all was well.

Now, what about a keyboard and mouse?

Well, a keyboard wasn’t a problem, but it didn’t seem to like my mouse. Actually, I think the combined power consumption probably just exceeded the capabilities of my old Blackberry power supply, which only delivers 0.5A.

If you’re just doing text-console stuff, then this isn’t an issue, because it’s easy to log into it over the network from another machine. It prints its IP address just above the login prompt, so you can connect to it using SSH, if you’re on a real computer, or using PuTTY if you’re on Windows.

But suppose you’d like to play with the graphical interface, even though you don’t have a spare keyboard and mouse handy?

Well, fortunately, the Pi uses X Windows, and one of the cunning things about X is that it’s a networked display system, so you can run programs on one machine and display them on another. You can also send keyboard and mouse events from one machine to another, so you can get your desktop machine to send mouse movements and key presses from there. On another Linux box, you can run x2x. On a Mac, there’s Digital Flapjack’s osx2x (If that link is dead, see the note at the end of the post).

These both have the effect of allowing you to move your mouse pointer off the side of the screen and onto your RaspberryPi. If you have a Windows machine, I don’t think there’s a direct equivalent. (Anyone?) So you may need to set up something like Synergy, which should also work fine, but is a different procedure from that listed below. The following requires you to make some changes to the configurations on your RaspberryPi, but not to install any new software on it.

Now, obviously, allowing other machines to interfere with your display over the network is something you normally don’t want to happen! So most machines running X have various permission controls in place, and the RaspberryPi is no exception. I’m assuming here that you’re on a network behind a firewall/router and you can be a bit more relaxed about this for the purposes of experimentation, so we’re going to turn most of them off.

Running startx

Firstly, when you log in to the Pi, you’re normally at the command prompt, and you fire up the graphical environment by typing startx. Only a user logged in at the console is allowed to do that. If you’d like to be able to start it up when you’re logged in through an ssh connection, you need to edit the file /etc/X11/Xwrapper.config, e.g. using:

sudo nano /etc/X11/Xwrapper.config

and change the line that says:

allowed_users=console

to say:

 allowed_users=anybody

Then you can type startx when you’re logged in remotely. Or startx &, if you’d like it to run in the background and give you your console back.

Allowing network connections

Secondly, the Pi isn’t normally listening for X events coming over the network, just from the local machine. You can fix that by editing /etc/X11/xinit/xserverrc and finding the line that says:

exec /usr/bin/X -nolisten tcp "$@"

Take out the ‘-nolisten tcp’ bit, so it says

exec /usr/bin/X "$@" 

Now it’s a networked display system.

Choosing who’s allowed to connect

There are various complicated, sophisticated and secure ways of enabling only very specific users or machines to connect. If you want to use those, then you need to go away and read about xauth.

I’m going to assume that on your home network you’re happy for anyone who can contact your Pi to send it stuff, so we’ll use the simplest case where we allow everything. You need to run the ‘xhost +‘ command, and you need to do it from within the X environment once it has started up.

The easiest way to do this is to create a script which is run as part of the X startup process: create a new file called, say:

/etc/X11/Xsession.d/80allow_all_connections

It only needs to contain one line:

/usr/bin/xhost +

Now, when the graphical environment starts up, it will allow X connections across the network from any machine behind your firewall. This lets other computers send keyboard and mouse events, but also do much more. The clock in the photo above, for example, is displayed on my Pi, but actually running on my Mac… however, that’s a different story.

For now, you need to configure x2x, osx2x, or whatever is sending the events to send them to ip_address:0, where ip_address is the address of your Pi. I’m using osx2x, and so I create the connection using the following:

Once that’s done, I can just move the mouse off the west (left-hand) side of my screen and it starts moving the pointer on the RaspberryPi. Keyboard events follow the pointer. (I had to click the mouse once initially to get the connection to wake up.)

Very handy, and it saves on desk space, too!

Update: Michael’s no longer actively maintaining binaries for osx2x, and the older ones you find around may not work on recent versions of OS X. So I’ve compiled a binary for Lion(10.7) and later, which you can find in a ZIP file here. Michael’s source code is on Github, and my fork, from which this is built, is here.

Enjoyed this post? Why not sign up to receive Status-Q in your inbox?

15 Comments

many thanks for this useful info. just my luck, been waiting months for it to come and my usb keyboard does not work. the shops are closed until wednesday due to our lovely queen of the uk.

Note that the small Apple keyboard (wired) doesn’t work if a mouse is plugged into its USB port. Both work fine when plugged into the 2 RasPi USB ports.

I second the thanks Q. All that X config stuff can make the head hurt when you have to work it out!

We were all caught unawares by the sooner than expected delivery. The SD card reader/writer supplied with the GEO Duet has its uses after all!

Quentin,
Thanks for bringing x2x to the fore. It looks like it would be useful – but I have to admit that I am struggling to build it for cygwin let alone my pi. If you get a chance could I please encourage you to either add a link to the answer or write down the build steps needed for x2x on Pi.
All the best,

James

Hi James –

For what I’m describing here, you don’t need to build x2x on the Pi. It’s a program that you’d run on another machine that’s already running X Windows – which is almost certainly another Linux/Unix box – to allow it to send keyboard and mouse movement to your Pi. The Pi (and any other X-based machine) already has the ability to receive these events, as described above.

This also means that there aren’t many situations, I’d imagine, where you’d want to run it under Cygwin – most Windows machines aren’t running X Windows. If you’ve got a Windows machine, you might do better looking at Synergy.

Now, you might be wanting to do something different: there’s no reason you shouldn’t run x2x on your Pi to allow its mouse and keyboard to control another Linux machine, for example, or even another Pi, and yes, for that you would need to build x2x on the Pi. I may have a go at that when I get a chance…

Is this still working? I’ve installed osx2x and followed your configuration on the Pi. I had to additionally install x11-xserver-utils to get the xhost utility on the Pi.

When I attempt to connect from my MacBook Pro (lion) to the Pi, I get a “Failed to connect to server.” message. What I don’t understand is that I can ssh and forward the X in the normal fashion from the terminal. Any ideas?

Hi,

This worked for me. I needed to compile osx2x with xcode (had to add a headers path)

On the pi I
made the change above in /etc/X11/xinit/xserverrc to remove the -nolisten tcp
ran startx
ran export DISPLAY=:0.0
ran xhost +

On my mac I used the host name ‘:0.0’

That export step seemed critical

The links to osx2x is not working and the ones i have found by searching don’t work on lion.
Anyone have a solution?

I’ve updated the post – there’s a link at the bottom to a version of OSX2X I’ve just built, which does work on Lion.

Hi!

Thanks for this post! I am just getting into learning about the Raspberry Pi and currently using the Openelec (XBMC) os version Frodo. What files would I need to edit? What would I need to enable from the Openelec/RPi side? Thank you. [BTW, I do have your 0sx20 app and it runs yet gives me the connection to host error — since I need to make the mods you suggest in this post (which are for Debian os).]

    Hi Luis –

    I’m not sure if this is easy or even possible with OpenElec – I’m afraid I don’t have any experience of using it. One thing to mention is that my notes talk about sending X Windows keyboard and mouse events to the Pi, but I think OSX2X can, as an alternative, send VNC events.

    So you either need to do something similar to the above to allow your Pi to receive X events, or you need to run a VNC server on it. All I can suggest is that you search for things like:

    “Enable remote X access on OpenELEC”

    or

    “openelec x11vnc”

    or similar! Let us know if you get it going.

    Q

Thank you very much for the fast response qsf, it’s appreciated! Yes, I have been doing some googling around yet not coming across much. I’ll will keep digging through and seeing what I can do. Once I have something I’ll repost just incase others might have an interest. By the way, I do have terminal and X11 on my Mac, plus, Xcode, JellyFish and osx2x — playing around with such to see what I can do. I also have VNC yet have not been able to configure properly. I did see the response from Jon (dated 12/17/2012) above and checking to see what I can do with those inputs as well.

My key issue is being able to figure out which files Openelec uses to create the similar features that you describe in your post.

Again, thank you!

Luis

qsf (et all),

I was able to find a couple of posts at the Openelec forum. There are add-ons for Openelec, yet, not for Openelec/XBMC running on Pi. Here are a couple of posts which you can track to see if things change over time. You’ll have to read through the complete posts to get the overall picture of the development and add ons.

http://openelec.tv/forum/13-miscellaneou…te-desktop

http://openelec.tv/forum/128-addons/3477…4?start=24

I’ll keep digging!

PRKid

OS X 10.9 won’t even let me open OSX2X?

[…] la maquina, he puesto un scritp en “/etc/X11/Xsession.d/80allow_all_connections” (visto aqui) y en ese script ponemos: /usr/bin/xhost + Yo esto lo uso en local, pero para más seguridad ver […]

[…] via Remote keyboard and mouse for your RaspberryPi | Status-Q. […]

Got Something To Say:

Your email address will not be published. Required fields are marked *

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

*

© Copyright Quentin Stafford-Fraser