Solved

SendKeys (Another!??)  :-)

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 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