Solved

sending keys to another application...

Posted on 2002-05-12
10
363 Views
Last Modified: 2010-04-04
Hi guys,

I want to send keys to another application. Not only characters or strings, I want to send Ctrl-left arrow, Ctrl-Right arrow, Ctrrl-Shift whatever...

Any clues?

best regards,
Manuel López (lopem)
0
Comment
Question by:lopem
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 5

Expert Comment

by:alanwhincup
ID: 7004878
Have a look at this example:

http://www.swissdelphicenter.ch/en/showcode.php?id=220

I've used this function and find it works very well.

Cheers,

Alan
0
 
LVL 2

Expert Comment

by:Tasomia
ID: 7004879
Hi,

There's a function from P. Below at:
http://www.swissdelphicenter.ch/en/showcode.php?id=220
0
 
LVL 2

Expert Comment

by:Tasomia
ID: 7004885
alanwhincup, you were faster...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:lopem
ID: 7004987
I tried the code of the page you sent me (see code below)... But it didnt work. I previously load an instance of Notepad and nothing. No keys sent. Am I doing something wrong? Whta about using Word or Excel? What I have to ask in the findwindow function? WinWord? WinExcel? Word? Excel?

thanks in advance...
Manuel Lopez (lopem)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


{****************************************************}
{ Postkey function with HWND }

procedure PostKeyExHWND(hWindow: HWnd; key: Word; const shift: TShiftState;
                           specialkey: Boolean);
type
  TBuffers = array [0..1] of TKeyboardState;
var
  pKeyBuffers: ^TBuffers;
  lParam: LongInt;
begin
  (* check if the target window exists *)
  if IsWindow(hWindow) then
  begin
    (* set local variables to default values *)
    pKeyBuffers := nil;
    lParam := MakeLong(0, MapVirtualKey(key, 0));

    (* modify lparam if special key requested *)
    if specialkey then
      lParam := lParam or $1000000;

  (* allocate space for the key state buffers *)
  New(pKeyBuffers);
  try
    (* Fill buffer 1 with current state so we can later restore it.
    Null out buffer 0 to get a "no key pressed" state. *)
    GetKeyboardState(pKeyBuffers^[1]);
    FillChar(pKeyBuffers^[0], SizeOf(TKeyboardState), 0);
    (* set the requested modifier keys to "down" state in the buffer*)
    if ssShift in shift then
      pKeyBuffers^[0][VK_SHIFT] := $80;
    if ssAlt in shift then
    begin
      (* Alt needs special treatment since a bit in lparam needs also be set *)
      pKeyBuffers^[0][VK_MENU] := $80;
      lParam := lParam or $20000000;
    end;
    if ssCtrl in shift then
      pKeyBuffers^[0][VK_CONTROL] := $80;
    if ssLeft in shift then
      pKeyBuffers^[0][VK_LBUTTON] := $80;
    if ssRight in shift then
      pKeyBuffers^[0][VK_RBUTTON] := $80;
    if ssMiddle in shift then
      pKeyBuffers^[0][VK_MBUTTON] := $80;

    (* make out new key state array the active key state map *)
    SetKeyboardState(pKeyBuffers^[0]);
    (* post the key messages *)
    if ssAlt in Shift then
    begin
      PostMessage(hWindow, WM_SYSKEYDOWN, key, lParam);
      PostMessage(hWindow, WM_SYSKEYUP, key, lParam or $C0000000);
    end
    else
    begin
      PostMessage(hWindow, WM_KEYDOWN, key, lParam);
      PostMessage(hWindow, WM_KEYUP, key, lParam or $C0000000);
    end;
   (* process the messages *)
    Application.ProcessMessages;
    (* restore the old key state map *)
    SetKeyboardState(pKeyBuffers^[1]);
    finally
      (* free the memory for the key state buffers *)
      if pKeyBuffers <> nil then
      Dispose(pKeyBuffers);
    end; { If }
  end;
end; { PostKeyEx }

procedure TForm1.Button1Click(Sender: TObject);
var
  targetWnd: HWND;
begin
  targetWnd := 0;
  targetWnd := FindWindow('notepad',nil);
  if targetWnd <> 0 then
  begin
    PostKeyExHWND(targetWnd, Ord('I'), [], False);
  end;
end;



end.
0
 
LVL 2

Expert Comment

by:Tasomia
ID: 7005397
1. You need the correct handle:

 Wnd := FindWindow('notepad', nil);
 if wnd <> 0 then
 begin
   wnd := FindWindowEx(Wnd, 0, 'Edit', nil);
   //...

2.
 Note that Setting the keyboard state will not work across Applications
 running in different memory spaces on Win32 unless AttachThreadInput is
 used to connect to the target thread first.
0
 

Expert Comment

by:jeff168
ID: 7014200
use hook
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7014847
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101, Netminder or Mindphaser will return to finalize these if they are still open in 7 days.  Experts, please post closing recommendations before that time.

Below are your open questions as of today.  Questions which have been inactive for 21 days or longer are considered to be abandoned and for those, your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20103843.html
http://www.experts-exchange.com/questions/Q.20106401.html
http://www.experts-exchange.com/questions/Q.11362697.html
http://www.experts-exchange.com/questions/Q.20143988.html
http://www.experts-exchange.com/questions/Q.20171229.html
http://www.experts-exchange.com/questions/Q.20190216.html
http://www.experts-exchange.com/questions/Q.20190226.html
http://www.experts-exchange.com/questions/Q.20193039.html
http://www.experts-exchange.com/questions/Q.20195740.html
http://www.experts-exchange.com/questions/Q.20230594.html
http://www.experts-exchange.com/questions/Q.20241586.html
http://www.experts-exchange.com/questions/Q.20273100.html
http://www.experts-exchange.com/questions/Q.20281799.html
http://www.experts-exchange.com/questions/Q.20283305.html
http://www.experts-exchange.com/questions/Q.20297478.html
http://www.experts-exchange.com/questions/Q.20299749.html



*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations.
If you are interested in the cleanup effort, please click this link
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed in the link below
http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thanks everyone.
Moondancer
Moderator @ Experts Exchange
0
 
LVL 3

Author Comment

by:lopem
ID: 7014971
How, Jeff?
0
 

Accepted Solution

by:
jeff168 earned 50 total points
ID: 7019824
you can find a sample from <<Delphi5 Developer's Guid>>
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7068888
Finalized by Moondancer - EE Moderator
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to center only a line in richedit? 4 69
Show Listview image from database (String field) 5 143
DBGrid or StringGrid ? 6 119
Problem working with dynamic array - help 2 35
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question