简体   繁体   English

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

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

i need to find out automationid for key board values?我需要找出关键板值的 automationid 吗? how to send keystrokes to application UI automation?如何将击键发送到应用程序 UI 自动化? i need to automate page up and page down function in key board.我需要在键盘中自动执行向上翻页和向下翻页功能。 What is the best way to do it?最好的方法是什么?

EDIT: in my application following the process.编辑:在我的应用程序中遵循该过程。 Assume end user open MS Word document with 5 pages and he presses page up and page down buttons to move within these pages.假设最终用户打开了 5 页的 MS Word 文档,他按下向上翻页和向下翻页按钮在这些页面内移动。 i want to automate this scenario using c#.我想使用 c# 自动化这个场景。 currently i have uses UIAutomationClient.dll and UIAutomationTypes.dll.目前我使用了 UIAutomationClient.dll 和 UIAutomationTypes.dll。 can i uses these?我可以用这些吗?

A very good way for automated sending of keystrokes of all kinds is the AutoIt automation and scripting language .自动发送各种击键的一个很好的方法是AutoIt 自动化和脚本语言 Lots can be done with it, especially page up and page down sending.它可以做很多事情,尤其是向上翻页和向下翻页发送。

I suggest writing an EXE with autoit, which connects itself to the program it will send keystrokes to.我建议用 autoit 编写一个 EXE,它将自己连接到它将向其发送击键的程序。

  1. Active the application's window using P/Invoke.使用 P/Invoke 激活应用程序的窗口。
  2. Call SendWait("C") with any character.使用任何字符调用SendWait("C")

Eg例如

// 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("=");
}

See How to: Simulate Mouse and Keyboard Events in Code > "To send a keystroke to a different application"请参阅如何:在代码中模拟鼠标和键盘事件>“将击键发送到不同的应用程序”

I think you want to send keystrokes to an application for which you don't have source code.我认为您想将击键发送到您没有源代码的应用程序。
I can't help you telling you how to do it directly in C#.我不能帮你告诉你如何直接在 C# 中完成。
But you can do it very easily with AutoIt ;但是你可以用AutoIt很容易地做到这一点; it has a DLL you can reference in C# to do exactly what you need.它有一个 DLL,您可以在 C# 中引用它来完全满足您的需要。

Have you read How to send keys instead of characters to a process?您是否阅读过如何将键而不是字符发送到进程?

This tells you exactly how to send keys to an application.这会准确地告诉您如何将密钥发送到应用程序。

// use a windows form and use this list with send keyhttps://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm I'm pretty sure he's just asking for values of the keys for his SendKey.Send("{}"); // 使用 Windows 窗体并将此列表与发送键一起使用https://www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm我很确定他只是在询问他的 SendKey.Send 的键值("{}");

in this scenario get the word document to fore ground and then used on screen key board在这种情况下,将 word 文档置于前台,然后在屏幕键盘上使用
System.Diagnostics.Process.Start("osk.exe"); and click page up and down buttons using mouse input since for the mouse click need screen coordinates for the page up and down button.并使用鼠标输入单击向上和向下翻页按钮,因为鼠标单击需要向上和向下翻页按钮的屏幕坐标。

( i tried to detect on screen key board using UI automation. but it did not detect keys of the screen. https://stackoverflow.com/questions/11077738/windows-sdk-inspect-tool-what-are-the-reasons-on-screen-keyboard-does-not-disp couldn't find solution for this problem.so because of that i use this move click method to click the button. ) (我尝试使用 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