Solved

SendKeys (Another!??)  :-)

Posted on 2002-05-30
3
355 Views
Last Modified: 2010-04-04
Yet another question in the database regarding SendKeys functionality...

In another question about a year ago:
http://www.experts-exchange.com/questions/Q_20116258.html

"inthe" provided a nice little unit from Ken Henderson that provides AppActivate and SendKeys functions.  

The asker of the question noted the following:
"AppActivate returns True, but the Sendkeys command does not write to Notepad.
. . .
Also, if I send {ENTER} in the string, my computer makes a painful noise until it gets a stack overflow."

Because there was another way to accomplish what this particular person needed to do, apparently he didn't use the unit (even though it was marked as the accepted answer) and this problem was never addressed.

Okay, here's the specifics on the problem:

The provided AppActivate command always works GREAT when your program is run within the IDE.  However, when you run it outside the development environment, it never actually switches focus, even though it claims to.   Thus any keystrokes are sent to YOUR program as if you'd never issued the AppActivate call.

Actually, its behavior depends on the state of the target window.
If the desired application is:
   Minimized: it will work.  
   Normalized: it will fail.
   Maximized:  it will normalize, then fail.

This behavior has been observed on both W2k and W98SE.
Any ideas why, or how to fix it?
0
Comment
Question by:charles_ebs
  • 2
3 Comments
 

Author Comment

by:charles_ebs
ID: 7047568
For your convenience, here's a code section:

{AppActivate

This is used to set the current input focus to a given window using its
name. This is especially useful for ensuring a window is active before
sending it input messages using the SendKeys function. You can specify
a window's name in its entirety, or only portion of it, beginning from
the left.

}

var
WindowHandle : HWND;

function EnumWindowsProc(WHandle: HWND; lParam: LPARAM): BOOL; export; stdcall;
const
MAX_WINDOW_NAME_LEN = 80;
var
WindowName : array[0..MAX_WINDOW_NAME_LEN] of char;
begin
{Can't test GetWindowText's return value since some windows don't have a title}
GetWindowText(WHandle,WindowName,MAX_WINDOW_NAME_LEN);
Result := (StrLIComp(WindowName,PChar(lParam), StrLen(PChar(lParam))) <> 0);
If (not Result) then WindowHandle:=WHandle;
end;

function AppActivate(WindowName : PChar) : boolean;
begin
try
Result:=true;
WindowHandle:=FindWindow(nil,WindowName);
If (WindowHandle=0) then EnumWindows(@EnumWindowsProc,Integer(PChar(WindowName)));
If (WindowHandle<>0) then begin
SendMessage(WindowHandle, WM_SYSCOMMAND, SC_HOTKEY, WindowHandle);
SendMessage(WindowHandle, WM_SYSCOMMAND, SC_RESTORE, WindowHandle);
end else Result:=false;
except
on Exception do Result:=false;
end;
end;
0
 

Author Comment

by:charles_ebs
ID: 7071170
Here's a way to fix the problem:

Keep this line:
   SendMessage(WindowHandle, WM_SYSCOMMAND, SC_HOTKEY, WindowHandle);
Remove this line:
   //SendMessage(WindowHandle, WM_SYSCOMMAND, SC_RESTORE, WindowHandle);
Add this line:
   SetForegroundWindow(WindowHandle);

SC_RESTORE isn't necessary.  If a window has been previously maximized, the routine will now keep that state.

I tried removing the SC_HOTKEY, but if the target window was previously minimized, it would restore but lose the first series of keystrokes sent to it.   Now it seems to work well in all situations.
0
 
LVL 1

Accepted Solution

by:
Moondancer earned 0 total points
ID: 7071455
200 points refunded to you, this item moved to our PAQ at zero points and closed.
Moondancer - EE Moderator
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

Title # Comments Views Activity
IExtractImage Delphi 14 198
Activex get website that activex running on 3 85
Delphi OLE Error 8 93
Delphi Ado Query To Converts Joined Columns Into Rows Based on Primary Key 4 36
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

930 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

10 Experts available now in Live!

Get 1:1 Help Now