SendKeys VB.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21220844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SendKeys VB.net
提问by MikeyT
Hi ive developed a application that works with my site by using
嗨,我开发了一个可以与我的网站一起使用的应用程序
SendKeys.send("{ENTER}") to submit info on one of my forms.
SendKeys.send("{ENTER}") 在我的表单之一上提交信息。
Is there a way to stop it from running outside the application?
有没有办法阻止它在应用程序之外运行?
For example im trying to run the program in the background and when im browsing my facebook or on google it randomly keeps hitting enter.
例如,我试图在后台运行该程序,当我浏览我的 facebook 或在谷歌上时,它会随机地一直按回车键。
Any help is greatly appreciated thanks for your time.
感谢您抽出宝贵时间提供任何帮助。
回答by Basic
The short answer is to look at the windows available for a process and check the titlesto see if it's the page you want.
简短的回答是查看进程可用的窗口并检查标题以查看它是否是您想要的页面。
If System.Diagnostics.Process.GetProcessesByName("firefox").MainWindowTitle = 'My Page Title'
...
End If
That said, there are much better ways to do this - If you're using firefox, look into GreaseMonkey, if in Chrome, look at TamperMonkey.
也就是说,有更好的方法可以做到这一点 - 如果您使用 firefox,请查看GreaseMonkey,如果在 Chrome 中,请查看TamperMonkey。
Both allow you to inject custom javascript into any page whose url matches a pattern you choose. In effect, you could write some Javascript to submit a form(say) 30 seconds after page load.
两者都允许您将自定义 javascript 注入任何 url 与您选择的模式匹配的页面。实际上,您可以编写一些 Javascript 以在页面加载 30 秒后提交表单(例如)。
This would have the benefit of working if a different tab is selected as well as not requiring a whole application.
如果选择了不同的选项卡并且不需要整个应用程序,这将有好处。
回答by JaredPar
The SendKeys.Sendmethod will indiscriminately send the key stroke to the active application. There is no way to use this API to target a specific application.
该SendKeys.Send方法会不加选择地将击键发送到活动应用程序。无法使用此 API 来针对特定应用程序。
You could change your app to try and verify the active application is the one you want to send keys too. This is destined to be a flaky process though. No matter how good your verification is it's always possible that the active app is switched to another app after your verification completes.
您可以更改您的应用程序以尝试验证活动应用程序是否也是您想要发送密钥的应用程序。但这注定是一个不稳定的过程。无论您的验证有多好,在您的验证完成后,活动应用程序总是有可能切换到另一个应用程序。
If VerifyActiveAppIsTarget() Then
SendKeys.Send("{Enter}") ' Active app could change before this runs
I would persue a different solution for sending data between my apps
我会采用不同的解决方案在我的应用程序之间发送数据

