Solved

Setting active window for keystrokes

Posted on 1998-08-10
7
183 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

729 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