It's one of the many defined USB device classes. Every USB device is assigned a class, which defines what its
general purpose is. There are loads of classes -- printers, hubs, speakers, and webcams to mention a few, but in
this example we'll be emulating HID --
Human Interface Device
. The ATmega32U4 takes care of the USB-
hardware hurdle, but we've still got to clear the firmware one. Time for some example code! We broke up the HID
example into two parts.
Example 2a: USB Keyboard Made Simple
Example 2b: USB Mouse Functionality
Example 2a: USB Keyboards Made Simple
To emulate a USB keyboard, we'll be making use of the
Keyboard
class. Here are some of the functions made
available to use by this class:
Keyboard.write(char)
- This function will send a single character over USB. The character passed can be
any standard, printable, ASCII-defined character: 0-9, a-z, A-Z, space, symbols, etc. Here's an example line
of code:
Keyboard.write('z') // This will send a single 'z' character to your computer.
Keyboard.print(string)
- If you need to perform a series a
Keyboard.write()
's, consider using just a
single
Keyboard.print()
. This works similar to
Serial.print()
– give it a string of characters and it'll
send that stream of characters over USB.
Keyboard.println(string)
is also defined, if you want a
newline/linefeed to close out your string. An example:
Keyboard.print("Hello, world"); // This'll send your computer an 'H', followed by 'e', fol
lowed by...
Keyboard.press(byte)
and
Keyboard.release(byte)
give you more precise control over key presses.
They do exactly what you'd expect. One presses a button down, the other releases a button. Make sure you
release any buttons you press, otherwise you'll encounter some wiggyness on your computer.
That's it. You don't need to include any libraries or anything, just invoke any of those functions. Here's an
example
sketch
to try it out: