简体   繁体   中英

How to simulate key press event?

I need to type something in a textbox inside a web-browser control. Simply setting the value doesn't work because this doesn't run javascript.

So:

  1. InvokeMember: I thought I could use invokemember for this(since there's InvokeMember("Click") ). But google is silent about keyboard events.

  2. Focus the element and then send keys to the control. It works( Sendkeys.Send ), but only if the control is activated. If I switch to another window, it will send keys completely, but not where I want to.

  3. Javaspript: Is there a way to find out which javascript runs when I change the value of a textbox? I don't know much about javascript. Elements attributes don't have something like OnClick/OnKeyUp etc.

So... In case anybody is interested.

  1. InvokeMember doesnt have anything for key pressing.
  2. Its impossible to send keys to the control.
  3. There is a way to "send keys" using iQuery which... is fake and poorly-designed. It doesnt help in my case. And when it does "send key", "InvokeMember("OnKeyUp")" works as well.

What is possible? Its possible to send keys to a window... Teoretically its possible to send keys to unactive window(using SendMessage() and PostMessage()), but practically nobody has successfully done this since WinXP & x32 times...

I didnt try myself, but there are people who was able to send keys using "SendInput". But this useless anyway, cause the function can only send keys to active window.

So... SendKeys.Send(which, i guess, is a wrapper for SendInput) is still the best way around.

Solution

I created a form(window) for my webbrowser control. I initialize it, but dont use "Show". Then i simply make this form active right before "SendKeys.Send"(dont forget to focus on both element(inside the control) and control(inside the form) as well). Since there is no form to show, user doesnt notice anything and keys go to the right window. If its needed, its possible to save the handle of window which was active previously and make it active again after the sending(so focus wont be lost).

Summary

Although im ok with this solution and i will use it, its far from perfect... If anybody finds something better, please let me know(even in distant future).

You can, for each character, set it and call an event. In this example Im calling the keypress event. Call the event for the last character you typed.

he.setAttibtute("value", "a");
KeyEventArgs kea = new KeyEventArgs(Keys.A);
he.InvokeMember("onkeypress", kea);

Where he is the HtmlElement

About this: 'Javaspript. Is there a way to find out what javascript runs when i change a value of textbox? I dont know much about java. Element's attributes dont have something like OnClick/OnKeyUp etc.'

To view all javascript you can use firebug plugin for firefox. It has corresponding tab.

More info: http://getfirebug.com/whatisfirebug/

You can set up a break point and view all javascript variables you want.

In case anyone bumps into this looking for a framework agnostic way to fire any HTML and Mouse event, have a look here: How to simulate a mouse click using JavaScript?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM