• 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:
int buttonPin=9;//Setabuttontoanypin
void setup()
{
pinMode(buttonPin,INPUT);//Setthebuttonasaninput
digitalWrite(buttonPin,HIGH);//Pullthebuttonhigh
}
void loop()
{
if (digitalRead(buttonPin)== 0)//ifthebuttongoeslow
{
Keyboard.write('z');//senda'z'tothecomputerviaKe
yboardHID
delay(1000);//delaysothereare
n'takajillionz's
}
}
In this sketch, connecting pin 9 to ground will make the Pro Micro spit out
a ‘z’ character. If you have a simple, momentary button handy, tie one end
to pin 9 and the other to ground. Otherwise, just use a wire to short 9 to
GND.
After a
Keyboard.write() or Keyboard.print() function has been
performed by the Pro Micro, your computer will have to decide what to do
with it. What your computer does with that character, or string of characters,
is entirely dependent on what program it’s running at the time. If you have a
text editor open and active, it’ll print it out there.
USB Mouse Functionality
That covers about half of USB HID library. How about we add a mouse to
the mix now? Implementing a USB HID mouse requires a few more
functions, but it’s still crazy simple. There are five functions provided by
Arduino’s HID class that can be used to implement a mouse:
Page 1
of 2