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.
Printable View
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.
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:
Option Explicit Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _ ByVal lpClassName As String, _ ByVal lpWindowCaption As String _ ) As Long Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _ ByVal hwndParent As Long, _ ByVal hwndChildAfter As Long, _ ByVal lpClassName As String, _ ByVal lpWindowCaption As String _ ) As Long Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" ( _ ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long _ ) As Long Private Const WM_CHAR = &H102 Private hWndNotePad As Long Private hWndEditBox As Long Private isLinked As Boolean Private Sub Command1_Click() Text1.Text = "" 'see if notepad program is running hWndNotePad = FindWindow("Notepad", vbNullString) If hWndNotePad = 0 Then Call MsgBox("Start Notepad First", vbOKOnly) Exit Sub End If 'get handle to text area in notepad hWndEditBox = FindWindowEx(hWndNotePad, 0&, "Edit", vbNullString) 'now we will link our text box input to Notepad's isLinked = True End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) If isLinked Then PostMessage hWndEditBox, WM_CHAR, CLng(KeyAscii), 0& End If End Sub
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? :confused: -.
How use makepoints macro in vb?
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
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 ?