Search code examples
switch-statementsendkeys

Switching to another application (previously active) on button click in .net


Hi I want to do something like on screen keyboard. I want user to click a button on inactive application and then key press will be sent to active application while keeping active application active. I wrote the code for hover event of the button in inactive application and it is working. But what I want is to do it in click event. It is not working because inactive application becomes active. The code is below for hover event. Thank you.


private void button1_MouseHover(object sender, EventArgs e)
{
    SendKeys.Send("{TAB}");
}


Solution

  • Finally I could figure out a way to do it. Refer the code below.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    using System.Diagnostics;
    using System.Timers;
    using tmr = System.Timers;
    using System.Threading;
    
    namespace KeyPressTest
    {
        public partial class Form1 : Form
        {
            [DllImport("user32.dll")]
            static extern int GetForegroundWindow();
    
            [DllImport("user32")]
            private static extern UInt32 GetWindowThreadProcessId(Int32 hWnd, out Int32 lpdwProcessId);
    
            [DllImport("user32.dll")]
            static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    
            [System.Runtime.InteropServices.DllImport("user32.dll")]
            private static extern bool SetForegroundWindow(IntPtr hWnd);
    
            tmr.Timer tm = new tmr.Timer();
            Int32 hwnd = 0;
    
            private Int32 GetWindowProcessID(Int32 hwnd)
            {
                Int32 pid = 1;
                GetWindowThreadProcessId(hwnd, out pid);
                return pid;
            }     
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                tm.Elapsed += Timer_Elapsed;
                tm.Interval = 100;
                tm.Start();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                SetForegroundWindow((IntPtr)hwnd);
                Thread.Sleep(40);
                SendKeys.Send("{TAB}");
            }       
    
            private void Timer_Elapsed(object sender, System.EventArgs args)
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new MethodInvoker(delegate
                    {
                        if (this.Handle.ToInt32() != GetForegroundWindow())
                        {
                            hwnd = GetForegroundWindow();
                        }
                    }));
                }
                else
                {
                    if (this.Handle.ToInt32() != GetForegroundWindow())
                    {
                        hwnd = GetForegroundWindow();
                    }
                }
                if (hwnd == 0)
                {
                    return;
                }
                string appProcessName = "";
                string appExePath = "";
                string appExeName = "";
                try
                {
                    appProcessName = Process.GetProcessById(GetWindowProcessID(hwnd)).ProcessName;
                    appExePath = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.FileName;
                    appExeName = appExePath.Substring(appExePath.LastIndexOf(@"\") + 1);
                }
                catch (Exception ex)
                {
                }
                if (textBox1.InvokeRequired)
                {
                    textBox1.Invoke(new MethodInvoker(delegate
                    {
                        textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
                    }));
                }
                else
                {
                    textBox1.Text = appProcessName + " | " + appExePath + " | " + appExeName;
                }
            }
        }
    }