N
N
NikitaSova2020-09-28 10:14:40
C++ / C#
NikitaSova, 2020-09-28 10:14:40

Why does nothing happen when I press the keyboard?

Hi all.
I wrote a simple code, the essence of which is that by pressing the spacebar on the keyboard, a MessageBox appears and writes that the button is pressed. But for some reason, nothing happens at all when I press the spacebar, there is not even an error. I do not understand what the problem is. Help the fool please.
the code:

using System;
using System.Windows.Forms;
class MyButtonClass : Form
{
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Space)
        {
            MessageBox.Show("кнопка нажата");
        }
    }
    static void Main()
    {
        Application.Run(new MyButtonClass());
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-09-28
@yarosroman

Set the form's KeyPreview property to true

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question