Keyboard Emulator

Hey Gang

I am relatively new to Arduino. I am wanting to create a keyboard emulator. I have done much searching including purchasing a number of books but it is proving to be rather difficult. I’d like to simply start by writing something like “Hello world!”. From what little I do understand using PS2 is much more simple than USB. I have downloaded the PS2Keyboard Library and have got as far as the following;

#include <PS2Keyboard.h>
#define DATA_PIN 4
PS2Keyboard keyboard;

void setup()
{
  keyboard.begin(DATA_PIN);
}

void loop()
{

Any help would be greatly appreciated.

Kind Regards

Jason

I am wanting to create a keyboard emulator.

For what purpose? Your request is like saying "I want to build a rocket." If the rocket is just supposed to look like a rocket, for an art exhibit for instance, the design criteria will be quite a bit different than the design criteria for a rocket that is to launch humans into space for a flight to Mars.

The PS2 keyboard library is for connecting a PS2 library to the Arduino. Is that what you want to do? What do you expect to do with whatever is typed on the keyboard?

Details, details, details... The more you provide, the better the answers you'll get.

I think you are using the wrong library. The PS2Keyboard library is for getting keystrokes from a PS2 keyboard attached to the Arduino.

There is a PS2Dev library that allows you to emulate a PS2 device such as a mouse or keyboard. You can find it at the bottom of this page:

http://www.arduino.cc/playground/ComponentLib/Ps2mouse

Hey Gang

Thanks for the replies. I want the Arduino to be the device and the computer to be the host. I want to receive input via the interrupts (magnetic swipe) and send the result into Excel.

I have tried using the PSDev library but the sample file is written to use a mouse. I admit, it is a little above my head too. If I could see a keyboard example I believe I could manipulate it to what I want (well at least try ;) ).

Look forward to hearing from you.

Kind Regards

Jason

If you have an UNO or mega you can reprogram it to be a USB Keyboard HID device, so it can behave just like a USB keyboard as far as the PC host is concerned. You can find the details on how to do this here: Arduino Hacking Blog.

The PS2Dev library has functions for reading and writing to the hardware. The mouse example shows how to detect commands from the host and how to send data to the host. I think that all you need to do is send the scan codes for the keys you want to emulate:

http://www.computer-engineering.org/ps2keyboard/scancodes2.html
http://www.computer-engineering.org/ps2keyboard/

Be sure to send the corresponding “BREAK”(key up) code after each “MAKE” (key down) code!

http://www.computer-engineering.org/ps2keyboard/

"USB keyboard - Latest keyboard supported by all new computers (Macintosh and IBM/compatible). These are relatively complicated to interface and are not covered in this article." That's not much help in these modern times 8)

Hey Guys

Thanks for the replies.

Mr Hunt

I have had a look at your blog. It looks really interesting although being so new to Arduino I am hesitant. I have read the thread on DFU and it is a little overwhelming. It this process reversible?

Mr Wasser

I had a look at the mouse example but couldn't relate it to a keyboard. As mentioned earlier, I'm pretty new at this. Is there a paper or something with examples of how to use the PS2Dev library?

Mr Roads

Thanks :)

Kind Regards

Jason

Hi there

Have you thought about using AAC keys?

Arduino sends commands though serial and AAC keys intrepetate it as keypresses or mouse movements.

-Fletcher

Hi Jason,

yes the DFU update is reversible - you can always reprogram the UNO with Arduino-usbserial.hex to restore the normal operation. You'll probably find that you do this a few times, using the keyboard firmware to test your sketch and then switching back to the usbserial firmware so you can upload a new sketch.

Cheers, Darran,

Hi Guys

Sorry for the late reply. Thanks for your suggestions. I will check them out.

Kind Regards

Jason

I’m looking for the same thing. I want to use buttons attached to the arduino to do things with iTunes. GoBetwino works but doesn’t do the trick for me. What I want is to be able to select the search field in iTunes, print the name of an artist and than hit the play button.
I tried AAC Keys and after some reading and experimenting I got this to work. The code below may seem a bit complicated but I’m using the Nuelectronics LCD-keypad-shield as button input device. The buttons are on a analog input. The analog value represents one of the six buttons being pressed. Each button sends another string to AAC Keys. Important is the delay after the Serial.print() command. Without it AAC registers more than one keypress. It’s all on the forum, but it took me a while to figure it out.

To press two buttons like “alt-b” the string to trigger AAC is: Serial.print(\033,hold,alt.b)

#include <LCD4Bit_mod.h> 
LCD4Bit_mod lcd = LCD4Bit_mod(2); 

//Key message
char msgs[5][60] = {"\033,hold,alt.h", 
                    "\033,hold,alt.b", 
                    "\033,hold,ctrl.o", 
                    "test", 
                    "test"};
int adc_key_val[5] ={30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;

void setup(){ 
  Serial.begin(9600);
  lcd.init();
  lcd.clear();
  lcd.printIn("Start");
}

void loop(){
  adc_key_in = analogRead(0);    // read the value from the sensor  
  key = get_key(adc_key_in);	 // convert into key press
  if (key != oldkey){		 // if keypress is detected
    delay(50);		         // wait for debounce time to eliminate storing
    adc_key_in = analogRead(0);  // read the value from the sensor  
    key = get_key(adc_key_in);   // convert into key press
    if (key != oldkey){			
      oldkey = key;
      if (key >=0){
        lcd.clear();
        lcd.printIn(msgs[key]);
        Serial.print(msgs[key]);
        delay(500);
  }}}
  delay(50);
}

// Convert ADC value to key number
int get_key(unsigned int input){
  int k;
  for (k = 0; k < NUM_KEYS; k++){
    if (input < adc_key_val[k]){
      return k;
  }}
  if (k >= NUM_KEYS){
    k = -1;     // No valid key pressed
    return k;
}}

The above did not work to well because AAC behaves difficult when pushing more than two keys at a time. When combined with AutoHotKey (ahk) it is more controlable.

Arduino code same as above exept for:

char msgs[5][60] = {"blondie atomic", 
                    "\033,hold,alt.p", 
                    "\033,hold,alt.n", 
                    "\033,hold,alt.z", 
                    "\033,hold,alt.s"};

At the moment only one song can be selected with a push of a button. But this is a start.

AHK code:

#SingleInstance, Force

itunes := ComObjCreate("iTunes.Application")

DetectHiddenWindows, On

!s::
itunes.PlayPause()
return

!p::
itunes.PreviousTrack()
return

!n::
itunes.NextTrack()
return

!u::
ControlSend, ahk_parent, ^{up}, iTunes ahk_class iTunes
return

!d::
ControlSend, ahk_parent, ^{down}, iTunes ahk_class iTunes
return

!z::
IfWinExist,ahk_class iTunes
   {
   itunes.Pause()
   winset,top
   winactivate
   controlclick,Edit1,iTunes ahk_class iTunes ;activate the "search" control
   controlfocus,Edit1,iTunes ahk_class iTunes ;activate the "search" control
   ControlSend, ahk_parent,{Esc}, iTunes ahk_class iTunes
   }
else
   Run C:\Program Files\iTunes\iTunes.exe
return

This works well, but don't forget to start AAC after uploading.