The 256-byte array that receives the status data for each virtual key. KeyBoardState 这个数组接收虚拟键的状态表参考下面代码:procedure TForm1.Button1Click(Sender: TObject); var KeyState: TKeyBoardState; begin GetKeyboardState(KeyState); //读取VK_开头的虚拟键 if (Win32Platform = VER_PLATFORM_WIN32_NT) then begin if boolean(KeyState[VK_NUMLOCK]) then // 读取状态 begin keybd_event(VK_NUMLOCK, MapVirtualKey(VK_NUMLOCK, 0), KEYEVENTF_EXTENDEDKEY, 0); keybd_event(VK_NUMLOCK, MapVirtualKey(VK_NUMLOCK, 0), KEYEVENTF_EXTENDEDKEY OR KEYEVENTF_KEYUP, 0); end else begin keybd_event(VK_NUMLOCK, MapVirtualKey(VK_NUMLOCK, 0), KEYEVENTF_EXTENDEDKEY, 0); keybd_event(VK_NUMLOCK, MapVirtualKey(VK_NUMLOCK, 0), KEYEVENTF_EXTENDEDKEY OR KEYEVENTF_KEYUP, 0); end; end else begin if boolean(KeyState[VK_NUMLOCK]) then begin KeyState[VK_NUMLOCK]:= $0; SetKeyboardState(KeyState); // 这个函数应用在旧平台 end else begin KeyState[VK_NUMLOCK]:= $1; SetKeyboardState(KeyState); end; end; end;
虛擬鍵的狀態Procedure SendKeyDown(VKey: Byte; NumTimes : Word; GenUpMsg : Boolean); var Cnt : Word; ScanCode : Byte; NumState : Boolean; KeyBoardState : TKeyboardState; begin If (VKey=VK_NUMLOCK) then begin NumState:=ByteBool(GetKeyState(VK_NUMLOCK) and 1); GetKeyBoardState(KeyBoardState); If NumState then KeyBoardState[VK_NUMLOCK]:=(KeyBoardState[VK_NUMLOCK] and not 1) else KeyBoardState[VK_NUMLOCK]:=(KeyBoardState[VK_NUMLOCK] or 1); SetKeyBoardState(KeyBoardState); exit; end; ScanCode:=Lo(MapVirtualKey(VKey,0)); For Cnt:=1 to NumTimes do If (VKey in ExtendedVKeys)then begin KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY); If (GenUpMsg) then KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP) end else begin KeyboardEvent(VKey, ScanCode, 0); If (GenUpMsg) then KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP); end; end;
KeyBoardState 这个数组接收虚拟键的状态表参考下面代码:procedure TForm1.Button1Click(Sender: TObject);
var
KeyState: TKeyBoardState;
begin
GetKeyboardState(KeyState); //读取VK_开头的虚拟键
if (Win32Platform = VER_PLATFORM_WIN32_NT) then
begin
if boolean(KeyState[VK_NUMLOCK]) then // 读取状态
begin
keybd_event(VK_NUMLOCK, MapVirtualKey(VK_NUMLOCK, 0), KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_NUMLOCK, MapVirtualKey(VK_NUMLOCK, 0), KEYEVENTF_EXTENDEDKEY OR KEYEVENTF_KEYUP, 0);
end else
begin
keybd_event(VK_NUMLOCK, MapVirtualKey(VK_NUMLOCK, 0), KEYEVENTF_EXTENDEDKEY, 0);
keybd_event(VK_NUMLOCK, MapVirtualKey(VK_NUMLOCK, 0), KEYEVENTF_EXTENDEDKEY OR KEYEVENTF_KEYUP, 0);
end;
end else
begin
if boolean(KeyState[VK_NUMLOCK]) then
begin
KeyState[VK_NUMLOCK]:= $0;
SetKeyboardState(KeyState); // 这个函数应用在旧平台
end else
begin
KeyState[VK_NUMLOCK]:= $1;
SetKeyboardState(KeyState);
end;
end;
end;
var
Cnt : Word;
ScanCode : Byte;
NumState : Boolean;
KeyBoardState : TKeyboardState;
begin
If (VKey=VK_NUMLOCK) then begin
NumState:=ByteBool(GetKeyState(VK_NUMLOCK) and 1);
GetKeyBoardState(KeyBoardState);
If NumState then KeyBoardState[VK_NUMLOCK]:=(KeyBoardState[VK_NUMLOCK] and not 1)
else KeyBoardState[VK_NUMLOCK]:=(KeyBoardState[VK_NUMLOCK] or 1);
SetKeyBoardState(KeyBoardState);
exit;
end; ScanCode:=Lo(MapVirtualKey(VKey,0));
For Cnt:=1 to NumTimes do
If (VKey in ExtendedVKeys)then begin
KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY);
If (GenUpMsg) then
KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP)
end else begin
KeyboardEvent(VKey, ScanCode, 0);
If (GenUpMsg) then KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP);
end;
end;