Saturday, January 11, 2014

Software only Arduino keyboard emulator

I'm a proud owner of an Arduino Duemilanove, which unfortunately can not act as a USB keyboard without extra hardware modifications or unsafe burning processes. But we Duemilanove owners still want to send an occasional keyboard event from time to time. I've written an Arduino library with a corresponding piece of software to go on your PC (Windows only for now guys, sorry) to evoke the events the Arduino is sending.

The Arduino part is pretty straight forward. It sends structured messages to the receiver on the PC, which are structured like this:
sk [command] [key_code]

A command can be either, press, release or tap (press and release), the key_code is something Microsoft provides us with here. The receiver takes the key_code and tells the oprating system what do with it according to the command received from the Arduino.

Which means you can do stuff like, hold Shift, tap 1, release Shift. Which will in return get you an ! on the PC.

The Arduino code for such a task will looks like this:

 #include <SoftKeyboard.h>  
   
 SoftKeyboard sk;  
   
 void setup() {  
  Serial.begin(9600); //Must init Serial in order for SoftKeyboard to work!  
 }  
   
 void loop() {  
  sk.press(0x10);  //hold Shift down  
  sk.tap(0x31);   //tap '1' (with shift makes it '!')  
  sk.release(0x10); //release Shift  
  delay(1000);  
 }  

The receiver was implemented with C#, using some un-managed code of course (other wise you can not get full advantage of Windows' keyboard APIs).

Notes:
You can't see the serial port traffic form the Arduino IDE and be connected to the SoftKeyboard receiver at the same time, so the receiver has a -v option, to let you see what the Arduino is sending.

Ok, that was enough blah-blah, you can find the code and the receiver .exe here: https://github.com/MeLight/soft-keyboard

Let me know if you have questions or suggestions (you can do to that in the repo too of course)

2 comments:

  1. could you provide c# code that u have written Thanks :)
    For a such a project

    ReplyDelete
    Replies
    1. Ok, added the Program.cs file to the repo

      Delete