Axarydax
Axarydax

Reputation: 16623

c# capture Ctrl+PageUp keystroke

I am having trouble capturing Ctrl+PageUp keystroke in a ListView control in WinForms application.

I am using this code to capture keystrokes -

private void ListViewEx_KeyDown(object sender, KeyEventArgs e)
{
...
if(e.Control){
if((e.KeyCode ^ Keys.Left) == 0)
    MessageBox.Show("Left"); //shows messagebox
else if((e.KeyCode ^ Keys.PageUp) == 0)
    MessageBox.Show("PageUp"); //does not
...
}

Do I need to dive into WndProc to process this key? Thanks.


Edit: I've found out that this works, the problem was in enclosing TabControl handling these keys before ListControl got to them.

Upvotes: 2

Views: 2873

Answers (3)

Hans Passant
Hans Passant

Reputation: 942099

The e.KeyData argument includes the modifier keys. Make it look like this:

  if (e.KeyData == (Keys.Control | Keys.PageDown)) {
    // Do your stuff
    Console.WriteLine("Ctrl+PgDn");
  }

Upvotes: 4

Fredrik Mörk
Fredrik Mörk

Reputation: 158369

No need for WndProc:

if ((e.Modifiers & ModifierKeys) == Keys.Control && e.KeyCode == Keys.PageUp)
{
    // ctrl + page up was pressed
}

Upvotes: 4

thelost
thelost

Reputation: 6694

check for

Keys.Control | Keys.PageUp

Upvotes: 2

Related Questions