unit HotKey2_San;interface
uses
windows,sysutils,messages,classes,forms;
type
TKeyEvent_San=procedure (Sender: TObject; var Key: Word; Shift: TShiftState) of object;
THotKey2Rec=class
private
enabled:boolean;
publicSync:boolean;
Shift:TShiftState;
Key:Word;
MethodProc:TNotifyevent;
constructor Create(AShift:TshiftState;AKey:Word;AMethodProc:TNotifyEvent;ASync:boolean);end;///////////////
THotKey2_san=class
private
OldProc:TKeyEvent_san;
protected
List:TList;
procedure DeleteAll;
function FindItem(AShift:TShiftState;AKey:Word):THotKey2Rec;
//////////////
procedure OnFormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
public
procedure DeleteHotKey(AShift:TshiftState;AKey:Word);
function AddHotKey(AShift:TshiftState;AKey:Word;AMethodProc:TNotifyEvent;ASync:boolean=true):THotKey2Rec;
//////////
constructor Create(AForm:TForm);
Destructor Destroy;override;
end;implementation{ THotKey2_san }function THotKey2_san.AddHotKey(AShift: TshiftState; AKey: Word;AMethodProc:TNotifyEvent;ASync:boolean):THotKey2Rec;
var
t:thotkey2rec;
begin
t:=thotkey2rec.Create(ashift,akey,amethodproc,async);
list.Add(t);
result:=t;
end;constructor THotKey2_san.Create(AForm:TForm);
begin 
inherited create;
aform.KeyPreview:=true;
oldproc:=aform.OnKeyDown;
aform.OnKeyDown:=OnFormKeyDown;
///////
list:=tlist.Create;
end;procedure THotKey2_san.DeleteAll;
var
t:thotkey2rec;
begin
while list.Count>0 do
begin
t:=thotkey2rec(list.Items[0]);
try
list.Delete(list.IndexOf(t));
finally
t.Free;
t:=nil;
end; 
end;
end;procedure THotKey2_san.DeleteHotKey(AShift: TshiftState; AKey: Word);
var
t:thotkey2rec;
begin
t:=finditem(ashift,akey);
if t=nil then exit;
try
list.Delete(list.IndexOf(t));
finally
t.Free;
t:=nil;
end; 
end;destructor THotKey2_san.Destroy;
begin
try
deleteall;
list.Free;
finally
inherited;
end;
end;function THotKey2_san.FindItem(AShift: TShiftState;
AKey: Word): THotKey2Rec;
var
i:integer;
t:thotkey2rec;
begin
result:=nil;
for i:=0 to list.Count-1 do
begin
t:=thotkey2rec(list.Items[i]);
if (t.Shift=ashift) and(t.Key=akey) then
begin
result:=t;
break;
end;
end;
end;procedure THotKey2_san.OnFormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
var
t:thotkey2rec;
setted:boolean;
begin
setted:=false;
t:=finditem(shift,key);
try
if (t<>nil)and(t.enabled) then
begin
if t.Sync then
begin
t.enabled:=false;
setted:=true;
end;
t.MethodProc(t);
if setted then
begin
t.enabled:=true;
end;
end;
finally
if assigned(oldproc) then
oldproc(sender,key,shift);
inherited;
end;
end;{ THotKey2Rec }constructor THotKey2Rec.Create(AShift: TshiftState; AKey: Word;
AMethodProc: TNotifyEvent;ASync:boolean);
begin
enabled:=true;
///////////
sync:=async;
shift:=ashift;
key:=akey;
methodproc:=amethodproc;
end;end.

解决方案 »

  1.   

    给你个类,很好用。
    add是添加热键以及处理函数。
    delete是删除。
    不过一般formcreate的时候create然后add。formclose的时候free就够了。
    很好用。
      

  2.   

    把Ctrl+S设置为热键,就象你上面的例子,把对象的热键设置为Ctrl+S,执行的动作在对象事件中声明即可。不过对象必须可以设置热键才可以。假如不行的可以把form的keypreview设置为true,然后在onkeydown事件中判断
      

  3.   

    Stanely的想法是不错的。建议你把THotKey2_san的Create 的aowner改成TWincontrol
    这样应用范围会广泛些。
      

  4.   

    恩,是的
    但是我想写成TWidGetControl也可以。
    看来用overload吧
    TControl好像没有OnKeyDown
      

  5.   

    不行,还是用TForm吧,好些。
    因为keypreview;别的控件就用他自己提供的onkeydown吧。
      

  6.   

    试试这段代码:private
    //热键处理过程
        procedure Onhotkey(var message: tmessage);message wm_hotkey;procedure TForm1.FormCreate(Sender: TObject);
    begin
    registerhotkey(form1.Handle,1000,MOD_CONTROL,$53 );
    end;
    procedure tform1.Onhotkey(var message:tmessage);
    begin
    //处理热键消息
      case message.WParam of
        1000:showmessage('ctrl+s');
      end;
    end;procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    begin
    //注销注册的热键
      UnregisterHotKey(form1.Handle,1000);
    end;
      

  7.   

    //用我类的例子:
    ///////////////////////
    unit Unit1;interfaceuses
      Windows,hotkey2_san, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure aaa(sender:tobject);
      end;var
      Form1: TForm1;
      hk:thotkey2_san;
    implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      hk:=thotkey2_san.Create(self);
      hk.AddHotKey([ssctrl],ord('S'),aaa);//一定要大写'S'!
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      hk.Free;
    end;procedure TForm1.aaa(sender: tobject);
    begin
      showmessage('Ctrl+S');
    end;end.
    ///////////win2000pro+delphi6