Bool GetKeyboardState(PBYTE lpKeyState)如:function GetNumState:String; var ks: TKeyboardState; begin GetKeyBoardState(ks); if odd(ks[VK_NUMLOCK])then Result := 'NUM'; end;
我发一个过程给你参考: procedure tform1.timer1timer(sender:tobject); begin statusbar1.panels[0].text:=timetostr(time); if getkeystate(vk_numlock)=1 then statusbar1.panels[1].text:='Num lock:ON' else statusbar1.panels[1].text:='Num lock:OFF'; if getkeystate(vk_capital)=1 then statusbar1.panels[2].text:='Caps lock:ON' else statusbar1.panels[2].text:='Caps lock:OFF'; end;
关键是这个:constructor TWinXP.Create(AOwner: TComponent); var I,Instances:Integer;begin inherited Create(AOwner); if not (AOwner is TForm) then raise Exception.Create('Control parent must be a form!'); Instances := 0; for I := 0 to Owner.ComponentCount - 1 do if (Owner.Components[I] is TWinXP) then Inc(Instances); if (Instances > 1) then raise Exception.Create('Only one instance of TWinXP allowed on form'); end;
GetKeyState(VK_LCONTROL)与GetKeyBoardState(ks)的区别是什么? 为什么我用 if odd(ks[VK_LCONTROL])then showmessage('VK_LCONTROL'); 时,总是成立,但我没有按下Control左键啊!!!
var
ks: TKeyboardState;
begin
GetKeyBoardState(ks);
if odd(ks[VK_NUMLOCK])then
Result := 'NUM';
end;
procedure tform1.timer1timer(sender:tobject);
begin
statusbar1.panels[0].text:=timetostr(time);
if getkeystate(vk_numlock)=1 then
statusbar1.panels[1].text:='Num lock:ON'
else
statusbar1.panels[1].text:='Num lock:OFF';
if getkeystate(vk_capital)=1 then
statusbar1.panels[2].text:='Caps lock:ON'
else
statusbar1.panels[2].text:='Caps lock:OFF';
end;
var
I,Instances:Integer;begin
inherited Create(AOwner);
if not (AOwner is TForm) then
raise Exception.Create('Control parent must be a form!'); Instances := 0;
for I := 0 to Owner.ComponentCount - 1 do
if (Owner.Components[I] is TWinXP) then Inc(Instances);
if (Instances > 1) then
raise Exception.Create('Only one instance of TWinXP allowed on form');
end;
为什么我用
if odd(ks[VK_LCONTROL])then showmessage('VK_LCONTROL');
时,总是成立,但我没有按下Control左键啊!!!
GetKeyState(VK_LCONTROL)
与
GetKeyBoardState(ks);
ks[VK_LCONTROL]结果应该是一样的,但开关键是以低位判断,其它键是以高位判断。
这段代码只是判断控件在窗体上是否合法放置而已,我找不到更多有用的代码了。
你们的代码都有用,但我想在KeyDown时有效获取状态好象不行,只有在平时或KeyUp时才有效。无论是控件的Keydown事件还是Application的WM_KEYDOWN消息中都不行。