Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SendKeys (Another!??)  :-)

Posted on 2002-05-30
3
Medium Priority
?
383 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

877 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