Solved

Simulate a keyPressed to other window

Posted on 2000-03-20
9
318 Views
Last Modified: 2010-04-04
Well,
i wanna send a #13 = Return to a foreign program window
i do have the window's handle with a EnumWindowsetc.
the window is just an information, but you have to click an OK-button
i'd like to simulate the pressing or clicking of the return-key.

but  i think I have to get the Buttons Handle for this ??

This wouldn't help
g:=findwindow('TheWindow',nil);
d:=Childwindowfrompoint(g,point(50,50));
sendmessage(d,wm_char,#13,0);

so, please, help
Indi
0
Comment
Question by:Indefrei
9 Comments
 
LVL 2

Expert Comment

by:alexstewart@beta
ID: 2637887
Try the SendKeys code on the delphi cd. Also Al Williams had a Dr Dobbs article on this in delphi. The basic call is
....
Keybd_event
....
You need to activate the target window first so the keys go to it...

Alex

0
 
LVL 1

Author Comment

by:Indefrei
ID: 2637991
Don't agree with
>>You need to activate the target window first so the keys go to it...
cause

g:=findwindow('Notepad',nil);
d:=Childwindowfrompoint(g,point(50,50));
sendmessage(d,wm_char,ord('A'),0);

works well, without activating (whatever you mean with that)
perhaps it's the wrong way, but
i'm just looking for the child's handle
Indi
0
 
LVL 7

Expert Comment

by:ahalya
ID: 2637995
if you just want to click a button, then they easiest way is to send a "Button Clicked" command to its parent window.

assume "h_Parent" is the handle of your parent window (that you got using enumwindows or findWindow);

You'll need to know the DialogItemID of the button you want to click. (can be found using WinSight32). If you want to find the DlgID using code, then you can use enumchildwindows (or FindWindowEx).
Say "DlgID" is the ItemID of the button, then you can "Click" the button by using:

h_Btn := GetDlgItem(h_parent, DlgID);
PostMessage( h_Parent, wm_Command,  DlgID, MakeLong(h_Btn, BN_Clicked));
0
 
LVL 17

Expert Comment

by:inthe
ID: 2638012
indi ,
if the button has a caption of 'Yes' with the Y has a underline for instance
it is '&Yes' (maybe you know ..
anyway you can do this:

var WName : Array[0..99] of Char;
    HH : THandle;

//Search Childs
Function TWCallBackChild(H : THandle; V : Longint) : Bool; stdcall;
begin
  GetwindowText(H,@WName[0],100);
  if StrPas(@WName[0]) = '&Yes' then
    HH := H;
  Result := True;
end;

//search Parents
Function TWCallBack(H : THandle; V : Longint) : Bool; stdcall;
begin
  GetwindowText(H,@WName[0],100);
  if StrPas(@WName[0]) = '&Yes' then
  begin
    HH := H;
  end
  else
    EnumChildWindows(H,@TWCallBackChild,V);
  Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var Dummy : Longint;
begin
  HH := 0;
  EnumWindows(@TWCallBack,Dummy);  //Search through the windows
  if HH <> 0 then  //if found then press
  begin
    setforegroundwindow(hh);
sendmessage(HH,bm_click,0,0);//x_calibar
  end;
end;

end.

an example is open notepad and type something thenpress the x in corner to close notepad it asks to save "yes" "no" "cancel" buttons..
run the code above and it will press the '&Yes' button
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:Indefrei
ID: 2638018
Sounds good
with enumchildwindows etc.
try to give a code example, please
I'm back tomorrow
hope to cu
Indi
0
 
LVL 1

Author Comment

by:Indefrei
ID: 2638033
Ooops
too slow
didn't expect your presence so late, Barry
thought you are still searching for HIM, he,he
thanks everybody for the comments
check it tomorrow
Indi
0
 
LVL 1

Author Comment

by:Indefrei
ID: 2638043
ps
>> &Yes
As you can read above
it is a foreign program, means, i even don't know in which language it is written
and it is just an OK button
0
 
LVL 1

Author Comment

by:Indefrei
ID: 2638060
I see
The O is Underlined
try that
good night everybody
Indi
0
 
LVL 7

Accepted Solution

by:
ahalya earned 50 total points
ID: 2638387
The following procedure will click the "OK" button. The handle of the window is the input. (the one you got in your enumwindows).
[You could use enumchildwindows as I have suggested earlier and given in the sample code of inthe.  This is an alternative way of doing it]

procedure ClickOkButton(h:HWND);

var c : HWND;
     s : string;
     b : array[0..100] of char;
     DlgID : Word;

begin;
c := GetWindow(h, GW_CHILD); //get first child window
repeat
  GetWindowText(c, @b, 100);
  s := string(b);
  DlgID  := GetDlgCtrlID(c);
  c := GetWindow(c, GW_HWNDNEXT); //get first child window
until (c = 0) or (lowercase(s) = '&ok');
PostMessage(h, wm_Command, DlgID, MakeLong(h, BN_Clicked));
end;

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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