G
Gary Roach
i'm trying to send keystrokes to an application that i launch from vba.
sendkeys doesn't seem to work because i can't guarantee that the app has
gotten started and has the focus and is ready to receive input. i decided to
use sendmessage to send input to the window in question. i'm using notepad
as a test. this works great with WM_CHAR, Asc ("a"), but when i try sending
system keys which ultimately is what i want to do, i can't get any response.
for the test, i'm sending alt-F4, expecting the window to briefly open, and
then close. instead, it just remains open. any idea what the problem is?
here's the code i'm using:
Public Sub Foo15()
Dim ProcessID As Long
ProcessID = Shell("""c:\windows\system32\notepad.exe""", vbNormalFocus)
Dim hWndJob As Long
hWndJob = FindWindow(vbNullString, vbNullString)
Do Until hWndJob = 0
If GetParent(hWndJob) = 0 Then
Dim PID As Long
Call GetWindowThreadProcessId(hWndJob, PID)
If PID = ProcessID Then
Exit Do
End If
End If
hWndJob = GetWindow(hWndJob, GW_HWNDNEXT)
Loop
EnumChild hWndJob, 0
End Sub
Private Function EnumChild(ByVal hWndJob As Long, ByVal lParam As Long) As
Long
SendMessage hWndJob, WM_SYSKEYDOWN, &H73, 1 Or 2 ^ 29
SendMessage hWndJob, WM_SYSKEYUP, &H73, 1 Or 2 ^ 29 Or 2 ^ 30
Or -2147483648#
EnumChildWindows hWndJob, AddressOf EnumChild2, 10
End If
End Function
Private Function EnumChild2(ByVal hWndJob As Long, ByVal lParam As Long) As
Long
EnumChild2 = EnumChild(hWndJob, lParam)
End Function
sendkeys doesn't seem to work because i can't guarantee that the app has
gotten started and has the focus and is ready to receive input. i decided to
use sendmessage to send input to the window in question. i'm using notepad
as a test. this works great with WM_CHAR, Asc ("a"), but when i try sending
system keys which ultimately is what i want to do, i can't get any response.
for the test, i'm sending alt-F4, expecting the window to briefly open, and
then close. instead, it just remains open. any idea what the problem is?
here's the code i'm using:
Public Sub Foo15()
Dim ProcessID As Long
ProcessID = Shell("""c:\windows\system32\notepad.exe""", vbNormalFocus)
Dim hWndJob As Long
hWndJob = FindWindow(vbNullString, vbNullString)
Do Until hWndJob = 0
If GetParent(hWndJob) = 0 Then
Dim PID As Long
Call GetWindowThreadProcessId(hWndJob, PID)
If PID = ProcessID Then
Exit Do
End If
End If
hWndJob = GetWindow(hWndJob, GW_HWNDNEXT)
Loop
EnumChild hWndJob, 0
End Sub
Private Function EnumChild(ByVal hWndJob As Long, ByVal lParam As Long) As
Long
SendMessage hWndJob, WM_SYSKEYDOWN, &H73, 1 Or 2 ^ 29
SendMessage hWndJob, WM_SYSKEYUP, &H73, 1 Or 2 ^ 29 Or 2 ^ 30
Or -2147483648#
EnumChildWindows hWndJob, AddressOf EnumChild2, 10
End If
End Function
Private Function EnumChild2(ByVal hWndJob As Long, ByVal lParam As Long) As
Long
EnumChild2 = EnumChild(hWndJob, lParam)
End Function