Arduino Function: Keyboard.write

Description

This function sends a keystroke to a connected computer, similar to pressing and releasing a key on your keyboard. 
You can send some ASCII characters or the additional keyboard modifiers and special keys.

Only ASCII characters that are on the keyboard are supported (see ASCIITable.com).

Syntax

Keyboard.write(character)

Parameters

  • character : a char or int to be sent to the computer. Can be sent in any notation that's acceptable for a char. For example, all of the below are acceptable and send the same value, 65 or ASCII A:
 
1
Keyboard.write(65);         // sends ASCII value 65, or A 
2
Keyboard.write('A');            // same thing as a quoted character
3
Keyboard.write(0x41);       // same thing in hexadecimal
4
Keyboard.write(0b01000001); // same thing in binary (weird choice, but it works)

Returns

int number of bytes sent

Warning

When you use the Keyboard.write() command, the Arduino takes over your keyboard! Make sure you have control before you use the command. A pushbutton to toggle the keyboard control state is effective.

Examples

void setup() {
// make pin 2 an input and turn on the
// pullup resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
Keyboard.begin();
}

void loop() {
//if the button is pressed
if(digitalRead(2)==LOW){
//Send an ASCII 'A',
Keyboard.write(65);
}
}


Based on the reference originally written by Arduino Community,  wiring.org.co and  arduino.cc