C# 将密钥发送到其他应用程序到特定的文本字段

C# sendkeys to other application to particular textfield(C# 将密钥发送到其他应用程序到特定的文本字段)
本文介绍了C# 将密钥发送到其他应用程序到特定的文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 C# 将其他应用程序的密钥发送到其他应用程序中的特定文本字段.

I need to sendkeys to other application to particular textfield in other application using C#.

这可能吗?如果是的话,谁能给我示例代码?

Is that possible? If yes, can anyone give me sample code?

推荐答案

SendKeys 仅将击键组合发送到活动窗口.

因此,如果您的 C# 应用程序在正确的时间调用 SendKeys 函数,那么当用户关注其他应用程序的文本框时,一切都会正常工作.

So if your C# application calls the SendKeys function at precisely the right time, when the user has that other application's textbox focused, everything will work just fine.

当然,问题在于现实世界很少如此完美.您不知道用户何时将关注其他应用程序,当然也不知道他们是否单击了文本框控件.您也无法让用户告诉您的 C# 应用程序何时发送文本(例如单击按钮),因为为了单击按钮,您的 应用程序必须具有前台焦点,因此不是您要接收键盘输入的应用程序.

The problem, of course, is that the real world is rarely this perfect. You don't know exactly when the user will have the other application focused, and certainly won't know if they've clicked in the textbox control. You also won't have any way for the user to tell your C# application when to send the text (such as clicking a button) because in order to click the button, your application must have the foreground focus, and therefore not the application that you want to receive the keyboard input.

这只是对一些在尝试以这种方式进行 UI 自动化时可能遇到的潜在问题的简要概述.这是非常脆弱的,很容易导致用户出现一些意想不到的、令人愤怒的行为.我强烈建议不要这样做.更好的选择是直接与其他应用程序通信,例如通过向其发送消息,而不是尝试侵入性地控制其 UI.

And that's just a brief overview of some of the potential problems you'll encounter when trying to do UI automation this way. This is extremely fragile, and can easily lead to some unexpected, infuriating behavior for the user. I strongly recommend against it. A much better option is communicating directly with the other application, such as by sending it messages, rather than trying to invasively control its UI.

如果你绝对必须这样做,尽管所有的建议都是相反的,最好的方法是获取你想要发送文本的窗口的句柄,然后发送一个 WM_SETTEXT 消息.当然,这需要您从 Win32 API 中 P/Invoke 一些函数.请参阅此问题的答案一些示例代码.

If you absolutely must do this, despite all recommendations to the contrary, the best way is to get the handle to the window you want to send the text to, and then send a WM_SETTEXT message. That will, of course, require that you P/Invoke a few functions from the Win32 API. See the answers to this question for some sample code.

这篇关于C# 将密钥发送到其他应用程序到特定的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)