1
votes

Here some come code in linux for example:

void set_leds(int val) 
{
 int fd = open ("/dev/console", O_WRONLY);
 // argument (Bit 0 - Scroll Lock, Bit 1 - Num Lock, Bit 2 - Caps lock)
 ioctl (fd, KDSETLED, val);
 close(fd);
}
void set_leds_sequence(unsigned char * cmdSeq, int len)
{
 int i;
 for (i = 0; i < len; ++i)
 {
   set_leds( cmdSeq[i] );
 }
}
void activate(void)
{
 unsigned char seq_activate[3]  = {3, 5, 4};
 set_leds_sequence(seq_activate, 3);
}

How can i do something like that on Windows (C++\C)? I have a feeling that the keybd_event does not approach me. Is there a simple method to turn on/off Caps Lock, Scroll Lock and Num Lock LEDS on Windows?

1
a quick google search revealed this ancient microsoft web page. it has instructions for VB6, but you should be able to make sense of it: support.microsoft.com/en-us/kb/177674 - Richard Hodges
From the top of the keybd_event docs: Note This function has been superseded. Use SendInput instead. And indeed, SetKeyboardState explains that SendInput is the proper way to do it. - chris
seq_activate[3] = {3, 5, 4}; //3 - Caps lock & Scroll Lock on, 5 - Scroll Lock & Num Lock on\\ In the SendInput, as I understood, can't at the same time send a command to press Scroll Lock and Caps Lock. Only the first Scroll, and then only to the Caps. I may be wrong - MaysSpirit

1 Answers

1
votes

Here code i find: https://github.com/ftk/kbled/blob/master/keyboard.c

#define IOCTL_KEYBOARD_SET_INDICATORS        CTL_CODE(FILE_DEVICE_KEYBOARD, 0x0002, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define IOCTL_KEYBOARD_QUERY_INDICATORS      CTL_CODE(FILE_DEVICE_KEYBOARD, 0x0010, METHOD_BUFFERED, FILE_ANY_ACCESS)   

static HANDLE kbd;
using namespace std;

void OpenKeyboardDevice()
{
    if (!DefineDosDevice(DDD_RAW_TARGET_PATH, L"Kbd000000",
        L"\\Device\\KeyboardClass0"))
    {
        assert(false);
    }

    kbd = CreateFile(L"\\\\.\\Kbd000000", GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL, OPEN_EXISTING, 0, NULL);
    assert(kbd);
}


void CloseKeyboardDevice()
{
    DefineDosDevice(DDD_REMOVE_DEFINITION, L"Kbd000000", NULL);
    CloseHandle(kbd);
}



int set_leds(int led)
{
    uint32_t input = 0;
    DWORD len;
    input |= led << 16;
    if (!DeviceIoControl(kbd, IOCTL_KEYBOARD_SET_INDICATORS,
        &input, sizeof(input),
        NULL, 0,
        &len, NULL))
        return GetLastError();

    return 0;
}

void set_leds_sequence(unsigned char * cmdSeq, int len)
{
    int i;
    for (i = 0; i < len; ++i)
    {
        set_leds(cmdSeq[i]);
    }
}