3

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?

DevT
  • 4,843
  • 16
  • 59
  • 92
  • 1
    @Sirwani Has nothing to do with it. He wasn't to simulate keystrokes on an external application. – SimpleVar Jun 14 '12 at 10:28
  • Try a combination of WinApi call to SetForegroundWindow and SendKeys – SimpleVar Jun 14 '12 at 10:29
  • i have tried this one. but for that i need automationid. http://www.scip.be/index.php?Page=ArticlesNET20&Lang=EN – DevT Jun 14 '12 at 10:30
  • @YoryeNathan i need to automate . i have edited my question. plz check. thanx – DevT Jun 14 '12 at 11:17
  • 1
    Not 100%ly sure whether you mean keystroke (i. e. simulating the press of a key on keyboard) or buttonpush (i. e. simulating clicking with a mouse on a button). For the first, you do not need an AutomationId, for the latter, it is one way - since for the mouse click you will need screen coordinates, then you can use the ClickablePoint of the AutomationElement you got. For the first, use SendKeys.Send. – Andreas Reiff Jun 28 '12 at 14:35

6 Answers6

4

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.

Mare Infinitus
  • 8,024
  • 8
  • 64
  • 113
  • AutoIt can do that! but C# isn't required, as you can compile AutoIt script on its own. AND you can use it within C#... but it's not required – Mare Infinitus Jun 14 '12 at 14:28
4
  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"

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • 1
    Sometime, it will be useful to add time delay using Thread.Sleep(100) after SetForegroundWindow call and before sending keys. – Hassan Rahman Mar 22 '16 at 08:50
1

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.

Marco
  • 56,740
  • 14
  • 129
  • 152
1

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

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

Community
  • 1
  • 1
BugFinder
  • 17,474
  • 4
  • 36
  • 51
1

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

0

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. )

Community
  • 1
  • 1
DevT
  • 4,843
  • 16
  • 59
  • 92