Go Down

Topic: [Solved] Creating a generic HID keyboard using a Genuino Micro (Read 5176 times) previous topic - next topic

qwelyt

I have a project ( https://github.com/qwelyt/Keyboard ) where I'm trying to build my own keyboard from pretty much scratch.

I started to write my code on an Arduino UNO R3 SMD (much help fromhttp://mitchtech.net/arduino-usb-hid-keyboard/ )
I've found firmware that turns the UNO into a HID and all my code works as it should.

Using the UNO was great for prototyping. Now that I want to build the actual keyboard I wanted something smaller. I decided on the Arduino/Genuino Micro (https://store.arduino.cc/arduino-micro ) since it has USB capabilities and is small (dimension wise).

Now I ran into trouble. How do I actually port my code to the Micro?
I shouldn't have to flash a new firmware, should I? And if I have to, which?

I took a look at https://www.arduino.cc/en/Reference/MouseKeyboard but that library is... lets say "lacking the ability to be a real keyboard".

Basically what I want is to get my Micro to act as any generic keyboard you can buy. Not send specific letters, I want it to be a keyboard, so it should send key codes and let the computer decide what that means. Like I have it with the UNO right now.

That lib also have the problem with not being able to send USB datagrams (an uint8_t[8] basically). which allows you to send 6 keys at once + modifier ( "NKRO", which is really 6+modifiers-key-roll-over in USB ).

I looked at https://github.com/NicoHood/HID and couldn't really figure out what the project actually tries to do. It says it only supports US layouts, which ought to mean that it isn't sending key codes, but actually telling the computer what to output.

So. After all that rant (I admit I might be harsh atm, and a bit frustrated):
How can I send generic key codes with the Arduino Micro?

johnwasser

You can use raw USB keycodes by adding 136 to them.  That will give you the first 119 keycodes.  Did you need others?  
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

qwelyt

You can use raw USB keycodes by adding 136 to them.  That will give you the first 119 keycodes.  Did you need others? 
Yes. Since LCtrl to RGUI is E0 to E7 (or 224-231 in DEC).
I can see that the KeyboardMouse lib thinks different ( https://www.arduino.cc/en/Reference/KeyboardModifiers ). These comes as their own field though. So it *might* work with that.

I want to be able to use ALL key codes described in the USB HID Usage Tables.

johnwasser

The Keyboard library uses characters 128 through 135 to control the 8 modifier flags.

You don't have to use the Keyboard library.  You could use it as an example and write whatever USB reports you like.  Just take out this section in a few places and it will put the raw bytes into the USB report:
Code: [Select]
if (k >= 136) { // it's a non-printing key (not a modifier)
k = k - 136;
} else if (k >= 128) { // it's a modifier key
_keyReport.modifiers |= (1<<(k-128));
k = 0;
} else { // it's a printing key
k = pgm_read_byte(_asciimap + k);
if (!k) {
setWriteError();
return 0;
}
if (k & 0x80) { // it's a capital letter or other character reached with shift
_keyReport.modifiers |= 0x02; // the left shift modifier
k &= 0x7F;
}
}


You may want to put in recognition of codes 0xE0 to 0xE7 to set the modifier flags.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

qwelyt

I had a look at http://www.konstantin-schauwecker.de/typewriter.php as that project has a working keyboard setup. It's not exactly like mine, but it does use a Leonardo (close enough to a Micro) and does not use the Keyboard library.

I had a look at his "Keyboard.cpp" file (download the sources if you want to follow along) which does the HID setup and sends the key buffer.

After making some modifications and inspiration, I now have a working keyboard based on an Arduino Micro. Neato!

https://github.com/qwelyt/Keyboard/tree/master/Code/ModuleA/V2/ModuleA <-- For my sauce.

Go Up