How can I convert a string of "KEY_A" to the Keyboard.h KEY_A define value?

sofakng

Active member
I'm building a project that accepts commands over RS232 and then sends a keystroke over USB HID using the Teensy 3.2.

What is the most efficient way to accept any keystroke string and then convert it to the correct value for Keyboard.press() ?

Thanks for any help!
 
If you do it that way, you pretty much have to create some sort of lookup table in your program and search it.
 
Use Keyboard.print(char). It knows how to translate characters to the actual keystoke, based on the keyboard layout you selected in Tools > Keyboard Layout.

Code:
if (Serial.available()) {
  char c = Serial.read();
  Keyboard.print(c);
}
 
This thread inactive for a while but seems good starting point for me. I'm making a kbd/ptr sharing gizmo to be able to control chromebooks from linux computer (linux computer is raspberry pi for now but generic intention). I have C program on rpi to detect X events. send the X events to a teensy LC (with encoding of my scheme) and use the mouse.button mouse.move and keyboard.press/release to pass along X events to chromebook. Mouse is working fine but am still trying to understand all the nasty legacy of keyboards and scan codes. I don't want to use keyboard.print but rather keyboard.press and keyboard.release and am building the suggested lookup table. Main question for now, where are ALL the define's located for KEY_A, MODIFIERKEY_CTRL and all the rest? Doesn't seem to be in keyboard.h or HID.h.
thx
 
I _think_ I found the answer, it appears the relevant defines are in "../arduino-1.8.10/hardware/teensy/avr/cores/teensy/keylayouts.h" after teensy support is added. (I would guess that file is NOT present until teensy support added per pjrc.com how2s). The key codes (X events) on the linux side seemed even more cloaked in mystery for me (I'm not accustomed to digging into kernel and deep-X stuff). I eventually obtained those codes empirically by laboriously typing on keyboard and noting returned code. Anyway, I have my chromebook being remote controlled (mouse and keyboard) from a linux computer now (with Xevents being sent over TTL-serial link between rpi and teensy). I'll be refining the process to convert the necessary header files into a translation function (which currently runs on the rpi side) as I understand more about what the heck goes on in X or the kernel with key codes (scan codes??). If interested leave note here.
 
Back
Top