Solved

Setting active window for keystrokes

Posted on 1998-08-10
7
177 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Accepted Solution

by:
mtoft earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Bought This Question.
0
 
LVL 13

Expert Comment

by:Mirkwood
Comment Utility
Bought This Question.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 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

14 Experts available now in Live!

Get 1:1 Help Now