Action event outside delphi application

Possible duplicate:
How can I handle a key combination when my program is inactive?

hi guys I need to make a delphi program that creates a short key that works outside of a delphi application. for example: when I press ctrl + 1, it inserts certain text. When I press ctrl + 2, another text, etc. It really helps my work. I managed to make a delphi application that does this, but it only works inside this application. I want it to work in all Windows applications while my application is open (and minimized). Can someone help me? I am new to delphi, I am trying to learn.

I tried this code that someone recommended to me, but it does not work. he does not do anything. what I did 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. 
0
source share
2 answers

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

Alternatively, you can find the useful ShortCutToKey () which returns the key code and shift status in the Delphi shortcut.

PS: Do not forget to check the return value of RegisterHotKey (), since it will not be executed if the hotkey is already registered by another application.

Edit : Sorry, although I used a different WM_MESSAGE, since you first placed the code as plain text, and I only looked at it ...

I think the problem with your code is that you are using GlobalAddAtom for the ID key, but you only need to use the unique identifier inside your application (the docs for the function say that you need to use GlobalAddAtom only for the common DLL). Try using only 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; 

In addition, MOD_CONTROL and its associated constants are already defined by Delphi, you do not need to redefine them.

+2
source

andrei, check this sample code to insert text into an external application using the hotkey.

the code shows two options

1) sending the combination Ctrl + V to the focused window

2) send WM_PASTE message

 function GetFocusedHandle: THandle; var ActiveHWND : THandle; FocusedThread : DWORD; begin Result:=0; ActiveHWND := GetForegroundWindow; FocusedThread := GetWindowThreadProcessID(ActiveHWND, nil) ; try if AttachThreadInput(GetCurrentThreadID, FocusedThread, true) then Result := GetFocus; finally AttachThreadInput(GetCurrentThreadID, FocusedThread, false) ; end; end; procedure TForm17.WMHotKey(var Msg: TWMHotKey); var FocusWindowHwnd : THandle; begin if Msg.HotKey = HotKey1 then //option 1 begin Clipboard.AsText := 'Text from Ctrl + 1 Hotkey';//Assign the text to the clipboard //send the Ctrl + V combination to the current focused window keybd_event(VK_CONTROL, 0, 0, 0); keybd_event(Ord('V'), 0, 0, 0); end else if Msg.HotKey = HotKey2 then //option 2 begin FocusWindowHwnd:=GetFocusedHandle; //get the handle to the focused window if FocusWindowHwnd<>0 then begin Clipboard.AsText := 'Text from Ctrl + 2 Hotkey';//Assign the text to the clipboard SendMessage(FocusWindowHwnd,WM_PASTE,0,0);//send the WM_PASTE message end; end; end; 
+1
source

All Articles