快捷键(30分)

L

ldelphi

Unregistered / Unconfirmed
GUEST, unregistred user!
我为我的软件 A 设置了全局快捷键。当我运行 A 时,无论 A 是否获得焦点(在窗口的最前台),只要按了我预先定义的快捷键后, A 都会响应(很讨厌)。能否使得:当 A 是焦点时才响应;不是焦点时,不响应。以下是我设置全局快捷键的步骤:

1.在 type 加入以下代码
procedure hotykey(var msg:TMessage); message WM_HOTKEY;
2.在 var 加入以下代码
idXmjc, idXm, idQq, idEmail, idYddh, idJtdh, idSsdh, idDhtUp, idDhtDown: Integer;
3.在 TForm1.FormCreate 加入以下代码
idXmjc:=GlobalAddAtom('hotkey');
RegisterHotKey(handle,idXmjc,mod_Shift,67);
idXm:=GlobalAddAtom('hotkey2');
RegisterHotKey(handle,idXm,mod_Shift,88);
idQq:=GlobalAddAtom('hotkey3');
RegisterHotKey(handle,idQq,mod_Shift,81);
idEmail:=GlobalAddAtom('hotkey4');
RegisterHotKey(handle,idEmail,mod_Shift,69);
idYddh:=GlobalAddAtom('hotkey5');
RegisterHotKey(handle,idYddh,mod_Shift,89);
idJtdh:=GlobalAddAtom('hotkey6');
RegisterHotKey(handle,idJtdh,mod_Shift,74);
idSsdh:=GlobalAddAtom('hotkey7');
RegisterHotKey(handle,idSsdh,mod_Shift,83);
idDhtUp:=GlobalAddAtom('hotkey8');
RegisterHotKey(handle,idDhtUp,mod_Shift,VK_Up);
idDhtDown:=GlobalAddAtom('hotkey9');
RegisterHotKey(handle,idDhtDown,mod_Shift,VK_Down);
4.在 TForm1.FormDestroy 加入以下代码
UnRegisterHotKey(handle,idXmjc);
UnRegisterHotKey(handle,idXm);
UnRegisterHotKey(handle,idQq);
UnRegisterHotKey(handle,idEmail);
UnRegisterHotKey(handle,idYddh);
UnRegisterHotKey(handle,idJtdh);
UnRegisterHotKey(handle,idSsdh);
UnRegisterHotKey(handle,idDhtUp);
UnRegisterHotKey(handle,idDhtDown);
5.在 MainForm 加入以下代码
procedure TForm1.hotykey(var msg:TMessage);
begin
if (msg.LParamLo=MOD_Shift) then
begin
case msg.LParamHi of
67 : cbSslx.ItemIndex := 0;
88 : cbSslx.ItemIndex := 1;
81 : cbSslx.ItemIndex := 2;
69 : cbSslx.ItemIndex := 3;
89 : cbSslx.ItemIndex := 4;
74 : cbSslx.ItemIndex := 5;
83 : cbSslx.ItemIndex := 6;
VK_Up : Table1.Prior;
VK_Down : Table1.next;
end;

edtSr.Text := '';
ActiveControl := edtSr;
end;
end;
 
你的情况并不需要全局快捷键呵
你可以在主窗口上建一个PopupMenu
设其为不可见
在里面定义一个快捷键就可以了
 
to vine
我都觉得不需要全局快捷键呵,但我不会做其他的,能详细点吗?^_^thanks
 
做一个TAction(放一个ActionList,加一个动作),指定快捷键就是了.然后按钮或菜单使用这个动作
 

发觉lynu的方法比我的还好
在mainform上放一个TActionList
加一个Action,设置其快捷键为你所需要的
然后在onExecute写你需要的代码就是了
如:
unit Unit1;

interface

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

type
TForm1 = class(TForm)
ActionList1: TActionList;
Action1: TAction;
procedure Action1Execute(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Action1Execute(Sender: TObject);
begin
showmessage('OK');
end;

end.
 
to lynu
你的方法的确不错,但ActionList好像没有我的快捷键(shift+A等)
 
将窗体的KeyPreView属性设为TRUE;
在ONKEYDOWN中判断.
 
to coolcat
我感觉你的方法是可以的,能不能详细点。按照你的方法做了,不过不行,你帮我看看啦

procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
var
msg:TMessage;
begin
if (msg.LParamLo=MOD_Shift) then
begin
case msg.LParamHi of
67 : cbSslx.ItemIndex := 0;
88 : cbSslx.ItemIndex := 1;
81 : cbSslx.ItemIndex := 2;
69 : cbSslx.ItemIndex := 3;
89 : cbSslx.ItemIndex := 4;
74 : cbSslx.ItemIndex := 5;
83 : cbSslx.ItemIndex := 6;
VK_Up : Table1.Prior;
VK_Down : Table1.next;
end;

edtSr.Text := '';
ActiveControl := edtSr;
end;
end;
 
多人接受答案了。
 
顶部