N
N
Nika2018-09-26 23:57:42
C++ / C#
Nika, 2018-09-26 23:57:42

How can I make the program window close when I press a key?

It is necessary that the program closes when you press 'd' and does nothing when you press any other key

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Peter, 2018-09-27
@glamurniy_kluben

Something like this.
У формы есть событие KeyPress и KeyDown.

if(e.KeyChar == 'д')
            {
                Application.Exit();
            }

Или так по событию KeyDown
private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.L)
            {
                Application.Exit();
            }
        }

If for console.
Console
do
        {
           //Тут что надо делать. Или не надо.
        } while (Console.ReadKey().Key != ConsoleKey.L);

A
Andrey, 2018-09-27
@andrey71

Was looking for a method like this. Found, on a stack like...
For WPF.

IntPtr hWnd = CloseThisWindow.FindWindowByCaption(IntPtr.Zero, "НАЗВАНИЕ ОКНА");
            bool ret = CloseThisWindow.CloseWindow(hWnd);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question