Win API - Sending keystrokes to a hWnd 
Author Message
 Win API - Sending keystrokes to a hWnd

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

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


    Next r

    working = False

End Sub

Thanks all.
Chris Hornberger


Tue, 14 Sep 2004 00:01:26 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Sending Keystrokes with API

2. Question about sending keystrokes to another window w/ API

3. Sending keystrokes via API call

4. WIN 2k API vs WIN 98/WIN NT API - Help needed

5. How to monitor keystrokes in win 95 ?

6. Help: Keystrokes fm win app to dos app

7. DataList Control won't match user's keystrokes when list too long

8. DataList Control won't match user's keystrokes when list too long

9. Win 98,Win XP and API

10. Sending keystrokes...........

11. Win API Call not working in app distributed to WIn 98

12. send keystrokes to a web page


Powered by phpBB® Forum Software