delphi – 当我的程序不活动时,如何处理键盘快捷键?
发布时间:2020-12-15 09:22:18 所属栏目:大数据 来源:网络整理
导读:如果我像这样使用它,可以用于多个事件吗? unit Unit4;interfaceuses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,Clipbrd;type TForm4 = class(TForm) procedure FormCreate(Sender: TObject); procedure WMHotkey(var Me
如果我像这样使用它,可以用于多个事件吗?
unit Unit4; interface uses Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,Clipbrd; type TForm4 = class(TForm) procedure FormCreate(Sender: TObject); procedure WMHotkey(var Message: TWMHotKey); message WM_HOTKEY; procedure FormDestroy(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form4: TForm4; implementation const MY_ID = 123; MY_ID1 = 123; MY_ID2 = 123; {$R *.dfm} procedure TForm4.FormCreate(Sender: TObject); begin RegisterHotKey(Handle,MY_ID,MOD_CONTROL,ord('1')); RegisterHotKey(Handle,MY_ID1,ord('2')); RegisterHotKey(Handle,MY_ID2,ord('3')); end; procedure TForm4.FormDestroy(Sender: TObject); begin UnregisterHotKey(Handle,MY_ID); UnregisterHotKey(Handle,MY_ID1); UnregisterHotKey(Handle,MY_ID2); end; procedure TForm4.WMHotkey(var Message: TWMHotKey); begin if Message.HotKey = MY_ID then begin if not AttachThreadInput(GetCurrentThreadId,GetWindowThreadProcessId(GetForegroundWindow),true) then RaiseLastOSError; try Clipboard.AsText := 'text1'; SendMessage(GetFocus,WM_PASTE,0); finally AttachThreadInput(GetCurrentThreadId,false); end; if Message.HotKey = MY_ID1 then begin if not AttachThreadInput(GetCurrentThreadId,true) then RaiseLastOSError; try Clipboard.AsText := 'text2'; SendMessage(GetFocus,false); end; if Message.HotKey = MY_ID2 then begin if not AttachThreadInput(GetCurrentThreadId,true) then RaiseLastOSError; try Clipboard.AsText := 'text3'; SendMessage(GetFocus,false); end; end; end; end; end; end. 解决方法
使用
RegisterHotKey 功能.如果您希望应用程序不可见,您可能需要
my answer to a similar question中的所有详细信息.
试试这个: unit Unit4; interface uses Windows,Clipbrd; type TForm4 = class(TForm) procedure FormCreate(Sender: TObject); procedure WMHotkey(var Message: TWMHotKey); message WM_HOTKEY; procedure FormDestroy(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form4: TForm4; implementation const MY_ID = 123; {$R *.dfm} procedure TForm4.FormCreate(Sender: TObject); begin RegisterHotKey(Handle,ord('1')); end; procedure TForm4.FormDestroy(Sender: TObject); begin UnregisterHotKey(Handle,MY_ID); end; procedure TForm4.WMHotkey(var Message: TWMHotKey); begin if Message.HotKey = MY_ID then begin if not AttachThreadInput(GetCurrentThreadId,true) then RaiseLastOSError; try Clipboard.AsText := 'This is my own text!'; SendMessage(GetFocus,false); end; end; end; end. 当然,您需要使用此方法并对其进行修改,以使其适合您的特定情况. (也就是说,您可能想要的东西比在Ctrl 1上打印“这是我自己的文本!”的应用程序更多,但没有别的.) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |