-2

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.
Kromster
  • 7,181
  • 7
  • 63
  • 111
M0HX
  • 31
  • 7

1 Answers1

4

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 5
    No. The function is well documented and there are loads of good examples available. – David Heffernan Apr 19 '14 at 12:37
  • @Mǿĥãmmèḓ Ĥḿĥn Peter Below, one of the TeamB members on Embarcadero's newsgroups has posted several examples of SendKeys() equivalents over the years. A complete example is here http://cc.embarcadero.com/Item/27218 – MartynA Apr 19 '14 at 13:33
  • Don't worry guys, i just found a solution C; Thanks anyway i appreciate your help. – M0HX Apr 19 '14 at 20:26
  • @MǿĥãmmèḓĤḿĥn: welcome to the internet. :) you've got a lot left to learn. – Wouter van Nifterick Apr 20 '14 at 03:01
  • @MǿĥãmmèḓĤḿĥn In other words, please delete your comment with your email address, I'm sure you don't want millions of future viewers seeing it and sending you loads of things you don't want. – Jerry Dodge Apr 20 '14 at 04:28
  • Thanks i meant by "thanks anyway" is i found another solution for my problem ^-^ , and i'v deleted my comment . That was my solution : Clipboard.AsText := memo1.Text; keybd_event(VK_CONTROL, 0, KEYEVENTF_EXTENDEDKEY or 0, 0); keybd_event(Ord('V'), 0, 0, 0); keybd_event(VK_CONTROL, $45, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP, 0); Keybd_Event(VK_RETURN, 1, 0, 0); Keybd_Event(VK_RETURN, 1, KEYEVENTF_KEYUP, 0); – M0HX Apr 20 '14 at 15:59
  • That is exactly what I said to do. Only you used the old deprecated api rather than its replacement. So you did not find another solution. – David Heffernan Apr 20 '14 at 16:12