Note that there are some explanatory texts on larger screens.

plurals
  1. PODevice Manager Interaction Sendkeys
    text
    copied!<p>I'm trying to write a simple console program that will launch device manager and by using keystrokes, navigate though it. So far i cannot get they key stroke to register in Device manager. </p> <p>The program launches device manager fine but no keystrokes seem to be working inside device manager. I know the tree part of device manager is called SysTreeView32 by using Spy++. </p> <p>Any suggestions? </p> <p>Here is my code so far. </p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Windows.Forms; using System.Threading; using System.Runtime.InteropServices; namespace Dev_Mgr_Auto { class Program { [DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lp1, string lp2); [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetForegroundWindow(IntPtr hWnd); static void Main(string[] args) { //start Device Manager Process.Start("devmgmt.msc"); Thread.Sleep(1500); // find window handle of Device manager IntPtr handle = FindWindow("MMCMainFrame", "Device Manager"); if (!handle.Equals(IntPtr.Zero)) { // activate Device Danager window if (SetForegroundWindow(handle)) { // send key "Tab" SendKeys.SendWait("{TAB}"); // send key "Down" x 4 SendKeys.SendWait("DOWN 4"); } } }//end main }//end class }// end program </code></pre> <p><strong>EDIT1: You must run the program as administrator for it to work.</strong> </p> <p>Final Code: </p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Windows.Forms; using System.Threading; using System.Runtime.InteropServices; using System.Windows.Automation; namespace Video_Card_Updater { class Program { [DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string lp1, string lp2); [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam); static void Main(string[] args) { // this code needs a reference to UIAutomationClient and UIAutomationTypes Process process = Process.Start("devmgmt.msc"); do { process.Refresh(); Thread.Sleep(100); } while (process.MainWindowHandle == IntPtr.Zero); // get root element that corresponds to the process main window AutomationElement mainWindow = AutomationElement.FromHandle(process.MainWindowHandle); // get the first tree view control by its class name AutomationElement treeView = mainWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ClassNameProperty, "SysTreeView32")); // get the "Keyboards" node by its name AutomationElement Display = treeView.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "Display adapters")); // expand item ((ExpandCollapsePattern)Display.GetCurrentPattern(ExpandCollapsePattern.Pattern)).Expand(); // get first display AutomationElement firstDisplay = Display.FindFirst(TreeScope.Children, PropertyCondition.TrueCondition); // set focus to display and do the following key commands: firstDisplay.SetFocus(); SendKeys.SendWait("{ENTER}"); Thread.Sleep(100); SendKeys.SendWait("{TAB 3}"); Thread.Sleep(100); SendKeys.SendWait("{RIGHT}"); Thread.Sleep(100); //ALT + P SendKeys.SendWait("%P"); Thread.Sleep(100); SendKeys.SendWait("{ENTER}"); } // end main }//end class }// end program </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload