Question

so i am trying to make an auto typing program in delphi , i did it in VB.NET and it was easy because VB.NET has 'Sendkeys' Function .

so i made my own autotyper in delphi after research but this is only what i got .. i can only send keystrokes to 'notepad' , i am trying to make the same app but send keystrokes anywere like browsers,games,etc.

so this is my code :


unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,     Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Menus,
  System.Actions, Vcl.ActnList;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
Bevel1: TBevel;
Label1: TLabel;
Edit1: TEdit;
Button2: TButton;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Timer1: TTimer;
ActionList1: TActionList;
SelectAll: TAction;
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure SelectallExecute(Sender: TObject);
 private
{ Private declarations }
 public
{ Public declarations }
 end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.SelectallExecute(Sender: TObject);
begin
memo1.SelectAll;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
timer1.Enabled := true;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
timer1.Enabled := false;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  wnd: HWND;
  i: Integer;
  s: string;

begin
//timer intreval IntToStr '9arb 1000 mile seccounds
 Timer1.Interval:=StrToInt(edit1.Text)*1000;

  wnd := FindWindow('notepad', nil);
  if wnd <> 0 then
  begin
    wnd := FindWindowEx(wnd, 0, 'edit', nil);


    // Write Text in Notepad.
    // Text ins Notepad schreiben.
    s := memo1.Text;
    for i := 1 to Length(s) do
      SendMessage(wnd, WM_CHAR, Word(s[i]), 0);
    // Simulate Return Key.
    PostMessage(wnd, WM_KEYDOWN, VK_RETURN, 0);
    // Simulate Space.
    PostMessage(wnd, WM_KEYDOWN, VK_SPACE, 0);
      end;
    end;

    end.
Was it helpful?

Solution

The supported way to fake input is not to use SendMessage and PostMessage. Instead call SendInput which places input events into the message queue of the foreground window. This is how the .net SendKeys method is implemented.


In the comments you say that you used keybd_event. This is not advisable. From the documentation:

This function has been superseded. Use SendInput instead.

The documentation for SendInput explains why:

The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream. These events are not interspersed with other keyboard or mouse input events inserted either by the user (with the keyboard or mouse) or by calls to keybd_event, mouse_event, or other calls to SendInput.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top