sending system keystrokes to apps

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
 
N

Nikola Bozovic

Q: It seams to me that this is dead lock q=)? What are you trying to do here !?

[SolarAngel]

| 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
|
|
| --
| Gary Roach
| ADB Services
|
|
 
G

Gary Roach

i'm sending keystroke messages to an application window and all it's child
windows. this works for sending actual characters. i can send and "A" to
notepad using WM_CHAR. however, in this case, i want to send a system
character (alt-f4). this is only an excercise to see if i can do it.
ultimately i want to send keystrokes to control an application's menu
(alt-F, etc.)
 
A

Andrew Cushen

Gary-

see my reply to your earlier message. I have a link in
there to an example that uses SendMessage to send a hotkey
combination.

-Andrew
=======================================
-----Original Message-----
i'm sending keystroke messages to an application window and all it's child
windows. this works for sending actual characters. i can send and "A" to
notepad using WM_CHAR. however, in this case, i want to send a system
character (alt-f4). this is only an excercise to see if i can do it.
ultimately i want to send keystrokes to control an application's menu
(alt-F, etc.)

Nikola Bozovic said:
Q: It seams to me that this is dead lock q=)? What are
you trying to do
here !?
[SolarAngel]

| 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
|
|
| --
| Gary Roach
| ADB Services
|
|


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top