user335: Is it possible to build a HID device (like a keyboard) using an Arduino uno?
At the time being, I have button inputs on the Arduino giving outputs on the serial line. So, how can I transform my current firmware into something that can behave like a HID keyboard?

Shall I write a new window device driver?
Shall I create a software layer that could take my serial data and have windows read it as HID input?
Is there any better way to achieve this, if it is possible at all?


user-1: I created a project that fully replaces the Arduino Bootloader. You are able to upload sketches and use it as HID. See Project HID on Github: https://github.com/NicoHood/HID

user379: 
Is it possible to build a HID device (like a keyboard) using an Arduino uno?

yes it is! And there's a great tutorial on how to do that over here

At the time being, I have button inputs on the Arduino giving outputs on the
serial line. So, how can I transform my current firmware into something that
can behave like a HID keyboard?

As the tutorial shows you, you need to transform your firmware code so instead of
printing the events, you actually trigger events. What you need to do, is add to your
code:
void triggerKey(uint8_t mod, uint8_t chr) {
    uint8_t buf[8] = {mod, chr, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    // press key
    Serial.write(buf, 8);
    // emulate key press delay
    delay(10);
    buf[1] = 0x00;
    // release key
    Serial.write(buf, 8);
}

What you're doing here, is send from your main Arduino microcontroller events over
the serial line to the smaller microcontroller that handles USB communication. That
microcontroller will take those key "buffers" and transfer them as USB HID Keyboard
key presses over USB.
Beware that the key character (chr in the code here), is actually a keycode sent
over USB that gets interpreted by your USB HID driver, which then translates it into
a real character using the current layout you've setup for your keyboard.
The mod character is one of the modifier keys you have on the keyboard (shift,
control, alt...), 0x00 being no modifier.


Shall I write a new window device driver?


No, you don't need! With the DFU upgrade, your arduino will be seen as a USB keyboard
by windows.


Shall I create a software layer that could take my serial data and have windows read it as HID input?


You neither need to do that, and it's actually a very bad idea for many reasons:

your solution would not be portable, and thus you'd need to hack something for every platform you may use,
your solution would imply a lot of new code ; the more code one write, the more bugs is likely to be in your system,
your solution would be really hackish, as it would behave as a device driver (i.e. kernel land) taking input from
a user program (i.e. user land) and is likely to not work, be a hell to debug, and prone for security issues...
If that's even possible to do!


I'm using an Uno, but I'll look at what you posted because last time we considered the DFU update but we're kinda scared of bricking stuff

Finally you're having concerns, in a comment, about bricking your arduino. Please
believe me and the author of the article that you cannot brick your arduino
doing a firmware flash:
NOTE: There is no chance of ‘bricking’ the device using this method. The Arduino bootloader firmware can always be updated using the DFU protocol!
In case something gets really badly wrong, and you get a bootloader corruption in the
little AVR (atmega8u2), you still can reflash it using another arduino or an
atmel programmer using the 6 pins next to the USB plug.
In the end, know that the only way to brick an AVR, thus an Arduino as well, is
to play with the fuses and reset the programming fuses. And even then, you may use
HVSP to save your device, though it's often less trouble to just take a new chip.

335: <p>Is it possible to build a HID device (like a keyboard) using an Arduino uno?</p>

<p>At the time being, I have button inputs on the Arduino giving outputs on the serial line. So, how can I transform my current firmware into something that can behave like a HID keyboard?</p>

<ul>
<li>Shall I write a new window device driver?</li>
<li>Shall I create a software layer that could take my serial data and have windows read it as HID input?</li>
<li>Is there any better way to achieve this, if it is possible at all?</li>
</ul>
 -1: <p>I created a project that fully replaces the Arduino Bootloader. You are able to upload sketches and use it as HID. See Project HID on Github: <a href="https://github.com/NicoHood/HID" rel="noreferrer">https://github.com/NicoHood/HID</a></p>
 379: <blockquote>
<p>Is it possible to build a HID device (like a keyboard) using an Arduino uno?</p>
</blockquote>
<p>yes it is! And there's a great tutorial on how to do that over <a href="http://mitchtech.net/arduino-usb-hid-keyboard" rel="nofollow noreferrer">here</a></p>
<blockquote>
<p>At the time being, I have button inputs on the Arduino giving outputs on the
serial line. So, how can I transform my current firmware into something that
can behave like a HID keyboard?</p>
</blockquote>
<p>As the tutorial shows you, you need to transform your firmware code so instead of
printing the events, you actually trigger events. What you need to do, is add to your
code:</p>
<pre><code>void triggerKey(uint8_t mod, uint8_t chr) {
    uint8_t buf[8] = {mod, chr, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    // press key
    Serial.write(buf, 8);
    // emulate key press delay
    delay(10);
    buf[1] = 0x00;
    // release key
    Serial.write(buf, 8);
}
</code></pre>
<p>What you're doing here, is send from your main Arduino microcontroller events over
the serial line to the smaller microcontroller that handles USB communication. That
microcontroller will take those key &quot;buffers&quot; and transfer them as USB HID Keyboard
key presses over USB.</p>
<p>Beware that the key character (<code>chr</code> in the code here), is actually a keycode sent
over USB that gets interpreted by your USB HID driver, which then translates it into
a real character using the current layout you've setup for your keyboard.</p>
<p>The <code>mod</code> character is one of the modifier keys you have on the keyboard (<kbd>shift</kbd>,
<kbd>control</kbd>, <kbd>alt</kbd>...), <code>0x00</code> being no modifier.</p>
<blockquote>
<ul>
<li>Shall I write a new window device driver?</li>
</ul>
</blockquote>
<p>No, you don't need! With the DFU upgrade, your arduino will be seen as a USB keyboard
by windows.</p>
<blockquote>
<ul>
<li>Shall I create a software layer that could take my serial data and have windows read it as HID input?</li>
</ul>
</blockquote>
<p>You neither need to do that, and it's actually a very bad idea for many reasons:</p>
<ul>
<li>your solution would not be portable, and thus you'd need to hack something for every platform you may use,</li>
<li>your solution would imply a lot of new code ; the more code one write, the more bugs is likely to be in your system,</li>
<li>your solution would be really hackish, as it would behave as a device driver (i.e. <em>kernel land</em>) taking input from
a user program (i.e. <em>user land</em>) and is likely to not work, be a hell to debug, and prone for security issues...
If that's even possible to do!</li>
</ul>
<blockquote>
<p>I'm using an Uno, but I'll look at what you posted because last time we considered the DFU update but we're kinda scared of bricking stuff</p>
</blockquote>
<p>Finally you're having concerns, in a comment, about bricking your arduino. Please
believe me and the author of the <a href="http://mitchtech.net/arduino-usb-hid-keyboard" rel="nofollow noreferrer">article</a> that you <strong>cannot</strong> brick your arduino
doing a firmware flash:</p>
<p><em>NOTE: There is no chance of ‘bricking’ the device using this method. The Arduino bootloader firmware can always be updated using the DFU protocol!</em></p>
<p>In case something gets really badly wrong, and you get a bootloader corruption in the
little AVR (atmega8u2), you still can reflash it <a href="http://arduino.cc/en/Tutorial/ArduinoISP" rel="nofollow noreferrer">using another arduino</a> or an
<a href="http://www.atmel.com/tools/avrispmkii.aspx" rel="nofollow noreferrer">atmel programmer</a> using the 6 pins next to the USB plug.</p>
<p>In the end, know that the <strong>only</strong> way to brick an AVR, thus an Arduino as well, is
to play with the fuses and reset the programming fuses. And even then, you may use
<a href="http://microtherion.github.io/ScratchMonkey/HVSP.html" rel="nofollow noreferrer">HVSP</a> to save your device, though it's often less trouble to just take a new chip.</p>
