2016/06/13

How to turn Arduino Uno into HID Keyboard - Part 2 of 2

This post is the second part of How to turn Arduino Uno into HID Keyboard tutorial. The first part can be found here.

To test the HID Keyboard, I want to use the "Random Key/Random Delay" code from http://mitchtech.net/arduino-usb-hid-keyboard/ because of its simplicity and there is no wiring involved.

/* Arduino USB HID Keyboard Demo
 * Random Key/Random Delay
 */

uint8_t buf[8] = {
  0 }; /* Keyboard report buffer */

void setup()
{
  Serial.begin(9600);
  randomSeed(analogRead(0));
  delay(200);
}

void loop()
{
  int randomChar = random(4, 130);
  long randomDelay = random(1000, 10000);

  delay(randomDelay);

  buf[2] = randomChar;  // Random character
  Serial.write(buf, 8); // Send keypress
  releaseKey();
}

void releaseKey()
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); // Release key
}



To use the code, follow the below procedure (assuming the ATMega16U2 has "Arduino-usbserial-uno.hex" in it):

1. Copy and past the above code into Arduino IDE;

2. Compile and upload the code to Arduino Uno;

3.. Follow the instruction in my previous post to burn the "Arduino-keyboard-0.3.hex" into ATMega16U2;

4. Unplug the USB cable and plug it back in;

5. Launch a word editing program (MS-Word, UltraEdit, etc.) and wait to see random character appears at random interval.

When you are done with the test, follow the instruction in my previous post to burn the "Arduino-usbserial-uno.hex" back into ATMega16U2 then unplug the USB cable and plug it back to make the board a normal Arduino Uno again.

Reference

Arduino USB HID Keyboard
http://mitchtech.net/arduino-usb-hid-keyboard/

No comments:

Post a Comment