Go Down

Topic: pc keyboard with arduino? (Read 2582 times) previous topic - next topic

jhacked

Guys, i'm italian and i hope that all of you will understand my problem. I need to build a simple pc keyboard with three key: up arrow, down arrow, and enter key. I was thinking to this simple keyboard to solve my problem about my bluetooth apple keyboard that obviously doesn't function in the choice of the operating system. So I need to build a keybord that allow me to choose the operating system in the boot. Can i do it with arduino? And how can i do that? every information is welcome!! Thank you to all!!!!

fm

You can use an Arduino Leonardo clone, like the teensy, vinciDuino and similar boards. They have an ATmega32u4 with USB native interface. The vinciDuino supports the HDI interface straight from the Arduino IDE: keyboard, mouse and joystick are supported. I suppose that the teensy will as well.
   

justjed


doesn't function in the choice of the operating system.


Not sure if it matters, but what OS? You have a BT keyboard for a Linux box? Or something else? fm makes the assumption (a very likely one) that you'll be plugging into USB, but maybe you need a PS2 connection?

Of course, it will be fun to build this with Arduino, but have you considered a numeric keypad? With NumLock off, you'll have arrow keys. Your home-brew device can be much smaller, which I imagine will be a plus for doing it that way.
... it is poor civic hygiene to install technologies that could someday
facilitate a police state. -- Bruce Schneier

jhacked

i have a wireless apple keyboard on a windows pc. Just yesterday i was seeing tath numeric keypads and i forgot that they, with the numlock off, have the arrow keys. Anyway I need an usb connection because, in boot, it function and ps2 too, so i think is the same thing. isn't it? However I'm not sure if i'll build this. should is it hard? And what I would buy? A simple Arduino uno or an Arduino Leonardo? Thank you to all!

fm

#4
Apr 17, 2012, 08:34 am Last Edit: Apr 17, 2012, 08:36 am by fm Reason: 1
Leonardo is not out there yet. So my guess for wha you need is any device with an ATmega32u4 with native USB support. Something like the boards I mentioned. The IDE since version 1.0 supports the HDI class and has a built in class to manage a keyboard or a pointing device.

Not too sure about arrow keys, I haven't used them on my experiments. But I have use the rest though and it works very nicely.
   

Daanii

The Teensy may work well for you. You can get one for $16 from www.pjrc.com. They also have a code library at that website for USB keyboards: http://www.pjrc.com/teensy/usb_keyboard.html

jhacked


The Teensy may work well for you. You can get one for $16 from www.pjrc.com. They also have a code library at that website for USB keyboards: http://www.pjrc.com/teensy/usb_keyboard.html


oh god that library is really what I need!! I didn't understand that "teensy" was a name of an a arduino. so it should be hard considering that I have some experience in languages programming but no experience in hardware?

fm

The vinciDuino is also an Arduino that works directly on the Arduino IDE that has the associated libraries for the keyboard, very similar to the teensy. The main difference is the form factor, while the teensy is smaller, the vinciDuino allows for standard shields (arduino form factor).
   

jhacked

yes I get it, but i would to know one thing like I asked before, it would be hard?

fm

I will try to post an example this evening. What you are looking to do seams very simple:
Read a button and send a key according to the button press.
   

dhunt

With the Uno you can reprogram the atmega8u2 or atmega16u2 (rev 3 Uno) to be a USB HID keyboard.  Check the links here in the playground: http://arduino.cc/playground/Main/InterfacingWithHardware#USB.

jhacked

#11
Apr 17, 2012, 02:05 pm Last Edit: Apr 17, 2012, 03:31 pm by jhacked Reason: 1

With the Uno you can reprogram the atmega8u2 or atmega16u2 (rev 3 Uno) to be a USB HID keyboard.  Check the links here in the playground: http://arduino.cc/playground/Main/InterfacingWithHardware#USB.


it's better to buy something ready to use like the teensy. So I'll use it. fm, I'm waiting for that code example my friend. Really thank you! one more thing, where can I find some instruction to weld the buttons?

fm

#12
Apr 17, 2012, 10:07 pm Last Edit: Apr 17, 2012, 10:10 pm by fm Reason: 1
Here is what the code would look like:

Code: [Select]

#include <Arduino.h>
#include <USBAPI.h>

#define KEY_1   5  // Key 1 pin definition
#define KEY_2   6  // Key 2 pin definition
#define KEY_3   7  // Key 3 pin definition

// USB keyboard key mapping for arrow keys
#define KEY_RIGHT 79
#define KEY_LEFT 80
#define KEY_DOWN 81
#define KEY_UP 82
#define KEY_ENTER       40

#define LED_PIN 13


Up to this point, we have just declared a few constants. KEY_i (i=1,2,3) defines the digital inputs to read the push-buttons. Where as the KEY_x (x=RIGHT, LEFT, ...), define the USB keys for up, down, left and right.

Code: [Select]

void setup()
{
 // Initialise hardware
 pinMode ( LED_PIN, OUTPUT );
 pinMode ( KEY_1, INPUT );
 pinMode ( KEY_2, INPUT );
 pinMode ( KEY_3, INPUT );
}

Here you just define your pushbuttons as digital inputs.

Code: [Select]

void loop()
{  
 KeyReport keys = {0};  // KeyReport
 bool      keyPressed = false;
 static    ledState = 0;

 digitalWrite ( LED_PIN, ledState );
 ledState ^= 1;

 if ( digitalRead ( KEY_1 ) )
 {
   keys.keys[0] = KEY_UP;
   keyPressed = true;
 }
 
 if ( digitalRead ( KEY_2 ) )
 {
   keys.keys[0] = KEY_DOWN;
   keyPressed = true;
 }
 
 if ( digitalRead ( KEY_3 ) )
 {
   keys.keys[0] = KEY_ENTER;
   keyPressed = true;
 }
 
 if ( keyPressed )
 {
   Keyboard.sendReport(&keys);
 }
 delay ( 100 );
}

What this code does, is to read the state of the input, here we have assumed that you have a pull-down (when the button is not pressed you will be reading LOW and when the button is pressed you will read a HIGH) resistor on your push-button (schematic attached).

The main loop, blinks the LED, and then goes through each digital input to see what its state is. If the state is HIGH, it will set the variable "keys" to the mapped USB arrow key. The program will then send "Keyboard.sendReport (&key);" the USB key (as if it were your keyboard) to the computer.

That is all to it. You can see it is very easy what you are trying to do with an AVR like the ATMega32U4 and the Arduino IDE or even some libraries out there.

WARNING - 1 - the program is over simplistic here, it will only send 1 key event at the time, so if you have more than one button pressed it will only send one of them (bottom up order in the code). You will have to add some logic to debounce the key press. In the code here I have added a 100ms delay but there are other ways to do it.

WARNING - 2 - the Keyboard.sendReport, is not public in the Keyboard class (variable), so you will have to go into the distribution and move the sendReport method from where it says private, to public, in the USBAPI.h file. I may write an HID extension over the weekend.
   

jhacked


With the Uno you can reprogram the atmega8u2 or atmega16u2 (rev 3 Uno) to be a USB HID keyboard.  Check the links here in the playground: http://arduino.cc/playground/Main/InterfacingWithHardware#USB.


Guys, I bought the arduino UNO rev 3 just to do something more of a simple pc-keyboard. So i would like to know how I can install this on my arduino: http://hunt.net.nz/users/darran/weblog/b3029/Arduino_UNO_Keyboard_HID_version_03.html
Can someone give me some instructions please?
Thank you very much!

PaulS

Quote
I didn't understand that "teensy" was a name of an a arduino.

But it isn't.

Go Up