I have tryed to use the ' KeyboardLogout ' on a ESP32-S2 and are not able to get the ' Keyboard.press(KEY) ' to work.
No erros and no action.
do you have an idear to make it work.
I have tryed to use the ' KeyboardLogout ' on a ESP32-S2 and are not able to get the ' Keyboard.press(KEY) ' to work.
No erros and no action.
do you have an idear to make it work.
Yup, use the 32U4 for which the code was written.
Use the ESP32-S2/S3 equivalent USB device examples.
I have tryed that.
But it only works some times with 'Keyboard.press()'
the 'Keyboard.print()' and 'Keyboard.println()' works evrey time.
Provide the ESP32 code that you tried, please.
#if ARDUINO_USB_MODE
#warning This sketch should be used when USB is in OTG mode
void setup(){}
void loop(){}
#else
#define WINDOWS 0
#define ANDROID 1
#include "USB.h"
#include "USBHIDKeyboard.h"
USBHIDKeyboard Keyboard;
// change this to match your platform:
int platform = ANDROID;
void setup() {
// make pin 0 an input and turn on the pull-up resistor so it goes high unless
// connected to ground:
pinMode(0, INPUT_PULLUP);
// LED
pinMode(LED_BUILTIN, OUTPUT);
Keyboard.begin();
USB.begin();
}
void loop() {
while (digitalRead(0) == HIGH) {
// do nothing until pin 2 goes low
delay(500);
}
delay(1000);
switch (platform) {
case WINDOWS:
// Start LED
digitalWrite(LED_BUILTIN, HIGH);
// CTRL-ALT-DEL:
Keyboard.press(KEY_LEFT_CTRL);
delay(100);
Keyboard.press(KEY_LEFT_ALT);
delay(100);
Keyboard.press(KEY_DELETE);
delay(100);
Keyboard.releaseAll();
// ALT-l:
delay(2000);
Keyboard.press(KEY_LEFT_ALT);
delay(100);
Keyboard.press('l');
Keyboard.releaseAll();
//Stop LED
digitalWrite(LED_BUILTIN, LOW);
break;
case ANDROID:
// Start LED
digitalWrite(LED_BUILTIN, HIGH);
// CTRL-ALT-DEL:
Keyboard.press(0x87);
delay(500);
Keyboard.release(0x87);
delay(500);
Keyboard.print("notepad");
delay(500);
Keyboard.press(0xB0);
delay(500);
Keyboard.release(0xB0);
delay(1000);
Keyboard.println("Hello World");
//Stop LED
digitalWrite(LED_BUILTIN, LOW);
break;
}
// do nothing:
while (true) delay(1000);
}
#endif /* ARDUINO_USB_MODE */
I'm trying to get it to work with exactly the same code on a Lolin S3.
No errors but not working. Should I ground to pin 2 on this board too?
Sometimes you must ignore the comment and look only at the code because the comment is wrong. Try grounding pin 0.
I got it a bit working on my ESP32-USB-OTG (Lolin S3) but it's doing it once. After that it doesn't work anymore no matter how long I wait or press the button.
It works after reboot again but just one time.
Any idea?
#define OSX 0
#define WINDOWS 1
#define UBUNTU 2
#include "USB.h"
#include "USBHIDKeyboard.h"
USBHIDKeyboard Keyboard;
// change this to match your platform:
int platform = OSX;
void setup() {
// make pin 2 an input and turn on the pull-up resistor so it goes high unless
// connected to ground:
pinMode(0, INPUT_PULLUP);
Keyboard.begin();
USB.begin();
}
void loop() {
while (digitalRead(0) == HIGH) {
// do nothing until pin 2 goes low
delay(500);
}
delay(1000);
switch (platform) {
case OSX:
Keyboard.press(KEY_LEFT_GUI);
// Shift-Q logs out:
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press('Q');
delay(100);
Keyboard.releaseAll();
// enter:
Keyboard.write(KEY_RETURN);
break;
case WINDOWS:
// CTRL-ALT-DEL:
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_DELETE);
delay(100);
Keyboard.releaseAll();
// ALT-l:
delay(2000);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press('l');
Keyboard.releaseAll();
break;
case UBUNTU:
// CTRL-ALT-DEL:
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_DELETE);
delay(1000);
Keyboard.releaseAll();
// Enter to confirm logout:
Keyboard.write(KEY_RETURN);
break;
}
// do nothing:
while (true)
;
}
Look at the code. The while loop at the end of the program locks up the program in an infinite loop after one key press. Consider removing it.
thank you very much! It seems to be working fine now.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.