Solved

Setting active window for keystrokes

Posted on 1998-08-10
7
184 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
[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
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month4 days, 16 hours left to enroll

635 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