Delphi 当我的程序不是';不活跃?
如果我这样使用它可以吗..用于多个事件Delphi 当我的程序不是';不活跃?,delphi,events,shortcut,taction,Delphi,Events,Shortcut,Taction,如果我这样使用它可以吗..用于多个事件 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 Mess
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, MOD_CONTROL, ord('2'));
RegisterHotKey(Handle, MY_ID2, MOD_CONTROL, 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, 0);
finally
AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), false);
end;
if Message.HotKey = MY_ID1 then
begin
if not AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), true) then
RaiseLastOSError;
try
Clipboard.AsText := 'text2';
SendMessage(GetFocus, WM_PASTE, 0, 0);
finally
AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), false);
end;
if Message.HotKey = MY_ID2 then
begin
if not AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), true) then
RaiseLastOSError;
try
Clipboard.AsText := 'text3';
SendMessage(GetFocus, WM_PASTE, 0, 0);
finally
AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), false);
end;
end;
end;
end;
end;
end.
使用该函数。如果希望应用程序不可见,则可能希望所有详细信息都显示在中
试试这个:
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;
{$R *.dfm}
procedure TForm4.FormCreate(Sender: TObject);
begin
RegisterHotKey(Handle, MY_ID, MOD_CONTROL, 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, GetWindowThreadProcessId(GetForegroundWindow), true) then
RaiseLastOSError;
try
Clipboard.AsText := 'This is my own text!';
SendMessage(GetFocus, WM_PASTE, 0, 0);
finally
AttachThreadInput(GetCurrentThreadId, GetWindowThreadProcessId(GetForegroundWindow), false);
end;
end;
end;
end.
当然,您需要使用此方法并对其进行修改,以使其适合您的特定情况。(也就是说,您可能需要的不仅仅是在Ctrl+1上打印“这是我自己的文本!”的应用程序,而不是其他任何东西。)为了补充Andreas的答案,您可以将该功能与windows消息结合使用 试试这个代码
type
TForm17 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
HotKey1 : Integer;
HotKey2 : Integer;
procedure WMHotKey(var Msg: TWMHotKey); message WM_HOTKEY;
public
{ Public declarations }
end;
var
Form17: TForm17;
implementation
{$R *.dfm}
{ TForm17 }
procedure TForm17.FormCreate(Sender: TObject);
const
MOD_CONTROL = $0002;//0x0002
begin
// Register Ctrl + 1 hotkey
HotKey1 := GlobalAddAtom('Hotkey1');
RegisterHotKey(Handle, HotKey1, MOD_CONTROL, Ord('1'));
// Register Ctrl + 2 hotkey
HotKey2 := GlobalAddAtom('Hotkey2');
RegisterHotKey(Handle, HotKey2, MOD_CONTROL, Ord('2'));
end;
procedure TForm17.FormDestroy(Sender: TObject);
begin
//unregister the hotkeys
UnRegisterHotKey(Handle, HotKey1);
GlobalDeleteAtom(HotKey1);
UnRegisterHotKey(Handle, HotKey2);
GlobalDeleteAtom(HotKey2);
end;
procedure TForm17.WMHotKey(var Msg: TWMHotKey);
begin
if Msg.HotKey = HotKey1 then
begin
ShowMessage('Ctrl + 1 was pressed');
//do your stuff
end
else
if Msg.HotKey = HotKey2 then
begin
ShowMessage('Ctrl + 2 was pressed');
//do your stuff
end;
end;
正如其他人所建议的,它是RegisterHotKey函数。但是,要正确实现要设计的应用程序,需要向应用程序中注入键盘挂钩和DLL
我建议您看看TypePilot应用程序。它允许您键入或复制/粘贴带有您键入的某些快捷方式的任何文本。您可以键入“thnk”,应用程序会将其替换为“谢谢” 你能给我举个例子吗?比方说,当我按ctrl+1时,每个windows应用程序上都会出现delphi这个词,比如microsoft word或so@andrei当前位置我现在举一个例子。但至少在Word 2010中它似乎不起作用(但记事本工作正常)。Andreas当您使用
注册表快捷键释放热键时,您只需使用取消注册热键
功能。我知道。我只是草率地向OP展示了这个想法。为什么?我认为它可以足够获得活动窗口的句柄并发送粘贴消息?粘贴消息将粘贴剪贴板内容。如果您认为将用文本替换剪贴板内容,请粘贴文本并将数据放回剪贴板,请再想一想:剪贴板实际上并不需要包含数据。有可能将对数据的引用放入剪贴板,但您将无法正确保存该引用。可能存在以下重复项: