Solved

Setting active window for keystrokes

Posted on 1998-08-10
7
178 Views
Last Modified: 2010-04-30
I want to send keystrokes to an application but don't want to use shell to get a window ID. I'd rather use an api call such as one I found here:
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function SetActiveWindow Lib "user32" (ByVal hwnd As Long) As Long

lhWnd = FindWindow(vbNullString, "My Application")
lOldhWnd = SetActiveWindow(lhWnd)
SendKeys "^(l)", True
SendKeys "%fo", True
For some reason this isn't working for me. I get a value for lhWnd but the SetActiveWindow returns 0 and the keystrokes arent sent to the desired application. If u can see what I'm doing wrong please let me know.
0
Comment
Question by:royster
7 Comments
 
LVL 3

Expert Comment

by:rmichels
ID: 1468484
I'm wondering if you need a doevents after the SetActiveWindow?  Not a clean solution though, but the window activation may not be occuring until after you complete your code path through VB.
0
 
LVL 14

Expert Comment

by:waty
ID: 1468485
Activating Other Applications
Keystrokes that you send can go to only one place: the active application. If you do not activate another application, your Visual Basic application will send keystrokes to itself. While this might be handy for testing your application, it is generally not very useful. To send keystrokes to another application, you have to activate that application with the AppActivate statement. For example, if the Windows Terminal application was running, this statement would activate it:

AppActivate "Terminal"

If the application is not already running, you must start it with the Shell function (described earlier) before you can activate it and send keystrokes to it.
For More Information For information on the AppActivate statement, search Help for AppActivate.

Specifying Special Characters
You may want to send characters that you cannot simply type into a string, such as ENTER, F1, and TAB. To send these characters, you send the name of the key surrounded by brace characters ({}). For example, to send the keystrokes for TAB, F1, and ENTER, use this statement:

SendKeys "{TAB}{F1}{enter}"

The names of the keys are not case-sensitive, so “enter,” “Enter,” and “ENTER” are all equivalent.

Note You cannot send certain keys that generate interrupts rather than character codes, such as CTRL+ALT+DEL and PRINTSCRN.

For More Information For a complete list of the special key names, search Help for SendKeys.
0
 
LVL 1

Author Comment

by:royster
ID: 1468486
I already tried the shell function with appactivate. It semi-works but is flakey at best. Thats why I stated I don't want to use it. Thanks, Roy
0
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.

 
LVL 2

Accepted Solution

by:
mtoft earned 100 total points
ID: 1468487
Use API Call:
  SetForegroundWindow

To activate the application, then to activate a specific window in that application use
  SetActiveWindow

0
 
LVL 1

Author Comment

by:royster
ID: 1468488
Thanks, mtoft. That worked. You commented "then to activate a specific window in that application use SetActiveWindow". I'm interested in how to get a handle on the application specific window. -Roy
BTW, for anyone interested the delcaration for SetForegroundWindow is:
Declare Function apiSetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal Hwnd As Long) As Long

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1468489
Bought This Question.
0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1468490
Bought This Question.
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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

911 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