以最少的资源耗用,显示系统键状态

有不少人讨论过显示系统键(CapsLock、NumLock、Insert、ScrollLock等)状态的问题,用得最多的方法是添加一个Timer,在事件中刷新系统键状态显示;另一个方法是做一个系统HOOK,在HOOK中刷新显示。这两种方式都会严重占用系统资源,第一种方法还存在延迟的问题。现在介绍第三种方法:

键盘按钮被按下时系统会发送WM_KEYDOWN、WM_CHAR、WM_KEYUP消息给当前的激活应用程序,消息的wParam是键盘扫描码,这样我们就可以知道按键是否被按下或释放,在这里面刷新按键状态显示是最佳的时候。不过这里还有一个问题,应用程序在非激活状态时是收不到以上消息的,因此需要在程序被激活时检测并刷新状态显示。

下面给出实现代码:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls;

type
TForm1 = class (TForm)
StatusBar1: TStatusBar;
procedure FormCreate(Sender: TObject);
private
procedure AppActivate(Sender: TObject);
procedure AppOnMsg(var Msg: tagMSG; var Handled: Boolean);
procedure ShowKeyState; _ //显示系统按键状态 _

end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
_ //显示系统键状态,指定事件代理
_ ShowKeyState;
Application.OnMessage := AppOnMsg;
Application.OnActivate := AppActivate;
end;

procedure TForm1.AppActivate(Sender: TObject);
begin
ShowKeyState; _ //应用程序被激活时刷新系统键状态
_ end;

procedure TForm1.ShowKeyState;
begin
if (GetKeyState(145) and 1)<>0 then
StatusBar1.Panels.Items[6].Text := ' SCOR '
else
StatusBar1.Panels.Items[6].Text := '';
if (GetKeyState(144) and 1)<>0 then
StatusBar1.Panels.Items[5].Text := ' NUM '
else
StatusBar1.Panels.Items[5].Text := '';
if (GetKeyState(45) and 1)<>0 then
StatusBar1.Panels.Items[4].Text := ' 插入 '
else
StatusBar1.Panels.Items[4].Text := ' 覆盖 ';
if (GetKeyState(20) and 1)<>0 then
StatusBar1.Panels.Items[3].Text := ' 大写 '
else
StatusBar1.Panels.Items[3].Text := ' 小写 ';
end;

procedure TForm1.AppOnMsg( var Msg: tagMSG; var Handled: Boolean);
begin
if Msg.message = 256 then begin _ //WM_KEYDOWN
_ case Msg.wParam of _ //根据按键切换显示
_ 145: _ //Scorll
_ if (GetKeyState(145) and 1)<>0 then
StatusBar1.Panels.Items[6].Text := ' SCOR '
else
StatusBar1.Panels.Items[6].Text := '';
144: _ //Num
_ if (GetKeyState(144) and 1)<>0 then
StatusBar1.Panels.Items[5].Text := ' NUM '
else
StatusBar1.Panels.Items[5].Text := '';
45: //Ins
if (GetKeyState(45) and 1)<>0 then
StatusBar1.Panels.Items[4].Text := ' 插入 '
else
StatusBar1.Panels.Items[4].Text := ' 覆盖 ';
20: _//Caps
_ if (GetKeyState(20) and 1)<>0 then
StatusBar1.Panels.Items[3].Text := ' 大写 '
else
StatusBar1.Panels.Items[3].Text := ' 小写 ';
**end;
end; **
Handled := false; _//让系统继续处理消息
_ end;

**end.
**

Published At
Categories with Web编程
Tagged with
comments powered by Disqus