简体   繁体   English

如何模拟按键事件?

[英]How to simulate key press event?

I need to type something in a textbox inside a web-browser control.我需要在 Web 浏览器控件内的textbox输入一些内容。 Simply setting the value doesn't work because this doesn't run javascript.简单地设置该值不起作用,因为这不会运行 javascript。

So:所以:

  1. InvokeMember: I thought I could use invokemember for this(since there's InvokeMember("Click") ). InvokeMember:我想我可以为此使用 invokemember(因为有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.它有效( Sendkeys.Send ),但前提是控件被激活。 If I switch to another window, it will send keys completely, but not where I want to.如果我切换到另一个 window,它将完全发送密钥,但不是我想要的。

  3. Javaspript: Is there a way to find out which javascript runs when I change the value of a textbox? Javaspript:当我更改文本框的值时,有没有办法找出运行哪个 javascript? I don't know much about javascript.我不太了解 javascript。 Elements attributes don't have something like OnClick/OnKeyUp etc.元素属性没有 OnClick/OnKeyUp 等。

So... In case anybody is interested.所以...万一有人感兴趣。

  1. InvokeMember doesnt have anything for key pressing. InvokeMember 没有任何按键功能。
  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.有一种使用 iQuery 来“发送密钥”的方法……这是假的且设计不佳。 It doesnt help in my case.在我的情况下它没有帮助。 And when it does "send key", "InvokeMember("OnKeyUp")" works as well.当它“发送密钥”时,“InvokeMember(“OnKeyUp”)”也可以工作。

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...可以将密钥发送到 window...理论上可以将密钥发送到非活动窗口(使用 SendMessage() 和 PostMessage()),但实际上自从 WinXP 和 x32 次以来没有人成功完成此操作...

I didnt try myself, but there are people who was able to send keys using "SendInput".我自己没有尝试过,但是有些人能够使用“SendInput”发送密钥。 But this useless anyway, cause the function can only send keys to active window.但这无论如何都没用,因为 function 只能将密钥发送到活动的 window。

So... SendKeys.Send(which, i guess, is a wrapper for SendInput) is still the best way around.所以... SendKeys.Send(我猜它是 SendInput 的包装器)仍然是最好的方法。

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).然后我只是在“SendKeys.Send”之前使这个表单处于活动状态(不要忘记同时关注元素(控件内部)和控件(表单内部))。 Since there is no form to show, user doesnt notice anything and keys go to the right window.由于没有要显示的表格,因此用户不会注意到任何内容并将 go 键在右侧 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).如果需要,可以保存之前激活的 window 的句柄,并在发送后再次激活(这样焦点不会丢失)。

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他在哪里 HtmlElement

About this: 'Javaspript.关于这个:'Javaspript。 Is there a way to find out what javascript runs when i change a value of textbox?当我更改文本框的值时,有没有办法找出 javascript 运行的内容? I dont know much about java.我不太了解 java。 Element's attributes dont have something like OnClick/OnKeyUp etc.'元素的属性没有 OnClick/OnKeyUp 等。

To view all javascript you can use firebug plugin for firefox.要查看所有 javascript,您可以使用 firefox 的 firebug 插件。 It has corresponding tab.它有相应的选项卡。

More info: http://getfirebug.com/whatisfirebug/更多信息: http://getfirebug.com/whatisfirebug/

You can set up a break point and view all javascript variables you want.您可以设置一个断点并查看您想要的所有 javascript 变量。

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?如果有人遇到这种情况,正在寻找一种与框架无关的方式来触发任何 HTML 和鼠标事件,请看这里: 如何使用 JavaScript 模拟鼠标点击?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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