SendKeys does not work in C # in Visual Studio

1

I want an application that when pressing a button, the keys Alt + F4 are automatically pressed to close for example the form (it would be easier to close it by code and not having two keys pressed, but it is an example).

In short, SendKeys does not work for me because I press the button and the form does not close:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void Form1_DoubleClick(object sender, EventArgs e)
    {
        SendKeys.Send("%{F4}");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        System.Windows.Forms.SendKeys.SendWait("%{F4}");
    }
}

'

It does not work for me in any of the two ways, so I'm just trying to close the window or double-click on the form or click on the button. But it is not doing the keystrokes. I have tried something as simple as double-clicking the ENTER key to press the button, but it does not work either.

Greetings.

    
asked by M. Giner 28.11.2018 в 13:26
source

1 answer

0

Test with parentheses

  

SendKeys.SendWait ("(% {F4})");

However SendKeys does not work on all operating systems and will not always behave as you want. Keep in mind that you are trying to send these keystrokes to the application, and the operating system usually intercepts them before. In the case of Windows 7 and Vista, too soon (before it is sent).

I recommend using inputsimulator: link

Is responsible for doing all the hard work of exposing the methods Win32 SendInput to C #. This allows you to directly send the Windows keys.

    
answered by 28.11.2018 в 13:41