3

Need to make the Keyboard's LED (Caps lock / Num lock or Scroll lock LED) blink either using C# or VB.net. (With or without using interop is fine)

Andy
  • 5,287
  • 2
  • 41
  • 45

1 Answers1

1

Here's the C way

#include <windows.h>
#define err if (GetLastError() != 0) return GetLastError();

extern "C" __declspec(dllexport) int __stdcall TurnLed(int state)
{
    DWORD tmp = 4;
    DWORD buf = (2*GetKeyState(VK_NUMLOCK) + 4*GetKeyState(VK_CAPITAL) + state)<<16;
    DefineDosDevice(DDD_RAW_TARGET_PATH, "Kbd", "\\Device\\KeyboardClass1"); err
    HANDLE kbd = CreateFile("\\\\.\\Kbd", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING,  FILE_ATTRIBUTE_NORMAL,  NULL); err
    DeviceIoControl(kbd, CTL_CODE(FILE_DEVICE_KEYBOARD, 0x0002, METHOD_BUFFERED, FILE_ANY_ACCESS), &buf, sizeof(buf), 0, 0, &tmp, 0); err
    DefineDosDevice(DDD_REMOVE_DEFINITION, "Kbd", 0); err
    CloseHandle(kbd); err
    return 0;
}
Poma
  • 8,174
  • 18
  • 82
  • 144
  • Do you know what permissions are required to do the ioctl on "\\.\Kbd"? Could this be done from a non-Administrator process? I don't have a Windows machine here to test this. – Ted Middleton Apr 15 '14 at 19:19
  • This seems to only work for PS/2 keyboards. – IS4 Mar 29 '22 at 19:44