How do I receive a numlock/capslock LED signal from the PC?

Status
Not open for further replies.

nonlinearmind

New member
I want to set up my Teensy 3.1 to run a function when it receives a caps lock led signal from the PC. The driver software I'm using (controller mate on the mac) sees the 3 leds on the teensy keyboard and can send a signal to activate one, but I haven't been able to figure out how to listen for it in my arduino sketch. Does anyone know how to do this?
 
You need access to the "keyboard_leds" variable that is part of the usb keyboard implementation (in both Teensy 2* and Teensy 3.*). I think that you can access it directly - there's no wrapper around it in the C++ class interface, but it is defined as an extern variable in the usb_keyboard.h header file which I believe will give you direct access to the variable itself.

This variable is set by the PC and sent back to the keyboard in the USB keyboard report. It is an 8-bit unsigned int, with the values:
// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
// 00000001 = num lock
// 00000010 = caps lock
// 00000011 = both num lock and caps lock, etc
I don't know what the "compose" or "kana" leds are. These values correspond to individual leds being turned on by particular bits.

To detect whether caps lock is on, it should be a simple check for whether the corresponding bit is set:
Code:
// Bit numbers for each led - used to make it easier later to know what you were actually testing for...
#define USB_LED_NUM_LOCK 0
#define USB_LED_CAPS_LOCK 1
#define USB_LED_SCROLL_LOCK 2
#define USB_LED_COMPOSE 3
#define USB_LED_KANA 4

// This part should go inside your code where you want to test the state of the caps lock led
if (keyboard_leds & (1<<USB_LED_CAPS_LOCK)) // checks that the caps lock bit is set
{
    // CapsLock is ON - put your "on" code here
}
else
{
    // CapsLock is OFF - put your "off" code here
}
 
This variable is set by the PC and sent back to the keyboard in the USB keyboard report. It is an 8-bit unsigned int, with the values:
// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
// 00000001 = num lock
// 00000010 = caps lock
// 00000011 = both num lock and caps lock, etc
I don't know what the "compose" or "kana" leds are. These values correspond to individual leds being turned on by particular bits.

They are both modeal modifier keys (as are caps lock and num lock). Compose is used for keyboards that have separate accent keys (so you press compose, type acute accent, type "e" and get "é" for example. Kana is used for Japanese keyboards, to switch between Latin characters and Kana characters:

800px-KB_Japanese.svg.png
 
Status
Not open for further replies.
Back
Top