7 years, 11 months ago.

Using an USB Keyboard

I just got a Raspberry Pi...I am looking to use the new Kl25z board to interface an SNES controller to the Pi through the USB port (as an USB Keyboard). I have experience with microcontrollers and interfacing to SNES controllers...however, I am a Noob with USB interfacing. My thought was to map buttons from the gamepad to keyboard keys...however, with gamepads it is most likely two or more buttons/directions will be pushed at the same time...So my questions is...can the USB keyboard send more than one key code at a time and how do you send more than one key code?

2 Answers

7 years, 11 months ago.

I've been working on this on and off for the past two weeks or so. I was having the same issue and found that using mbeds USBKeyboard library only sends literal buttons pushes (as in push & release) so if you were to hold a SNES button down, it repeatedly sends key presses. I found out that creating a HID_REPORT object you can store up to 6 concurrent button presses, so i made shared snes keys (ie: left and right on the dpad share report.data[3]). For example:

USBKeyboard keyboard;
HID_Report report;

report.data[0]; // this is the USB ID
report.data[1]; // this is a modifier key (ie: ctrl/shift/alt)
report.data[2]; // i haven't figured this one out
report.data[3 to 8]; // these are the elements i've been using to send keyboard button presses to the pc
report.length = 9; // i honestly dont know much about usb packets......

keyboard.send(&report);

as long as you have a keyboard key codes stored in elements 3-8 when you send it, that button will stay pressed until you send a new report with 0 in the corresponding element

sam white
poster
7 years, 11 months ago.

Chris,

Thank you for the info…so if I mapped the left gamepad to “s” and the right gamepad to “d”

And the gamepad is held ‘left’ the code could be

Report.data[3] = ‘s’;

Until released then the code is

Report.data[3] = 0;

Or 'right'

Report.data[3] = ‘d’;

I used http://www.mindrunway.ru/IgorPlHex/USBKeyScan.pdf to set the codes in the report.data fields. It seems that USB keyboards don't use ASCII. So instead of 'right' as 'd': report.data[3] = 0x07; Similarily, as far as concurrent button presses goes, I used 'up' or 'down' in report.data[4].

I also forgot to mention that I set report.data[0] = 1; and left it that way. I tried changing it and setting it to different numbers but did not seem to work. I thought it made sense to leave it as 1 since it is an identifier.

posted by Chris Thomas 04 Apr 2013