Requirement: I have a custom website which is displayed on a big monitor using a Raspberry Pi. The website has a couple of buttons to navigate around with and I have added JavaScript keyboard shortcuts to them so that with simple keyboard clicks a user can navigate the site.

Question: All I would like to do now is to have 4 push buttons and assign each of the buttons with a keyboard shortcut. So that clicking on each of them will activate something on the website based on a pre-configured shortcut.

So is it possible to assign keyboard shortcuts to a push button connected to the GPIO and make it behave like keyboard?

  • Not saying this is necessarily the case but it sounds like it most likely is: Would it not be simpler to have the buttons hooked up to activate something on the website, rather than having them emulate something from the keyboard which would do the same thing?
    – goldilocks
    May 10, 2016 at 13:40
  • There are html buttons already. But due to the specific setup requirement we dont want to provide a full keyboard and mouse access to end user. Only 4 push buttons is what they get, Yes i can break a keyboard and create a DIY setup but was thinking if i can use 4 push buttons to get it done.
    – KD.
    May 10, 2016 at 13:43
  • You misunderstand me. What I am saying is using the buttons to emulated a keyboard is more complicated than just using the buttons to do something directly the same way the html buttons do via js -> server. I presume whatever that is, your server side code is not emulating a keyboard in order to change whatever on the website. The same is true for the buttons. Put another way, if this is the case your question is really, "How can I use buttons connected to the GPIOs to execute a command?". You seem to believe that command should be something that involves keyboard emulation. Why?
    – goldilocks
    May 10, 2016 at 13:48
  • Hmm ya could have been option if this was doing a webservice call or something on button click. This page is a very simple HTML page with maps and static content. Kind of very static. On click of button this will need to just navigate to various section on the same page. I am open for suggestion but the keyboard shortcut was something which seems would work on first thought. I really appreciate you spending time and trying to help me. :)
    – KD.
    May 10, 2016 at 13:53
  • 1
    You should look into pikeyd; it's a project to implement an RPi GPIO-to-keyboard daemon: github.com/mmoller2k/pikeyd
    – apraetor
    May 10, 2016 at 14:37

3 Answers 3


By far the easiest way to do this is to use a bluetooth dongle for the Pi (or use a Pi3), and one of these: https://www.adafruit.com/products/1535.

No fuss, no muss, no worrying about long cables introducing noise or static hazards, no coding to do.


All you need to do is to emulate a USB HID device with a couple push buttons. There are many examples of these, mainly in the arcade emulation field.

They are called "fightsticks" and this is a sample available from Amazon. I'm not suggesting that you get one, merely using them as a proof of concept.

Another product is from Adafruit and is a small Arduino compatible that can be programmed to act as a keyboard. And yes, you can have a keyboard with only two keys if you want. Adafruit almost certainly has sample code for this purpose. Adafruit also has a variety of switches that can be used if you don't already have any.


For completeness, the Linux kernel has built-in support for GPIO keys and GPIO matrix keypads.

These drivers allow you to hook up buttons to arbitrary GPIO lines (the simpler GPIO keys driver need them to be able to fire an interrupt, but there's also a polled variant) and assign a keycode to them. You can either choose the keycode of a known key or use your own keycodes, then map them to the required keysym, e.g. with Xmodmap. The latter makes it possible to assign different functions to your new key when shift/control/alt are pressed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.