2023年12月14日发(作者:)

Delphi 可以调用 Win API 的 Getkeyboardstate() 函数。

常量 按键名称

VK_INSERT Insert 键

VK_NUMLOCK Num Lock 键

VK_CAPITAL Caps Lock 键

VK_SCROLL Scroll Lock 键

键盘缓冲区每一位都有一位特定的格式,对于状态键来说,最低位是1时表示状态键处于 ON 状态,你可以使用 odd() 函数来确定这一位的状态,以下是一个简单的例子请参考。例子中放置一个 Timer 控件、一个 StayusBar1 状态条。

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, ComCtrls;

type

TForm1 = class(TForm)

StatusBar1: TStatusBar;

Timer1: TTimer;

procedure Timer1Timer(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation {$R *.DFM}

procedure 1Timer(Sender: TObject);

var

ks:tkeyboardstate;

begin

getkeyboardstate(ks); //检测键盘函数

if odd(ks[VK_NUMLOCK]) then [0].text:='NUM'

else [0].text:='';

if odd(ks[VK_INSERT]) then [1].text:='INSERT'

else [1].text:='';

if odd(ks[VK_CAPITAL]) then [2].text:='CAPITAL'

else [2].text:='' ;

if odd(ks[VK_SCROLL]) the [3].text:='SCROLL'

else [3].text:='';

end;

end.