Is it ok if i use it like this..for multiple events?
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.
to complement the Andreas answer you can use the
RegisterHotKey
function in combination with theWM_HOTKEY
windows message.try this code
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.
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:
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.)