Solved

SendKeys (Another!??)  :-)

Posted on 2002-05-30
3
347 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

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

Join & Write a Comment

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

19 Experts available now in Live!

Get 1:1 Help Now