VB.NET SendMessage() KeyStrokes.
Hi,
I have opened the NotePad and I want to send key strokes using SendMessage. I have got parent and child window and tried to send keystrokes using SendMessage() but didn't work. Why ? As far as I know the code is correct.
PHP Code:
Const WM_KEYDOWN As Integer = &H100
Const WM_KEYUP As Int32 = &H101
Const VK_RETURN As Integer = &HD
Dim hWnd_notepad As IntPtr = FindWindow(Nothing, "Untitled - Notepad")
Dim hWnd_notepad_class As IntPtr = FindWindowEx(hWnd_notepad, Nothing, "Edit", Nothing)
SendMessage(hWnd_notepad_class, WM_KEYDOWN, VK_RETURN, 0)
SendMessage(hWnd_notepad_class, WM_KEYUP, VK_RETURN, 0)
Any help would be appreciated.
Thanks!
Re: VB.NET SendMessage() KeyStrokes.
Re: VB.NET SendMessage() KeyStrokes.
Quote:
Originally Posted by
abhishek009
Hi,
I have opened the NotePad and I want to send key strokes using SendMessage. I have got parent and child window and tried to send keystrokes using SendMessage() but didn't work. Why ? As far as I know the code is correct.
FWIW, Testing on Win7 & 10 only here.
That code works in 7 but not in 10, tried using PostMessage instead and it works on both OS.
Re: VB.NET SendMessage() KeyStrokes.
Actually I'm trying on the web browser control. I have a text box or text view on the control and want to send keystroke to it. PostMessage worked for NotePad. I have got the handler for the Web Browser control. Focused on the text element but it didn't send the text. Can you check if it's working for web browser too ?
Thanks !
P.S : I don't want to use SendKeys or HTML method to set the text.
Re: VB.NET SendMessage() KeyStrokes.
Quote:
Originally Posted by
abhishek009
P.S : I don't want to use SendKeys or HTML method to set the text.
Quick test using html file (called SpellCheck) that is just a textbox and nothing else and this worked on Chrome browser, I never had any luck using APIs like this on browsers tho. You might want to try using UIAutomation, IIRC that can send stuff to browser elements. Good Luck!
Code:
Dim hWnd As IntPtr = FindWindow("Chrome_WidgetWin_1", "SpellCheck - Google Chrome")
If hWnd <> IntPtr.Zero Then
Dim HwndEdit As IntPtr = FindWindowEx(hWnd, IntPtr.Zero, "Chrome_RenderWidgetHostHWND", "Chrome Legacy Window")
If HwndEdit <> IntPtr.Zero Then
SetForegroundWindow(hWnd) ' bring chrome to foreground
' send return key
PostMessage(HwndEdit, WM_KEYDOWN, VK_RETURN, 0)
PostMessage(HwndEdit, WM_KEYUP, VK_RETURN, 0)
Else
MessageBox.Show("'Chrome_RenderWidgetHostHWND' not found!")
End If
Else
MessageBox.Show("'Chrome_WidgetWin_1' not found!")
End If
Re: VB.NET SendMessage() KeyStrokes.
Thank you for your help. I have never heard of UIAutomation. Can you give me the link and documentation ? I searched on google but no luck.
Thanks again! :)