Control an app from another application for voice commands

  • Thread starter Thread starter AMITSAINI2020
  • Start date Start date
A

AMITSAINI2020

Guest
Hello Everyone ,My motive is to control visual studio by voice commands by using microsoft speech services.

I want to control visual studio from my another window form app in c#. I have a little bit success but there is some problem in SendKeys to the application.


As i have open two instances of visual studio. when i run my program from visual studio , it works fine. i am giving Ctrl+N command (^n)by using send keys to add new File Dialog box. On run my program, the control goes to another instance and open add new file dialog box . it works well .

But when i run exe directly from bin folder, it does not send keys to Visual studio instance. Exe Finds well of window of visual studio but not open file Dialog box. There is some problem for setForgroundWindow(handel) method.



using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ControlVs
{
public partial class Form1 : Form
{
IntPtr handle = new IntPtr(0);

[DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]

[return: MarshalAs(UnmanagedType.Bool)]

internal static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow);
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
handle = FindWindow(@"HwndWrapper[DefaultDomain;;f515415e-f7ae-41b2-b5fd-11635f9b28a9]", null);
ShowWindow(handle, 5);
label1.Text = Convert.ToString(handle);
if (SetForegroundWindow(handle))

{
System.Threading.Thread.Sleep(1000);
/*

* Send "Hello World!" to the active windows.

* Then send TAB key to skip to the button.

* Send ENTER key means press the button there.

*/
// SendKeys.Send(@"^{v}");
SendKeys.SendWait(@"^{n}");
// SendKeys.SendWait("{H}{e}{l 2}{o} {W}{o}{r}{l}{d}{!}");

// SendKeys.SendWait("{TAB}");

//SendKeys.SendWait("{ENTER}");

}
}
}
}

Continue reading...
 
Back
Top