有时在进入程序后,我们需要设键盘输入为大写(Caps Lock)或(Num Lock)数字键盘时,
可以利用代码代劳。
/*
Set the Capslock, Numlock, Scrolllock on/off
--------------------------------------------
key : 'C'=Cap Lock, 'N'=Num Lock, 'S'=Scroll Lock
bState : TRUE=on, FALSE=off
*/
void SetKeyLock(char key, BOOL bState)
{
BYTE vk_key;
switch(key)
{
case 'C':
case 'c': vk_key=VK_CAPITAL; //Caps Lock
break;
case 'N':
case 'n': vk_key=VK_NUMLOCK; //Num Lock
break;
case 'S':
case 's': vk_key=VK_SCROLL; //Scroll Lock
break;
default: return; //ignore other keys
}
BYTE keyState[256];
GetKeyboardState((LPBYTE)&keyState);
if( (bState && !(keyState[vk_key] & 1)) ||
(!bState && (keyState[vk_key] & 1)) )
{
// Simulate a key press
keybd_event( vk_key,
0x45,
KEYEVENTF_EXTENDEDKEY | 0,
0 );
// Simulate a key release
keybd_event( vk_key,
0x45,
KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP,
0);
}
}
用法:
SetKeyLock('C',TRUE); //Set Caps lock on
SetKeyLock('N',TRUE); //Set Num lock on
SetKeyLock('S',FALSE); //Set Scroll off
注:这个函数只适合Windows系统。
阅读(2280) | 评论(0) | 转发(0) |