How to send keystrokes to application UI automation -C#

杀马特。学长 韩版系。学妹 提交于 2019-12-12 13:55:15

问题


i need to find out automationid for key board values? how to send keystrokes to application UI automation? 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. i want to automate this scenario using c#. currently i have uses UIAutomationClient.dll and UIAutomationTypes.dll. can i uses these?


回答1:


A very good way for automated sending of keystrokes of all kinds is the AutoIt automation and scripting language. 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.




回答2:


  1. Active the application's window using P/Invoke.
  2. Call SendWait("C") with any character.

E.g.

// 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"




回答3:


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#.
But you can do it very easily with AutoIt; it has a DLL you can reference in C# to do exactly what you need.




回答4:


Have you read How to send keys instead of characters to a process?

This tells you exactly how to send keys to an application.




回答5:


in this scenario get the word document to fore ground and then used on screen key board
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. )




回答6:


// use a windows form and use this list with send key https://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("{}");



来源:https://stackoverflow.com/questions/11031081/how-to-send-keystrokes-to-application-ui-automation-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!