frz
frz

Reputation: 59

How do I detect if a key is pressed without using a TextBox?

I have looked on Stack Overflow and Google but I have yet to find an answer to my question. I want to detect when a key is pressed without the need of a text box.

Upvotes: 0

Views: 148

Answers (2)

Daniel A.A. Pelsmaeker
Daniel A.A. Pelsmaeker

Reputation: 50276

All controls (including forms) have the KeyPress, KeyDown and KeyUp events that you may use.

And there are corresponding OnKeyPress, OnKeyDown and OnKeyUp methods that you can override in your own controls.

Upvotes: 0

sa_ddam213
sa_ddam213

Reputation: 43596

Override OnKeyPress main Form, this will catch the key presses

Winforms:

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);
    }

WPF: (will have to use OnKeyDown or OnKeyUp

    protected override void OnKeyDown(KeyEventArgs e)
    {
        base.OnKeyDown(e);
    }

Upvotes: 1

Related Questions