Keyboard Example

In the examples folder of the TinyUSB_Mouse_and_Keyboard library you will find 3 sample programs. The BLE52_Mouse_and_Keyboard library also has three similar files. There are only minor differences between the two. The BLE52 version will attempt to pair your nRF52840 with your PC or other device. See the earlier section on how to do the pairing. Once it is paired you should not need to pair it again and you can run on the examples with no further effort. The TinyUSB keyboard example looks like this.

Download: file
/*
 * Sample program demonstrating TinyUSB_Mouse_and_Keyboard.h module which
 * auto selects between standard Arduino Mouse.h API and TinyUSB mouse API.
 * This program tests the keyboard portion alone.
 */

//Include this module whether using Arduino stack or TinyUSB stack
#include <TinyUSB_Mouse_and_Keyboard.h>

uint8_t Multiple[3]= {'1','2','3'};
void setup() {
  Keyboard.begin();   //Unlike Arduino Keyboard.h, you must use begin.
  Serial.begin(115200);
  while (! Serial)delay (1);
  Serial.println("USB keyboard test");
  Keyboard.write('a');            //press and release 'a' key
  Keyboard.write(Multiple,3);     //multiple keys sequentially from a buffer
  Keyboard.print("456");          //print a string
  Keyboard.println("789");        //print another string with a line
  Keyboard.press(KEY_LEFT_SHIFT); //hold down the shift
  Keyboard.println("a uppercase sentence"); //this will be all caps
  Keyboard.release(KEY_LEFT_SHIFT);//release the shift
  Keyboard.println ("back to lowercase");
  Keyboard.press(KEY_LEFT_SHIFT); //press shift
  Keyboard.println("1234");       //some text
  Keyboard.releaseAll();          //release all
  Keyboard.println("1234");       //not shifted
  Keyboard.print("A mistake");  //will attempt to erase this
  delay(1000);
  Keyboard.press(KEY_LEFT_CTRL);  //will attempt control-z
  Keyboard.write('z');
  Keyboard.releaseAll();         //release the control key
  Serial.println("USB keyboard test completed");
};
void loop() {
}

/*
 * Click below before uploading and it will type characters in this comment
 * 
 * 
 *  
 *  
 *  
 *  
 *  
 *  
 */

Like the previous Mouse example, all of the code is in the setup section and there is nothing in the loop. This code will run just once each time you upload it.

Configure the Arduino IDE for your particular board and port to upload the code. If you are using an M0 or M4 based board be sure to select the Tools->USB Stack->TinyUSB option in your Arduino IDE.

If you are using USB connection on the nRF52840 or using the BLE52 version on the nRF52840 you will not have to change the USB Stack.

Open your serial monitor and compile and upload the code. While it is compiling, click your mouse somewhere in the comment at the bottom of the program. The program will then type some text into that comment and it won't hurt anything else. When you are finished you can delete the text.

This sample program makes use of all of the methods and features of the Keyboard class.

First it uses the Keyboard.write('a') command to press and release the "a" key. Then it uses the buffered version of the write method to write the three characters "123". Next it uses the more versatile Keyboard.print(…) method to type the characters "456". Finally it uses Keyboard.println(…) method to type "789" and then it adds a new line at the end.

Next we press and hold the left shift key and type a sentence using println(…). As you will see, even though the contents of that print statement is all in lowercase, the results come out in uppercase. We then use Keyboard.release(KEY_LEFT_SHIFT) to release the shift that we held down. When we use another println(…) To verify that we are back to lowercase. Note that this is a shift operation and not a caps lock. In the next series of commands we print "1234" with the shift key held and it comes out "[email protected]#$".

In our last test we print out the words "A mistake" and then one second later we hold down the left control key, write a "z" and then release all pressed keys. The control-z does an undo operation in this text editor as is common for most text editors. That concludes our test.

If you are using an M0 or M4 board you might want to try compiling the same code using the Arduino Stack. It should operate the same way using the traditional Arduino Keyboard.h library.

This guide was first published on May 13, 2020. It was last updated on May 13, 2020.
This page (Keyboard Example) was last updated on Jul 03, 2020.