Yogesh Yadav
Yogesh Yadav

Reputation: 133

Sendkeys event is not detected inside the application in windows form application

I am new to windows app development. I am developing a Windows Form Application where the layout is as follows:

There is one textbox and i have created the keyboard inside the application using SendKeys event.

Problem is that all other application on the system are able to detect the keys but the textbox inside the application is not able to detect the keys.

Basically app is having complete keyboard this is just one button press code

What I have tried:

public partial class Form1 : Form
{
    Control focusedC;
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams param = base.CreateParams;
            param.ExStyle |= 0x08000000;
            return param;
        }
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        FormBorderStyle = FormBorderStyle.None;
        WindowState = FormWindowState.Maximized;
        TopMost = true;
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Escape) {
            FormBorderStyle = FormBorderStyle.Sizable;
            WindowState = FormWindowState.Normal;
            TopMost = false;
        }
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        //checkbox is for CapsLock Key
    }

    private void button14_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked && focusedC != null)
        {
            focusedC.Focus();
            SendKeys.Send("Q");
        }
        else if(focusedC != null)
        {
            focusedC.Focus();
            SendKeys.Send("q");
        }
    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        focusedC = sender as TextBox;
    }
}

Upvotes: 0

Views: 899

Answers (2)

Of cource it doesn't work on your window. You set the WS_EX_NOACTIVATE style! It works on other windows but not yours, obviously. If you want it to work on your textbox remove or comment this line

param.ExStyle |= 0x08000000;

and it will work fine in your app window not others:

private void button14_Click(object sender, EventArgs e)
{
    if (checkBox1.Checked)
    {
        textBox1.Focus();
        SendKeys.Send("Q");
    }
    else
    {
        textBox1.Focus();
        SendKeys.Send("q");
    }
}

Upvotes: 1

Prany
Prany

Reputation: 2143

For WPF applications, you have to use SendKeys.SendWait() method.

SendKeys.SendWait("Q")

SendKeys.Send() will work for WinForm Applications.

Another option is to use WinAPI instead of SendKeys. More information here

Edit 1

Control focusedC;

//Enter event handler for your TextBox

private void textBox1_TextChanged(object sender, EventArgs e)
{
    focusedC = sender as TextBox;
}

//Click event handler 
private void  button14_Click(object sender, EventArgs e)
{
    if (focusedC != null)
    {
        focusedC.Focus();
        SendKeys.Send("Q");
    }
}

Edit 2: Using WinAPI

[DllImport("user32.dll")]
static extern void SendInput(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
  public static void PressKey(byte keyCode)
    {
        const int KEYEVENTF_EXTENDEDKEY = 0x1;
        const int KEYEVENTF_KEYUP = 0x2;
        SendInput((byte)keyCode, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
        SendInput((byte)keyCode, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

    }

Use by calling the PressKey function and Keycodes can be found here

Upvotes: 0

Related Questions