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. This TinyUSB example combines both mouse and keyboard capabilities. It looks like this.

 * 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 mouse and keyboard portions together.

//Include these modules whether using Arduino stack or TinyUSB stack
#include <TinyUSB_Mouse_and_Keyboard.h>

void setup() {
  //Don't really need much Serial output but we wanted to make sure it doesn't interfere
  Keyboard.begin(); //Unlike Arduino Keyboard.h and Mouse.h, 
  Mouse.begin();    //    you must use "begin" method for both mouse and keyboard.
  while (! Serial)delay (1);
  Serial.println("USB mouse and keyboard test");
  Keyboard.println("The first line of text");   //write some text
  Keyboard.println("The second line of text");
  Serial.println("Wrote some text.");
  delay(2000);;;   //hold the shift key
  Mouse.move(100,0);                //move the mouse;                    //click again
  Keyboard.releaseAll();            //release the shift
  Serial.println("Highlighted the first line.");
  delay(2000);;    //do a control-x
  Keyboard.write('x');              //press and release 'x' key
  Keyboard.releaseAll();            //release the control
  Serial.println("Cut the text");
  Mouse.move(0, 20);                //move the mouse down a bit;
  delay(2000);;    //do a control-v
  Keyboard.write('v');              //press and release 'v' key
  Keyboard.releaseAll();            //release the control
  Serial.println("Pasted the text");
  Serial.println("Test completed.");
void loop() {

 * Click just after the "*" on the on the next line before uploading

As mentioned previously, you should do both Mouse.begin() and Keyboard.begin() even though only one of them were actually run. They can be placed in either order. It's just a good idea to highlight the fact that this particular program uses both mouse and keyboard features.

Like the previous two examples, 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 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 configure the USB Stack.

Open your serial monitor and compile and upload the code. While it is compiling, click your mouse just after the "*" in the second line of the comment. We will then type some text on the next two lines. Then we will be positioned at the start of the first line. The program press and hold the shift key and move (but not drag) the mouse to the right. It will then left click the mouse which will select the text. This verifies that we can do things such as shift click in combination. The program will use a control-x to cut the text. We move the mouse down a ways and then then it below the second line of text. This completes our test.

If you're using an M0 or M4 board you might want to try compiling the same code using the Arduino Stack. It should work the same way using the traditional Arduino Keyboard.h and Mouse.h libraries.

Notice that approximately line 31 of the BLE52 version of this program we only check for Mouse.isConnected(). We previously mentioned that your board presents itself as a single USB or BLE device with both mouse and keyboard capabilities so you only need to check if one is connected.

This guide was first published on May 13, 2020. It was last updated on Mar 08, 2024.

This page (Mouse and Keyboard Combined Example) was last updated on Mar 08, 2024.

Text editor powered by tinymce.