Results 1 to 5 of 5

Thread: Sending Key press to an other program

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    157

    Sending Key press to an other program

    Hi,

    Does exist a way to simulate key press to an other program which is running ?
    Clearly, I've wrote a letter in a textbox, and then my program is telling to an other program that the equivalent key has been pressed.

    Thanks.

  2. #2
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Sending Key press to an other program

    This does what you want with Notepad.
    Put a textbox(multiline) and command button on a form. Cut and paste this code to the form's code module.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    4.     ByVal lpClassName As String, _
    5.     ByVal lpWindowCaption As String _
    6. ) As Long
    7.  
    8. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
    9.     ByVal hwndParent As Long, _
    10.     ByVal hwndChildAfter As Long, _
    11.     ByVal lpClassName As String, _
    12.     ByVal lpWindowCaption As String _
    13. ) As Long
    14.  
    15. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _
    16.     ByVal hwnd As Long, _
    17.     ByVal wMsg As Long, _
    18.     ByVal wParam As Long, _
    19.     ByVal lParam As Long _
    20. ) As Long
    21.  
    22. Private Const WM_CHAR = &H102
    23.  
    24. Private hWndNotePad As Long
    25. Private hWndEditBox As Long
    26. Private isLinked As Boolean
    27.  
    28. Private Sub Command1_Click()
    29.     Text1.Text = ""
    30.     'see if notepad program is running
    31.     hWndNotePad = FindWindow("Notepad", vbNullString)
    32.     If hWndNotePad = 0 Then
    33.         Call MsgBox("Start Notepad First", vbOKOnly)
    34.         Exit Sub
    35.     End If
    36.     'get handle to text area in notepad
    37.     hWndEditBox = FindWindowEx(hWndNotePad, 0&, "Edit", vbNullString)
    38.     'now we will link our text box input to Notepad's
    39.     isLinked = True
    40. End Sub
    41.  
    42. Private Sub Text1_KeyPress(KeyAscii As Integer)
    43.     If isLinked Then
    44.         PostMessage hWndEditBox, WM_CHAR, CLng(KeyAscii), 0&
    45.     End If
    46. End Sub

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    157

    Re: Sending Key press to an other program

    It's great ! This is what i've wanted and more, I understand message principes ! Thanks.

    I've change WM_CHAR with WM_KEYDOWN and I can control notepad like I'm working on it but I'm on my program in fact. However, in that way I've some troubles : I can't use ctrl+c or ctrl+v, the copy\paste function whereas I use ctrl+F, do you know why? I've changed postmessage api to sendmessage, then F3 key no longer open search notepad window -it's strange, isn't it? -.

    How use makepoints macro in vb?
    Last edited by IsWorking; Oct 31st, 2005 at 10:59 AM.

  4. #4
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Sending Key press to an other program

    You can play around with this:
    I subclassed the textbox
    Then forwarded either the WM_CHAR messages or the WM_KEYUP and WM_KEYDOWN messages to Notepad with mixed results
    Attached Files Attached Files

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    157

    Re: Sending Key press to an other program

    Thanks for this program, this is what I was trying to do. I have not already understood all but I'm progressing. Can we do the same things for mouse?
    I'm surprised why can I not launch context menu with special key or use alt button to get access to the menu ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width