Solved

sending keys to another application...

Posted on 2002-05-12
10
349 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now