Delphi应用外的动作事件 [英] action event outside delphi app

查看:143
本文介绍了Delphi应用外的动作事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


,当我的程序处于非活动状态时,如何处理键盘快捷键?


大家好
i需要制作一个delphi程序,该程序会创建一个在delphi应用程序外部可用的快捷键。例如:当我按ctrl + 1时,它将粘贴某些文本。当我按ctrl + 2时,出现另一个文本,依此类推。它确实有助于我的工作。我设法制作了一个可以执行此操作的delphi应用程序,但它只能在该应用程序内部运行。我希望它能在所有Windows应用程序中正常工作,只要我的应用程序处于打开状态(并最小化)。谁能帮我吗?我是delphi的新手,我想学习。



我尝试了有人向我推荐的这段代码,但是它不起作用。它什么都不做。我做错了什么?

  unit Unit3; 

界面

使用Windows,消息,SysUtils,变体,类,图形,控件,窗体,对话框,Clipbrd;

类型
TForm17 = class(TForm)
过程FormCreate(Sender:TObject);
过程FormDestroy(Sender:TObject);
private
{私有声明}
HotKey1:整数;
HotKey2:整数;
过程WMHotKey(var Msg:TWMHotKey);消息WM_HOTKEY;
public
{公开声明}
结尾;


var
Form17:TForm17;

实现

{$ R * .dfm}

{TForm17}

过程TForm17.FormCreate(Sender: TObject);
const
MOD_CONTROL = $ 0002; // 0x0002
开始
//注册Ctrl + 1热键
HotKey1:= GlobalAddAtom('Hotkey1');
RegisterHotKey(Handle,HotKey1,MOD_CONTROL,Ord(’1’));
//注册Ctrl + 2热键
HotKey2:= GlobalAddAtom(’Hotkey2’);
RegisterHotKey(Handle,HotKey2,MOD_CONTROL,Ord(’2’));
结尾;

过程TForm17.FormDestroy(Sender:TObject);
begin
//取消注册热键
UnRegisterHotKey(Handle,HotKey1);
GlobalDeleteAtom(HotKey1);
UnRegisterHotKey(Handle,HotKey2);
GlobalDeleteAtom(HotKey2);
结尾;

过程TForm17.WMHotKey(var Msg:TWMHotKey);
开始
,如果Msg.HotKey = HotKey1,则
开始
ShowMessage(按下Ctrl + 1);
Clipboard.AsText:=‘这是我自己的文字!’

结束
,否则
如果Msg.HotKey = HotKey2然后
开始
ShowMessage(按下Ctrl + 2);
Clipboard.AsText:=‘这是我自己的文字!’

结尾;
结尾;

结尾。


解决方案

您需要使用 RegisterHotKey UnregisterHotKey (从Win32 API中获取),它们非常易于使用。



此外,您可能会发现有用的ShortCutToKey(),它返回Delphi快捷方式的键代码和移位状态。



PS:请不要忘记请检查RegisterHotKey()的返回值,因为如果该热键已被其他应用程序注册,它将失败。



编辑:抱歉,我尽管您使用的是另一个WM_MESSAGE,但是自从您首先以纯文本形式发布了代码之后,我才对其进行了扫描...



我认为您的代码存在问题您正在使用GlobalAddAtom作为ID密钥,但是您只需要在应用程序内部使用唯一的ID(函数说,您只需要对共享的DLL使用GlobalAddAtom )。尝试仅使用以下命令:

  const 
ID_HOTKEY1 = 0;
ID_HOTKEY2 = 1;

过程TYourForm.FormCreate(Sender:TObject);
如果没有,则以
开头RegisterHotKey(Handle,ID_HOTKEY1,MOD_CONTROL,Ord('1'))
然后是Application.MessageBox('错误注册热键1','Error',MB_ICONERROR);
,如果不是RegisterHotKey(Handle,ID_HOTKEY2,MOD_CONTROL,Ord('2'))
然后是Application.MessageBox('注册热键2出错,'Error',MB_ICONERROR);
结尾;

过程TYourForm.FormDestroy(Sender:TObject);
开始
UnregisterHotKey(Handle,ID_HOTKEY1);
UnregisterHotKey(Handle,ID_HOTKEY2);
结尾;

过程TYourForm.WMHotKey(var Msg:TWMHotKey);
开始
Application.MessageBox(PChar(IntToStr(Msg.HotKey)),热键ID,MB_OK);
结尾;

此外,MOD_CONTROL和相关常量已由Delphi定义,您无需重新定义它们


Possible Duplicate:
How can I handle a keyboard shortcut when my program isn't active?

hello guys i need to make a delphi program that creates a short cut key that works outside the delphi app. for example: when i press ctrl+1 it pastes a certain text. When i press ctrl+2, another text, and so on. It really helps my work. I managed to make a delphi application that does that, but it only works inside that app. i want it to work in all windows applications as long as my app is open ( and minimized). Can anyone help me out? I'm pretty new at delphi, i'm trying to learn.

I tried this code which someone recommended to me but it doesn't work. it does nothing. what did i do wrong?

unit Unit3;  

interface  

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

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');  
     Clipboard.AsText := 'This is my own text!';  

  end  
  else   
  if Msg.HotKey = HotKey2 then  
  begin  
    ShowMessage('Ctrl + 2 was pressed');  
     Clipboard.AsText := 'This is my own text!';  

  end;  
end;  

end.     

解决方案

You need to use RegisterHotKey and UnregisterHotKey from the Win32 API, they are very straightforward to use.

Also, you may find useful ShortCutToKey(), which returns the key code and shift state of a Delphi shortcut.

PS: Don't forget to check the return value of RegisterHotKey(), since it will fail if the hotkey is already registered by other application.

Edit: sorry, I though that you were using another WM_MESSAGE, since first you posted the code as plain text and I only scanned through it...

I think that the problem with your code is that your are using GlobalAddAtom for the ID key, but you only need to use an unique ID inside your app (the docs for the function say that you need to use GlobalAddAtom only for a shared DLL). Try using just this:

const
  ID_HOTKEY1=0;
  ID_HOTKEY2=1;

procedure TYourForm.FormCreate(Sender: TObject);
begin
  if not RegisterHotKey(Handle,ID_HOTKEY1,MOD_CONTROL,Ord('1'))
    then Application.MessageBox('Error registering hot key 1','Error',MB_ICONERROR);
  if not RegisterHotKey(Handle,ID_HOTKEY2,MOD_CONTROL,Ord('2'))
    then Application.MessageBox('Error registering hot key 2','Error',MB_ICONERROR);
end;

procedure TYourForm.FormDestroy(Sender: TObject);
begin
  UnregisterHotKey(Handle,ID_HOTKEY1);
  UnregisterHotKey(Handle,ID_HOTKEY2);
end;

procedure TYourForm.WMHotKey(var Msg: TWMHotKey);
begin
  Application.MessageBox(PChar(IntToStr(Msg.HotKey)),'Hotkey ID',MB_OK);
end;

Also, the MOD_CONTROL and related constants are already defined by Delphi, you don't need to redefine them.

这篇关于Delphi应用外的动作事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆