Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SendKeys (Another!??)  :-)

Posted on 2002-05-30
3
Medium Priority
?
375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

715 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