请教:如何在代码中控制数字小键盘的状态 - C++ Builder / Windows SDK/API
即如何改变Num Lock的状态?多谢!
[解决办法]
void SIKeyDown(WORD Key){// 按下一个键INPUT input[1];memset(input, 0, sizeof(input));input[0].type = INPUT_KEYBOARD;input[0].ki.wVk = Key;input[0].ki.wScan = 0;input[0].ki.dwFlags = 0;input[0].ki.time = GetTickCount();SendInput(1,input,sizeof(INPUT));}//-------------------------------------void SIKeyUp(WORD Key){//松开一个键INPUT input[1];memset(input, 0, sizeof(input));input[0].type = INPUT_KEYBOARD;input[0].ki.wVk = Key;input[0].ki.wScan = 0;input[0].ki.dwFlags = KEYEVENTF_KEYUP;input[0].ki.time = GetTickCount();SendInput(1,input,sizeof(INPUT));}//----------------------------------------void SIKeyPress(WORD Key,int Interval){// 按下并松开一个按键SIKeyDown(Key);if(Interval>0) Sleep(Interval);SIKeyUp(Key);}void __fastcall TForm1::Button1Click(TObject *Sender){ SIKeyPress(0x90 ,500); //0x90就是 NUM LOCK 键 的 键值}
[解决办法]
不要把简单的事情复杂化。
void __stdcall CrnSetNumLock(bool bState){ BYTE btKeyState[256]; ::GetKeyboardState((LPBYTE)&btKeyState); if ((bState && !(btKeyState[VK_NUMLOCK] & 1)) || (!bState && (btKeyState[VK_NUMLOCK] & 1))) { keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0); keybd_event(VK_NUMLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); }}void __fastcall TForm1::CheckBox1Click(TObject *Sender){ CrnSetNumLock(CheckBox1->Checked);}
[解决办法]
经过测试老妖的代码和我得都是正常的,把你的代码贴出来看看
[解决办法]