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.
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.
解决方案 »
- 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 delphi + 面向之 重载,覆盖,继承,虚方法,动态方法,多态,封装,接口
- dephi socket编程
- 请问:怎样实现这样的功能?
- zzlazio...先给你100分.原贴的要分些给其他跟帖的兄弟.
- MDI方式中使用ActionMainMenuBar的问题
- 紧急求就`~ 哪里有delphi6,5等等自带的报表控件下载呢?
- D7如何调用其他的应用程序,急等。。。。
- 怎么把选中的多个图像文件装载入IMAGE控件。
- 请教Express - Borland Limited Edition的用法?
- SQlserver数据库的问题
- 高分赠送!如何格式化输出一定的文字?
- 问一些比较简单数据库的问题,我很菜,sql server还不会呢。
add是添加热键以及处理函数。
delete是删除。
不过一般formcreate的时候create然后add。formclose的时候free就够了。
很好用。
这样应用范围会广泛些。
但是我想写成TWidGetControl也可以。
看来用overload吧
TControl好像没有OnKeyDown
因为keypreview;别的控件就用他自己提供的onkeydown吧。
//热键处理过程
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;
///////////////////////
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