繁体   English   中英

如何将击键发送到应用程序 UI 自动化 -C#

[英]How to send keystrokes to application UI automation -C#

我需要找出关键板值的 automationid 吗? 如何将击键发送到应用程序 UI 自动化? 我需要在键盘中自动执行向上翻页和向下翻页功能。 最好的方法是什么?

编辑:在我的应用程序中遵循该过程。 假设最终用户打开了 5 页的 MS Word 文档,他按下向上翻页和向下翻页按钮在这些页面内移动。 我想使用 c# 自动化这个场景。 目前我使用了 UIAutomationClient.dll 和 UIAutomationTypes.dll。 我可以用这些吗?

自动发送各种击键的一个很好的方法是AutoIt 自动化和脚本语言 它可以做很多事情,尤其是向上翻页和向下翻页发送。

我建议用 autoit 编写一个 EXE,它将自己连接到它将向其发送击键的程序。

  1. 使用 P/Invoke 激活应用程序的窗口。
  2. 使用任何字符调用SendWait("C")

例如

// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

// Send a series of key presses to the Calculator application. 
private void button1_Click(object sender, EventArgs e)
 {
    // Get a handle to the Calculator application. The window class 
    // and window name were obtained using the Spy++ tool.
    IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator");

    // Verify that Calculator is a running process. 
    if (calculatorHandle == IntPtr.Zero)
    {
        MessageBox.Show("Calculator is not running.");
        return;
    }

    // Make Calculator the foreground application and send it  
    // a set of calculations.
    SetForegroundWindow(calculatorHandle);
    SendKeys.SendWait("111");
    SendKeys.SendWait("*");
    SendKeys.SendWait("11");
    SendKeys.SendWait("=");
}

请参阅如何:在代码中模拟鼠标和键盘事件>“将击键发送到不同的应用程序”

我认为您想将击键发送到您没有源代码的应用程序。
我不能帮你告诉你如何直接在 C# 中完成。
但是你可以用AutoIt很容易地做到这一点; 它有一个 DLL,您可以在 C# 中引用它来完全满足您的需要。

您是否阅读过如何将键而不是字符发送到进程?

这会准确地告诉您如何将密钥发送到应用程序。

// 使用 Windows 窗体并将此列表与发送键一起使用https://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm我很确定他只是在询问他的 SendKey.Send 的键值("{}");

在这种情况下,将 word 文档置于前台,然后在屏幕键盘上使用
System.Diagnostics.Process.Start("osk.exe"); 并使用鼠标输入单击向上和向下翻页按钮,因为鼠标单击需要向上和向下翻页按钮的屏幕坐标。

(我尝试使用 UI 自动化检测屏幕键盘。但它没有检测到屏幕的按键。https://stackoverflow.com/questions/11077738/windows-sdk-inspect-tool-what-are-the-reasons -on-screen-keyboard-does-not-disp无法找到解决此问题的方法。因此,我使用此移动点击方法来点击按钮。)

暂无
暂无

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

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