Send The Enter Key to a Webbrowser1
Hello.
I tried with ;
PostMessage(embeddedwb1.Handle,VK_RETURN,0,0);
sendmessage(embeddedwb1,wm_keyup,vk_return,0);
embeddedwb1.perform(wm_keyup,vk_return,0);
but my program doesn't work. and i tried with ;
Keybd_Event(VK_RETURN, 1, 0, 0);
my program is worked.but that is code for sending enter key for All Applications.i want to code for just my program.Than开发者_JAVA技巧k you for helping me.
Make sure the WebBrowser is focused using SetFocus(embeddedwb1.Handele)
before calling Keypd_Event(...)
.
Since you probably want to submit a form, did you consider doing that with the DOM? Here's some code that fills in the selected input field and then submits the first form. This works just fine if you navigate to http://www.google.com, but in the real world you'd need to be a lot more careful (search the input field by class or id, check if there's a form, etc)
procedure TForm23.Button2Click(Sender: TObject);
begin
((W.Document as IHTMLDocument2).activeElement as IHTMLInputElement).value := 'Search';
((W.Document as IHTMLDocument2).forms.item(0, '') as IHTMLFormElement).submit;
end;
if Javascript is used for submit form then Cosmin answer doesn't work I think. In this case you must know JS code of the button and execute it from your program. For example; in my page, I use JQuery to submit form. So:
procedure TForm1.Button1Click(Sender: TObject);
var
js_code : string;
Win : IHTMLWindow2;
begin
Win := (WebBrowser1.Document as IHTMLDocument2).parentWindow;
js_code := '$("#formlogin").submit();';
Win.execScript(js_code, 'JavaScript');
end;
Use this:
PostMessage(EmbeddedWB1.HWND, WM_CHAR, 13, 0);
This code send char 13 (Enter code) to EmbeddedWB.
精彩评论