Arduino Uno and Keyboard Library

Hi,

With an Arduino Uno, I want to send F1 Key if a led lights up, but it doesn't work. I got this error:

error: 'KEY_F1' was not declared in this scope
char key = KEY_F1;
C:\Users\user-pc\Desktop\switch2\switch2.ino: In function 'void loop()':
switch2:20: error: 'Keyboard' introuvable. Est-ce que votre croquis inclut la ligne '#include <Keyboard.h>' ?
Keyboard.press(key);
exit status 1
'KEY_F1' was not declared in this scope

When I change from Arduino Uno to Arduino Leonardo, it WORKS!
I don't understand what's wrong with Arduino Uno???

Here is the code, I'll be grateful if you could help me. Thanks!
``#include <Keyboard.h>
int led = 8;
int switcher = 7;
int buttonstate = 0;
char key = KEY_F1;
void setup(){
pinMode(led, OUTPUT);
pinMode(switcher,INPUT);

Serial.begin(9600);

}
void loop(){
buttonstate = digitalRead(switcher);
Serial.print(buttonstate);

if (buttonstate == HIGH) {
digitalWrite(led, HIGH);
//Keyboard.write(0xC2);
Keyboard.press(key);
Keyboard.releaseAll();
}
else {
digitalWrite(led, LOW);
}
}

There's a subset of Arduinos that can act as a keyboard. Leonardo is one as you have discovered. Uno is not.

Oh ok!

So I can't work with Arduino Uno? There is any alternative? because it's the only one I have with me.

The alternative for Uno is something like this:

but be aware it's a very advanced project that might "brick" your Uno if something goes wrong.

Another possibility is to write a program on the PC that listens to the Uno on a serial port. You can devise your own protocol and send it a message that tells it to simulate the press of a particular key.

Thanks @pert for the advice. I'll try it.

Please @wildbill could you explain more. Is the program with C#? If you have any example please share it with me. Thank you so much.

You could use most any language on the PC - they can all read a serial port and emit keyboard commands. C# is fine, but use whatever your language of choice is.

It's not something I've ever done specifically, so I have no code to show you. If I were trying to do this, I'd get myself an Arduino (or clone) that can act as a keyboard. Unless cash or delivery time are constraints, it's more painful to do it the way I suggested.

ulearner:
Thanks @pert for the advice. I'll try it.

You're welcome. I wish you luck!

ulearner:
Please @wildbill could you explain more. Is the program with C#? If you have any example please share it with me. Thank you so much.

You could do wildbill's solution very easily using the free, open source automation program EventGhost:
http://www.eventghost.net/
Although EventGhost does allow you to do advanced things with Python scripting, It also has a visual programming interface which will be perfectly sufficient for your project. So you only need to point and click and drag and drop from the GUI.

You should also be able to do it using AutoHotkey:

It doesn't have a visual programming interface so it will be a little more complex to create a working configuration to do what you need.

EventGhost is for Windows only and I believe the same is true for AutoHotkey. I'd expect there would be something similar for other operating systems, but I'm not familiar with them.

ulearner:
Oh ok!

So I can't work with Arduino Uno? There is any alternative? because it's the only one I have with me.

I recommend the Arduino Micro. It is probably cheaper than the Uno.

All of the Teensy series have this ability too.

I also recommend using the right board for the job. I like the Pro Micro for simple projects where I need keyboard or mouse emulation. It's small (allowing me to use cheaper enclosures) and easy to solder on to stripboard.

However, if all you have to work with is an Uno, well maybe some of our workarounds might do the trick.

Thank you all for your advices. I'm grateful to you!

I started programing with C#, if it doesn't work I will try EventGhost.