Click here to Skip to main content
15,888,044 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

What I have tried:

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

            }
        }
    }
}




namespace ControlVs
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(108, 97);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(382, 106);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(18, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "HI";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label1;
    }
}
Posted
Comments
Amit Saini 2-Aug-20 7:57am    
FindWindow(@"HwndWrapper[DefaultDomain;;f515415e-f7ae-41b2-b5fd-11635f9b28a9]", null);

is my visual studio instance class name . you can find class name by using spy++
BillWoodruff 2-Aug-20 9:30am    
Have you checked out the various tools available:

https://www.google.com/search?sxsrf=ALeKk032jjOHSUfDqYUQowT74OMIBa2NRA%3A1596374858101&ei=Sr8mX7vYBcH79QPBhJroBQ&q=c%23+voice+control+visual+studio&oq=c%23+voice+control+visual+studio&gs_lcp=CgZwc3ktYWIQAzIICCEQFhAdEB4yCAghEBYQHRAeMggIIRAWEB0QHjIICCEQFhAdEB4yCAghEBYQHRAeMggIIRAWEB0QHjIICCEQFhAdEB46BAgAEEc6BggAEAcQHjoICAAQBxAFEB46CAgAEAgQBxAeOgYIABAIEB46BAghEApQ3oMBWKTPAWDZ_gFoAnABeACAAc0BiAHKCpIBBjE1LjAuMZgBAKABAaoBB2d3cy13aXrAAQE&sclient=psy-ab&ved=0ahUKEwi7peHiz_zqAhXBfX0KHUGCBl0Q4dUDCAw&uact=5
Amit Saini 3-Aug-20 12:49pm    
Yes i have tried these voice command tools. It is based windows speech recognition library not microsoft based speech api. So it does not understand as good as cognitive services do for understanding pronunciation of my voice.
Amit Saini 3-Aug-20 13:00pm    
I am little bit close to my project. I have used cognitive services of Microsoft. Used it.But issue is setForgroundWindow. I think i have to add cognitive services of Microsoft in current working project for control develop to work on company projects and not by another application. i will keep two copies with and without voice commands of projects.

Now i will have to found automate browser so that i can write code and search on google with voice commands so that i don't need to sit on chairs for 8 hours long for doing programming.
[no name] 4-Aug-20 13:27pm    
Target an app designed for automation. No Morse code required.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900