下面的程式片段會切換 CapsLock, NumLock 及 ScrollLock 的鍵盤狀態 BCB // KeyState 是一個 256 bytes 的鍵盤狀態陣列, 每個元素其值為 0 或 1 PBYTE KeyState = new BYTE [256]; GetKeyboardState(KeyState); KeyState[VK_CAPITAL] = !KeyState[VK_CAPITAL]; KeyState[VK_NUMLOCK] = !KeyState[VK_NUMLOCK]; KeyState[VK_SCROLL] = !KeyState[VK_SCROLL]; SetKeyboardState(KeyState); DELPHI //設定鍵盤燈號並傳回上次的值 function KeyLockOn(nVirtKey: Integer; bLockIt: Boolean): Boolean; var KeyState: TKeyboardState; begin if not GetKeyboardState(KeyState) then exit; Result := (KeyState[nVirtKey] = 1); if bLockIt then KeyState[nVirtKey] := 1 else KeyState[nVirtKey] := 0; SetKeyboardState(KeyState); end; // 設定 Insert 狀態並傳回上次的設定值 function InsLock(bLockIt: boolean): Boolean; begin Result := KeyLockOn(VK_INSERT, bLockIt); end; // 設定 CapsLock 並傳回上次的設定值 function CapsLock(bLockIt: boolean): Boolean; begin Result := KeyLockOn(VK_CAPITAL, bLockIt); end; // 設定 NumLock 並傳回上次的設定值 function NumLock(bLockIt: boolean): Boolean; begin Result := KeyLockOn(VK_NUMLOCK, bLockIt); end; // 設定 ScrollLock 並傳回上次的設定值 function ScrollLock(bLockIt: boolean): Boolean; begin Result := KeyLockOn(VK_SCROLL, bLockIt); end;