AlbA
AlbA

Reputation: 81

Check if PAGEUP or PAGEDOWN is pressed

I have a windows-form application, with pages in it.

Short and simple question;

How to I check wether the Page-UP or Page-Down key is pressed when in the windows-form?

The purpose of this is, so that I can go through the pages by clicking one of those 2 buttons.

Upvotes: 2

Views: 5473

Answers (2)

Renatas M.
Renatas M.

Reputation: 11820

Set KeyPreview property on your form. And add form key down event handler:

private void form_KeyDown(object sender, KeyEventArgs e)
{
   if(e.KeyCode == Keys.PageUp)
      //do something on page up
   if(e.KeyCode == Keys.PageDown)
      //do something on page down
}

Upvotes: 6

Alex Filatov
Alex Filatov

Reputation: 2321

You should override ProcessCmdKey method

protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
        if (keyData == Keys.PageUp) {
            MessageBox.Show("Pressed PageUp");
            return true;
        }
        if (keyData == Keys.PageDown) {
            MessageBox.Show("Pressed PageDown");
            return true;
        }
        return base.ProcessCmdKey(ref msg, keyData);
    }

Upvotes: 4

Related Questions