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. Serial.begin(115200); 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); Mouse.click(); Keyboard.press(KEY_LEFT_SHIFT); //hold the shift key Mouse.move(100,0); //move the mouse Mouse.click(); //click again Keyboard.releaseAll(); //release the shift Serial.println("Highlighted the first line."); delay(2000); Keyboard.press(KEY_LEFT_CTRL); //do a control-x Keyboard.write('x'); //press and release 'x' key Keyboard.releaseAll(); //release the control Serial.println("Cut the text"); delay(2000); Mouse.move(0, 20); //move the mouse down a bit Mouse.click(); delay(2000); Keyboard.press(KEY_LEFT_CTRL); //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.
Text editor powered by tinymce.