3

需要使用 C# 或 VB.net 使键盘的 LED(Caps lock / Num lock 或 Scroll lock LED)闪烁。(使用或不使用互操作都可以)

4

1 回答 1

1

这是C方式

#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;
}
于 2013-07-27T23:39:24.290 回答