ESP32 Keyboard Simulation: A Comprehensive Guide

Keyboard simulation is one of the commonly used features in development.Today, I used the ESP32 microcontroller in conjunction with the ESP32-BLE-Keyboard library to conduct a test and successfully implemented this feature.Now, I will organize and share my testing process with everyone, hoping it will be helpful.

1. Development IDE

I used Arduino IDE.

ESP32 Keyboard Simulation: A Comprehensive Guide

ESP32 Keyboard Simulation: A Comprehensive Guide

2. Development Chip

ESP32 Keyboard Simulation: A Comprehensive Guide

ESP32 Keyboard Simulation: A Comprehensive Guide

3. Library Import

The core library used here is ESP32-BLE-Keyboard, but it was found to be missing in local validation.

ESP32 Keyboard Simulation: A Comprehensive Guide

Therefore, it is necessary to load this library as a .zip file.

ESP32 Keyboard Simulation: A Comprehensive Guide

The content has been shared on the cloud disk:

Fanmu Technology Cloud Disk

http://clouddisk.fanmukeji.cn:8080/s/M0fo

The access password is: fanmukeji

After downloading, select 【Project】->【Import Library】->【Add .ZIP Library】

ESP32 Keyboard Simulation: A Comprehensive Guide

Select the downloaded zip library file.

ESP32 Keyboard Simulation: A Comprehensive Guide

ESP32 Keyboard Simulation: A Comprehensive Guide

4. Write Code

#include <BleKeyboard.h> // Import Bluetooth keyboard library

// Configure Bluetooth device name

BleKeyboard bleKeyboard(“ESP32_MediaKey”, “ESP32”, 100);

bool deviceConnected = false; // Mark device connection status

void setup() {

Serial.begin(115200);

Serial.println(“start ble keyboard…”);

bleKeyboard.begin();

Serial.println(“start ble keyboard finish”);

}

void loop() {

Serial.println(“Loop work”);

// Check device connection status

if (bleKeyboard.isConnected()) {

Serial.println(“bleKeyboard.isConnected()”);

if (!deviceConnected) {

Serial.println(“device is connected”);

deviceConnected = true;

}

// Check serial input commands

if (Serial.available()) {

char command = Serial.read();

// Print received command

Serial.print(“Received command: “);

Serial.println(command);

// Trigger by inputting letters via serial

if (command == ‘n’) {

Serial.println(“send next command”);

// Send “next” command

bleKeyboard.write(KEY_MEDIA_NEXT_TRACK);

}else if (command == ‘p’) {

Serial.println(“send previous command”);

// Send “previous” command

bleKeyboard.write(KEY_MEDIA_PREVIOUS_TRACK);

} else if (command == ‘s’) {

Serial.println(“send start command”);

// Send “play” command

bleKeyboard.write(KEY_MEDIA_PLAY_PAUSE);

}else if (command == ‘o’) {

Serial.println(“send stop command”);

bleKeyboard.write(KEY_MEDIA_STOP); // Send “stop” command

}else if (command == ‘u’) {

Serial.println(“send up command”);

bleKeyboard.write(KEY_UP_ARROW); // Send “up” command

}else if (command == ‘d’) {

Serial.println(“send down command”);

bleKeyboard.write(KEY_DOWN_ARROW); // Send “down” command

}else {

Serial.println(“Unknown command”);

// Additional commands can be added

// KEY_LEFT_CTRL

// KEY_LEFT_SHIFT

// KEY_LEFT_ALT

// KEY_LEFT_GUI

// KEY_RIGHT_CTRL

// KEY_RIGHT_SHIFT

// KEY_RIGHT_ALT

// KEY_RIGHT_GUI

// KEY_UP_ARROW

// KEY_DOWN_ARROW

// KEY_LEFT_ARROW

// KEY_RIGHT_ARROW

// KEY_BACKSPACE

// KEY_TAB

// KEY_RETURN

// KEY_ESC

// KEY_INSERT

// KEY_PRTSC

// KEY_DELETE

// KEY_PAGE_UP

// KEY_PAGE_DOWN

// KEY_HOME

// KEY_END

// KEY_CAPS_LOCK

// KEY_F1

// KEY_F2

// KEY_F3

// KEY_F4

// KEY_F5

// KEY_F6

// KEY_F7

// KEY_F8

// KEY_F9

// KEY_F10

// KEY_F11

// KEY_F12

// KEY_F13

// KEY_F14

// KEY_F15

// KEY_F16

// KEY_F17

// KEY_F18

// KEY_F19

// KEY_F20

// KEY_F21

// KEY_F22

// KEY_F23

// KEY_F24

// KEY_NUM_0

// KEY_NUM_1

// KEY_NUM_2

// KEY_NUM_3

// KEY_NUM_4

// KEY_NUM_5

// KEY_NUM_6

// KEY_NUM_7

// KEY_NUM_8

// KEY_NUM_9

// KEY_NUM_SLASH

// KEY_NUM_ASTERISK

// KEY_NUM_MINUS

// KEY_NUM_PLUS

// KEY_NUM_ENTER

// KEY_NUM_PERIOD

}

}

} else {

Serial.println(“bleKeyboard.disConnected()”);

// Reinitialize Bluetooth when the device is disconnected

if (deviceConnected) {

Serial.println(“device disconnected, restarting Bluetooth broadcast”);

deviceConnected = false;

}

//bleKeyboard.begin(); // Restart broadcast to allow reconnection

}

delay(5000); // Control sending frequency to avoid misoperation

}

5. Compile and Upload to Chip

After connecting to the ESP32 chip

ESP32 Keyboard Simulation: A Comprehensive Guide

Execute upload: (Note: At this time, the serial monitor needs to be closed to prevent the serial connection from failing)

ESP32 Keyboard Simulation: A Comprehensive Guide

After flashing, the chip starts up.

ESP32 Keyboard Simulation: A Comprehensive Guide

6. Debug Code

Connect the device with a mobile phone.

ESP32 Keyboard Simulation: A Comprehensive Guide

Open the serial monitor, 【note the port selection】

ESP32 Keyboard Simulation: A Comprehensive Guide

Leave a Comment

×