SendKeys.Send()
method does'nt work in all situations because Send()
method can be addressed to another Control !
In 2020, the best solution to be sure at 100% is to use SendMessage() function.
In VB.Net, I use following code
Public Declare Auto Function SendMessage Lib "user32.dll"
( ByVal hWnd As IntPtr
, ByVal wMsg As Int32
, ByVal wParam As Int32
, ByVal s As Int32
) As Int32
Private Const WM_CHAR As Int32 = &H102
SendMessage(txtInput.Handle, WM_CHAR, AscW(sValue.Chars(0)), 0)
Where txtInput
is a sample Textbox
control.
In my case, I click on html <div>
element defined in a WebView
control that changes color when I click on it or when mouse move over. When I click on this <div>
a ScriptNotify() VB.Net method is called to enter some characters in Textbox
using SendMessage()
function.
In old solution, I think that sometimes, WebView
gain focus before SendKeys.Send()
method is called so that propagated key is not send to txtInput
Textbox.
You can use old solution, but be certain that one day, SendKeys.Send() function will send what you want to another Handle.