views:

322

answers:

3

Hello,

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.

+1  A: 

check for

Keys.Control | Keys.PageUp
thelost
This will not work since the `KeyDown` event contains information about what key that triggered the event, not what keys are currently pressed. So you will get two separate `KeyDown` events: one for the Control key and one for the PageUp key.
Fredrik Mörk
This will work.
Hans Passant
It depends when you run this code ;)
thelost
+4  A: 

No need for WndProc:

if ((e.Modifiers & ModifierKeys) == Keys.Control && e.KeyCode == Keys.PageUp)
{
    // ctrl + page up was pressed
}
Fredrik Mörk
+3  A: 

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");
  }
Hans Passant