I'm working on an application that will send text to another
application via hWnds. I've got the hWnd code all set. I can send
plain text with no trouble (sample code included). What I haven't been
able to figure out is how to send a control-key sequence. I CAN NOT
use sendkeys as I can't guarantee that the target application will
necessarily be in focus at any given moment. Sendkeys is NOT an
option.
The API docs (Chuck Petzold's book was my reference) would seem to
suggest running a SendMessage() call before and after the existing
SendMessage call to turn on and turn off the VK_CONTROL key when
applicable. This doesn't seem to work and I can't find any
existing/working examples.
Here is the most basic version of the code used to send the plaintext
(contractual obligations keep me from posting my exact code):
Private Sub SendText()
Static working As Boolean
Dim pressedKey As String
Dim pressedKeys As String
Dim scnCode As Long
Dim interCharDelay As Long
If (working) Or (pHwnd < 1) Then
Exit Sub
End If
working = True
interCharDelay = 5
'// each "line" of text must have a CR-only at the end
pressedKeys = Me.Text1.Text
me.Text1.Text = ""
call LogLastLine(pressedKeys)
pressedKeys = Replace$(pressedKeys, vbCrLf, vbCr)
pressedKeys = Replace$(pressedKeys, vbLf, vbCr)
pressedKeys = Replace$(pressedKeys, vbTab, Space$(4))
pressedKeys = Replace$(pressedKeys, "{tab}", Space$(4))
'// follow lines should be changed to allow other special chars
'pressedKeys = Replace$(pressedKeys, "{tab}", Space$(4))
'pressedKeys = Replace$(pressedKeys, "{tab}", Space$(4))
'pressedKeys = Replace$(pressedKeys, "{tab}", Space$(4))
'pressedKeys = Replace$(pressedKeys, "{tab}", Space$(4))
If (Right$(pressedKeys, 1) <> vbCr) Then
pressedKeys = pressedKeys & vbCr
End If
Dim r As Long
Dim x As Long
For r = 1 To Len(pressedKeys)
pressedKey = Mid$(pressedKeys, r, 1)
scnCode = GetScan(pressedKey)'// function returns hw scancode
x = SendMessage(pHwnd, WM_CHAR, Asc(pressedKey), scnCode)
Sleep interCharDelay
If pUnloading Then
Exit For
End If
DoEvents
Next r
working = False
End Sub
Thanks all.
Chris Hornberger
http://www.*-*-*.com/