Simulate Keystokes and MouseClicks

n_pelov
n_pelov used Ask the Experts™
on
I want to make a program to Control a remote PC on the local network.
I need to simulate keystokes and mouse clicks on the remote PC
For mouse clicks I need procedure like ClickMouse(X,Y) where X and Y and coordinates of desktop (Not on current window). I can't use postmessage(handle,wm_LmouseDown,...) because I can't get the coordinates of the window witch will get the MouseClick.
That's it!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
listening...
Hi,

Use WindowFromPoint API:

procedure ClickMouse(X,Y: integer);
var
  h: HWND;
  p: TPoint;
begin
  p.X := X;
  p.Y := Y;
  h := WindowFromPoint(p);
  if IsWindow(h) then begin
    // do PostMessage(h,...
  end;
end;

Regards, Geo
Here is the complete function:

// X and Y are screen coordinates
procedure ClickMouse(X,Y: integer);
var
  h: HWND;
  pos: DWORD;
  p: TPoint;
  r: TRect;
begin
  // get the window handle
  p.X := X;
  p.Y := Y;
  h := WindowFromPoint(p);
  if IsWindow(h) then begin
    // get the window rect (left and top)
    GetWindowRect(h, r);
    // create lParam
    pos := WORD(X - r.Left) or (WORD(Y - r.Top) shl 16);
    // simulate mouse click
    PostMessage(h, WM_LBUTTONDOWN, 0, pos);
    PostMessage(h, WM_LBUTTONUP, 0, pos);
  end;
end;

Regards, Geo
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Commented:
//Right Mouse Button Click Emulation:
begin
 mouse_event(mouseeventf_rightdown,0,0,0,0);
 mouse_event(mouseeventf_rightup,0,0,0,0);
end;

//Left Mouse Button Click Emulation:
begin
 mouse_event(mouseeventf_leftdown,0,0,0,0);
 mouse_event(mouseeventf_leftup,0,0,0,0);
end;

//Left Mouse Button Double Click Emulation:
begin
for i:=0 to 1 do
 begin
  mouse_event(mouseeventf_leftdown,0,0,0,0);
  mouse_event(mouseeventf_leftup,0,0,0,0);
end;


I don't know how to control the mouse but you something that calculates the X and Y points. I got this on another pc but maybe geobul is right. you need a client and a server application to make it. you can put in the client a TPanel --> that's the mouse control field. if you need any help just ask, i'm trying to work on this too but it's not really working though... :D
but maybe geobul is right (dunno, not tested)

laterz... ;-)

Commented:
and about keystrokes: i think you need something like the windows screen keyboard. you can find it with that thing with as icon a wheelchair (i don't know the name in english... :S). if you haven't installed this you can go to the Control Panel/Software/Windows Setup tab/click on the thing with the wheelchair as icon. it's not really neccesary but just as an example.
but i don't know how to do this in delphi :(
I doubt that it is possible to generate keystrokes and mouseclicks remotely. You will need a local app to generate them. That app you can tell remotely to generate the keystrokes and mouseclicks.
The two functions for that are mouse_event and keybd_event.

Commented:
do something like this:

in the client:
//put a button or something for the left mouse click
begin
ClientSocket1.Socket.SendText('Left Double Click')
end;

in the server:

//on ServerSocket1 ClientRead event

var
 S : String;
begin
 if S := 'Left Double Click' then
  begin
   for i:=0 to 1 do
    begin
     mouse_event(mouseeventf_leftdown,0,0,0,0);
     mouse_event(mouseeventf_leftup,0,0,0,0);
end;

or something like that. i don't have my own source on this computer so i don't know if it's correct. but remote mouse click emulation can be done.
i don't know how to work with the keybd_event but i think it's possible, too.

Author

Commented:
Look I know how to make the client and the server.
I'm looking for emulating keystokes from client application on the same computer.
mouse_event looks like what I seek for. I'll try it.
Postmessage is't working 'couse getwindowRect doesn't get the border size and the caption of the window.

GIVE ME SOMETHING FOR EMULATING(is this right - not simulating :o) ) KEYSTOKES

Thanks!
10x
Hi,

Another mouse click procedure with mouse pointer movement

procedure ClickMouse(X,Y: integer);
begin
  SetCursorPos(X,Y);
  mouse_event(mouseeventf_leftdown,0,0,0,0);
  mouse_event(mouseeventf_leftup,0,0,0,0);
end;

Regards, Geo

Author

Commented:
will any one tell mi how to emulate KEYBOARD keystokes?
Thank you for mouse events
Hi,

Try this one:

procedure SimulateKeyDown(Key : byte);
begin
  keybd_event(Key, 0, 0, 0);
end;

procedure SimulateKeyUp(Key : byte);
begin
  keybd_event(Key, 0, KEYEVENTF_KEYUP, 0);
end;

procedure SimulateKeystroke(Key : byte;
                            extra : DWORD);
begin
  keybd_event(Key,
              extra,
              0,
              0);
  keybd_event(Key,
              extra,
              KEYEVENTF_KEYUP,
              0);
end;

procedure SendKeys(s : string);
var
  i : integer;
  w : word;
begin
  for i := 1 to Length(s) do begin
    w := VkKeyScan(s[i]);
   {If there is not an error in the key translation}
    if ((HiByte(w) <> $FF) and
        (LoByte(w) <> $FF)) then begin
      {If the key requires the shift key down - hold it down}
      if HiByte(w) and 1 = 1 then
        SimulateKeyDown(VK_SHIFT);
      {Send the VK_KEY}
      SimulateKeystroke(LoByte(w), 0);
      {If the key required the shift key down - release it}
      if HiByte(w) and 1 = 1 then
        SimulateKeyUp(VK_SHIFT);
    end;
  end;
end;

// usage
SendKeys('Hello World!');
SimulateKeystroke(VK_RETURN,0);
SimulateKeystroke(VK_TAB,0);

Regards, Geo

Commented:
yep, geobul's code works ;-) - (just put it in a TTimer with an interval of 2000 or something and select a memo for testing on local pc)
so you got the mouse and keyboard events
how do you want to build the client? place a TPanel as the Mouse Control Field? or what do you have in mind?

i guess the only thing you need now is sending the x and y coordinates, right? i dunno how to do this since you'll need to send and receive 2 integers --> X and Y. i know how to send 1 (and i think you also know that...) (example: Edit1.Text := Socket.RecieveText). but not how to send 2 :(
if someone knows, could he/she plzzz post it here?

when you got the sending/receiving ready you also need to tell the pc that Edit1.Text and Edit2.Text (if you want to receive the X and Y in Edit boxes). that's just about 3 lines code --> got it on my other pc... :P

Commented:
here's some other code ;-)

(*The code below doesn't make the Panel to be the full screen --> that
depends on the TPanel's height and width. But it's more precisly than
taking the mouse pointer's screen coordinates.*)

procedure TForm1.Timer1Timer(Sender: TObject);
var
 S : String;
 S2 : String;
 P: TPoint;
begin
 if GetCursorPos(P) then
  begin
   if (WindowFromPoint(P) = Panel1.Handle) then
    begin
     P := ScreenToClient(P);
     S := IntToStr(P.X);
     S2 := IntToStr(P.Y);
     Edit1.Text := S;
     Edit2.Text := S2;
    end;
  end;
end;


(*The code below is to put the mouse pointer on the coordinates. Put
this code in the server. *)

procedure TForm1.Timer2Timer(Sender: TObject);
var
 X : Integer;
 Y : Integer;
begin
 X := StrToInt(Edit1.Text);
 Y := StrToInt(Edit2.Text);
 SetCursorPos(X,Y);
end;

Author

Commented:
mouse_event works!!!
if keybd_event is that what I need geobul will get the points :)

he gave me the full information of what I need.

for the client and the server I'll use TWSocket from "Internet Component Suite" components. I have enough experience with them.
  NetMasters components witch come with delphi have many bugs - They are awfull!

If someone is interested I'll give him the source code when I finish it.

Thank you all!
I think you should have looked up keybd_event in the help when i mentioned it.

Windows will of course send the keystrokes to the application which has the focus. Your app which generates them should be a tray application which should hide its main window.

Commented:
n_pelov, how do you make the server? how do you receive the two coordinates (X and Y) from the client and put them into 2 Edit boxes? so that X goes into Edit1 and Y goes into Edit2. or is there another way to do that?

Commented:
and about the mouse control. if you want the remote mouse on the exact spot of your (local) mouse then you'll also need to know the remote resolution (which is easy to get). i don't know if there's a possibility to get the resolution and then make the client's remote mouse control field (if you use this) the pixels without resizing, so that the control field will be exactly like the remote screen only smaller. do you think this is possible? cuz then you would have the ultimate mouse control app ;-)

and robert, i think you can solve that with setting TabStop to False (of every component) and hiding the server's window.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial