Acech
Posts: 2
Joined: Thu Jan 31, 2019 8:17 am

Using a USB keyboard as a controller

Thu Jan 31, 2019 8:24 am

Good morning all,

I have a raspberry pi controlling RF sockets in order to turn specific lights on and off etc. I also have a USB keyboard which I would like to use as the interface, so as you press a specific key a script gets run turning on different light configurations.

I am struggling to find any links to how this might have been done before. I guess there are two things that need to happen - firstly the keyboard needs to be disabled - it is connected to a headless setup, so I wouldn't want the keyboard typing in the login box that would be open on the pi.

Secondly I would need some way to buffer the keyboard and throw an interupt when a key is pressed.

Are these things possible? And if so could anyone offer any advice as to how to do it?

Many thanks,

Chris

mattmiller
Posts: 2079
Joined: Thu Feb 05, 2015 11:25 pm

Re: Using a USB keyboard as a controller

Thu Jan 31, 2019 9:00 am

Have I got this right?

You want your program to receive different signals when different key are pressed on your keyboard but you don't want the keyboard to work as a normal keyboard?

Acech
Posts: 2
Joined: Thu Jan 31, 2019 8:17 am

Re: Using a USB keyboard as a controller

Thu Jan 31, 2019 10:24 am

Yes! - Sorry for being ambiguious!

I want to be able to use the keyboard on a headless set up, so as the pi boots it starts waiting for key presses. If a key is pressed a script will run.

tvjon
Posts: 706
Joined: Mon Jan 07, 2013 9:11 am

Re: Using a USB keyboard as a controller

Thu Jan 31, 2019 11:27 am

Acech wrote:
Thu Jan 31, 2019 8:24 am
...Are these things possible?...
Many thanks,

Chris
Yes.

Some of my RPi applications use up almost all of the GPIO's switched to an alternative dedicated function, so I sometimes have to use a different method of input, the simplest (& cheapest) being to use a USB (or even PS/2, as they are often better quality) keyboard. You can even have outputs as well for feedback, in the form of a rudimentary code via the typical 3 keyboard leds.

One such application is to practice playing simple tunes using midi but via a qwerty (azerty etc.) dedicated keyboard. Obviously there's no velocity possible, just note on & off, but it's fine for when impractical to carry a bulky midi controller.

You'd still have to decode the on/off midi sequences & decide if such is feasible for your particular devices.

I always do this whilst having monitor(s) & keyboard connected to the RPi, but headless shouldn't be a problem.

Are you familiar with building applications from source code?


"Secondly I would need some way to buffer the keyboard and throw an interupt when a key is pressed."

I didn't think your post ambiguous, but can you elaborate on your thinking there, bearing in mind you haven't given any details of the rf devices?

User avatar
topguy
Posts: 5612
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Using a USB keyboard as a controller

Thu Jan 31, 2019 12:20 pm

I would suggest alternative solution: ( much easier to implement IMO compared to what you suggest)
- Autologin that automatically runs a script that listens for keypresses and runs the commands you want.
A special keypress exits that script and gives you normal shell.

Return to “General programming discussion”