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

后端 未结 3 1268
走了就别回头了
走了就别回头了 2021-01-06 11:44

Is it ok if i use it like this..for multiple events?

unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms         


        
相关标签:
3条回答
  • 2021-01-06 12:03

    to complement the Andreas answer you can use the RegisterHotKey function in combination with the WM_HOTKEY windows message.

    try this code

    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;
    
    0 讨论(0)
  • 2021-01-06 12:08

    Use the RegisterHotKey function. If you want the application to be invisible, you might want all the details in my answer to a similar question.

    Try this:

    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.
    

    Of course, you will need to use this approach and modify it so it suits your particular case. (That is, you probably want something more than an application that prints "This is my own text!" on Ctrl+1, but nothing else.)

    0 讨论(0)
  • 2021-01-06 12:18

    As others suggested, it's RegisterHotKey function. However, proper implementation of the application that you want to design requires keyboard hook and DLL injection into the application.

    I would recommend that you take a look at TypePilot application. It lets you type or copy/paste any text with certain shortcuts that you type. Eg. you can type "thnk " and this will be replaced with "thank you" by the application.

    0 讨论(0)
提交回复
热议问题