2016-08-19 81 views
0

這裏一些Linux來的代碼,例如:我如何開啓/關閉大寫鎖定,滾動鎖定,Num Lock鍵編程

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); 
} 

我怎樣才能做這樣的事情在Windows(C++ \ C) ?我有一種感覺,keybd_event不接近我。 在Windows上打開/關閉Caps Lock,Scroll Lock和Num Lock LEDs有沒有簡單的方法?

+1

快速谷歌搜索發現這個古老的微軟網頁。它有VB6的說明,但你應該能夠理解它:https://support.microsoft.com/en-us/kb/177674 –

+0

從'keybd_event'文檔的頂部:*注意此功能有已被取代。代替使用SendInput。*事實上,'SetKeyboardState'解釋說'SendInput'是正確的方法。 – chris

+0

seq_activate [3] = {3,5,4}; // 3 - 大寫鎖定和滾動鎖定,5 - 滾動鎖定和數字鎖定\\ 在SendInput中,據我所知,不能同時發送命令以按Scroll Lock和Caps Lock。只有第一個Scroll,然後纔是Caps。我可能是錯的 – MaysSpirit

回答

0

下面的代碼,我發現: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]); 
    } 
} 
相關問題