C# 密钥访问另一个应用程序
我想将击键发送到另一个应用程序。例如:我打开了浏览器,我想将击键写入打开的记事本应用程序,而不定位记事本窗口。我只想让浏览器窗口始终处于活动状态。
是否可以?
I want to send keystrokes to another application. For example: I have opened a browser and I want write keystrokes to an opened notepad app without targeting notepad window. I just want for the browser window to be active the whole time.
Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您确实想将击键从网络浏览器发送到正在运行的Windows应用程序,那么这不应该是可能的。
原因是网络浏览器是(或应该)与操作系统和其他正在运行的应用程序隔离的沙箱,以防止不良分子通过互联网恶意干扰您的计算机。
If you really do mean sending keystrokes from a web browser to a running Windows application then this shouldn't be possible.
The reason is that web browsers are (or should be) sandboxed from the OS and other running applications to prevent undesirables from maliciously interfering with your computer via the internet.
如果您使用 Windows API,则可以,但是
SendInput
是唯一能保证正确执行此操作的 API,并且仅在聚焦窗口上执行此操作。对于其他窗口,您可以使用SendMessage
、WM_KEYUP
、WM_KEYDOWN
和WM_CHAR
,但它非常不可靠。对于应用程序来说,这样做通常也是不受欢迎的行为。You can, if you use the Windows API, however
SendInput
is the only one that will be guaranteed to do it properly, and only does so on the focused window. For other windows, you can useSendMessage
, withWM_KEYUP
,WM_KEYDOWN
andWM_CHAR
, but it's pretty unreliable. It's also usually undesirable behavior for an application to do this.