如何模拟按键事件? [英] How to simulate key press event?

查看:88
本文介绍了如何模拟按键事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Web浏览器控件的文本框中键入一些内容。简单地设置值是行不通的,因为它不能运行javascript。

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.

因此:


  1. InvokeMember:我想可以为此使用invokemember(因为存在 InvokeMember( Click))。但是Google对键盘事件保持沉默。

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

将焦点放在元素上,然后将键发送到控件。它可以正常工作( Sendkeys.Send ),但是仅当控件被激活时才有效。如果我切换到另一个窗口,它将完全发送密钥,但不会发送我想要的位置。

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.


推荐答案

所以...如果有人有兴趣。

So... In case anybody is interested.


  1. InvokeMember没有任何按键可用。


  2. 有一种使用iQuery来发送密钥的方法,它是伪造的,设计欠佳。在我的情况下没有帮助。并且当它执行发送密钥时, InvokeMember( OnKeyUp)也将起作用。

可能的事可以将键发送到窗口...从理论上讲,可以将键发送到非活动窗口(使用SendMessage()和PostMessage()),但是实际上从WinXP& No. x32次...

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...

我没有尝试过,但是有些人能够使用 SendInput发送密钥。但这仍然无济于事,导致该功能只能将键发送到活动窗口。

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.

所以... SendKeys.Send(我想这是SendInput的包装器)仍然是最好的方法。

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

解决方案

我创建了一个表单(窗口)用于我的网络浏览器控件。我初始化它,但不要使用显示。然后,我只是在 SendKeys.Send之前使该表单处于活动状态(不要忘记同时关注元素(在控件内)和控件(在表单内))。由于没有表格可显示,因此用户不会注意到任何东西,并且按键会转到右侧窗口。如果需要,可以保存先前处于活动状态的窗口句柄,并在发送后使其再次处于活动状态(这样就不会丢失焦点)。

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).

总结

虽然我可以使用此解决方案,但我会使用它,但它远非完美……如果有人发现更好的方法,请让我知道(甚至在遥远的未来。)

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).

这篇关于如何模拟按键事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆