如何有效控制键盘状态, 如数字键Num Lock?(100分)

  • 主题发起人 主题发起人 chnplzh
  • 开始时间 开始时间
C

chnplzh

Unregistered / Unconfirmed
GUEST, unregistred user!
以下语句在WIN98系统执行正常, 为什么在WIN2000不能正常运行设置呢?

function SetNumLock(vNumLock:boolean):boolean; //获取或设置键盘状态
function GetNumLock():boolean;

function SetNumLock(vNumLock:boolean):boolean;
Var KeyState : TKeyBoardState;
begin
GetKeyboardState(KeyState);
result:=GetNumLock();
if vNumLock then
KeyState[VK_NUMLOCK]:=1
else
KeyState[VK_NUMLOCK]:=0;
SetKeyboardState(KeyState);
end;

function GetNumLock():boolean;
Var KeyState : TKeyBoardState;
begin
GetKeyboardState(KeyState);
if KeyState[VK_NUMLOCK]=1 then
result := True
else
result:= False;
end;
[?][?]
 
procedure SetCapsLockKey(vcode: Integer; down: Boolean);
begin
if Odd(GetAsyncKeyState(vcode)) <> down then
begin
keybd_event(vcode, MapVirtualkey(vcode, 0),
KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(vcode, MapVirtualkey(vcode, 0),
KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
SetCapsLockKey(VK_NUMLOCK, True);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
SetCapsLockKey(VK_NUMLOCK, False);
end;
 
来自MSDN:
Because the SetKeyboardState function alters the input state of the calling
thread and not the global input state of the system, an application cannot use
SetKeyboardState to set the num lock, caps lock, or scroll lock indicator
lights on the keyboard.

 
不错,非常谢谢!
 

Similar threads

后退
顶部