Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

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

  1. #1

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

    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?

  2. #2
    Member
    Join Date
    Jan 2014
    Location
    London, UK
    Posts
    76
    This is a good question, the USB keyboard emulation appears to be one way.

  3. #3
    Member Dawnmist's Avatar
    Join Date
    Nov 2012
    Location
    Australia
    Posts
    51
    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
    }

  4. #4
    Thanks! Exactly what I needed!

  5. #5
    Senior Member
    Join Date
    Nov 2012
    Location
    Boston, MA, USA
    Posts
    1,108
    Quote Originally Posted by Dawnmist View Post
    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:


  6. #6
    Junior Member
    Join Date
    Jul 2019
    Posts
    11
    I know it's an old thread, but is this possible with other keys as well?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •