Sending keypresses to remote X session over SSH

18,453

Solution 1

Using x2x and some X11 forwarding, I got this to work.

ssh -X htpc x2x -to :0

The only downside currently is I had to bum into a TTY to kill SSH to exit it. Still looking up how to quit when I'm done! Otherwise, it's quite a nice and simple (and shortcuttable) method for transferring input from one computer to another.

Edit: adding -nomouse gave me the ability to close the forwarded x2x window and I can unfocus it to quickly go back to other things without needing to shut the connection down.

Solution 2

You should be able to do that using the xdotool command, after specifying the appropriate X display e.g. to simulate a single 'down arrow' press in the currently active window

$ DISPLAY=:0 xdotool getactivewindow key Down

For entering literal text, you can use xdotool type <string> e.g.

$ DISPLAY=:0 xdotool getactivewindow type 'your text here'

The xdotool package should be available from the Universe repository.

Share:
18,453

Related videos on Youtube

Oli
Author by

Oli

Hi, I'm Oli and I'm a "full-stack" web-dev-op. Eurgh. I'm also allergic to jargon BS. I spend most of my professional time writing Django websites and webapps for SMEs. I write a lot of Python outside of Django sites too. I administer various Linux servers for various tasks. I contribute to the open source projects that I use when I can. I'm a full-time Linux user and that has lead to helping other people live the dream. I am an official Ubuntu Member and I earnt my ♦ on SE's own Ask Ubuntu in 2011's moderator election. That's probably where I spend most of my unpaid time. I also run thepcspy.com which has been my place to write for the last decade or so. If you need to contact me for extended help, you can do so via my website, just remember that I have bills so if I feel your request is above and beyond normal duty, I might ask for remuneration for one-on-one support. For more social contact, you can usually find me (or just my computer) lurking in the Ask Ubuntu General Chat Room and on Freenode in #ubuntu and #ubuntu-uk under the handle Oli or Oli``.

Updated on September 18, 2022

Comments

  • Oli
    Oli almost 2 years

    So I'm sitting in our living room on an Ubuntu laptop working while also watching a TV that is controlled by an Ubuntu HTPC. I have an IR remote set up and can control everything I need to with it, 99% of the time.

    However text input on a remote isn't very easy. I don't need to do it very often but when I do, it feels easier to walk 15 feet to plug in a USB keyboard and do it that way. However... I'm criminally and medically lazy and feel walking any distance isn't just unnecessary, it shows the robots they have won. No more I say!

    I can SSH into the HTPC fine... Now, how do I send keypresses to that remote computer's X session?

    I should mention that I know I could VNC but the TV is 1080p and the laptop is 720p so this introduces some scaling issues. On a technical level, I'd rather a solution that gave me a way to temporarily turn the laptop into a remote keyboard.

    • Admin
      Admin over 10 years
      I'm even lazier than you. Instead of configuring a IR remote (I even bought one!), I additionally bought a trust wireless keyboard.
  • Oli
    Oli over 10 years
    I don't deny it works but it's a bit miserable if you're trying to type in a network location one key at a time.
  • Philipp Ludwig
    Philipp Ludwig almost 7 years
    -nomouse doesn't seem to work anymore; for me under ubuntu 16.04 x2x only prints the usage info, no matter where I put that flag.