Link to home
Start Free TrialLog in
Avatar of royster
royster

asked on

Setting active window for keystrokes

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.
Avatar of rmichels
rmichels

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.
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.
Avatar of royster

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of mtoft
mtoft

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of royster

ASKER

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

Bought This Question.
Bought This Question.