Trouble With KeyDown Event in Form (Noob)

L

Luc The Perverse

Hey - I am making my first C# form application! (More a teach myself app
than any pratical application)

I have a listbox that always has the control so I made an event handler for
KeyDown.

I seem to be losing information when Casting to char, since I never have any
problem using special keys if I use something like e.KeyCode == Keys.Delete

But . . there is no Keys.Astericks (or Star or similar)

private void PlayList_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e){
switch((char)e.KeyValue){
case 'x':case 'X':
Close();
break;
case '*':
System.Windows.Forms.MessageBox.Show("I never get here");
break;
default:
//do nothing
break;
}
}


Two things. How am I supposed to test for normal codes like this?

Is there a way to capture things for the entire form without making a
KeyDown handler for every item in the form?
 
P

Peter Duniho

[...]
Two things. How am I supposed to test for normal codes like this?

It depends on what you want. The KeyDown event is particular to specific
physical keys on the keyboard. There's no "*" key per se...that's the 8
key with the SHIFT key held down (on a standard keyboard). So to detect
that you'd look for the "8" key, also checking the state of the SHIFT key.

Additionally, you shouldn't be casting the KeyValue property to a char,
because that's not what it is. Simply use the values from the enumeration
in your switch statement. In other words, in your example you'd use
Keys.X and Keys.D8 for the case statements, and in the Keys.D8 statement
you'd only execute the code if the KeyEventArgs.Shift property was true.

Alternatively, if all you really care about is truly the ASCII code
generated by the user, rather than the physical key being pressed, handle
the KeyPress event instead. That does give you the final ASCII code that
results from the keyboard input and you don't have to mess with the
parsing the key+modifier combinations yourself.
Is there a way to capture things for the entire form without making a
KeyDown handler for every item in the form?

As the documentation points out:

To handle keyboard events only at the form level and not
enable other controls to receive keyboard events, set
the KeyPressEventArgs.Handled property in your form's
KeyPress event-handling method to true

In other words, handle the event in the form and mark it as handled so
that it doesn't propogate down to the child controls.

The same thing applies for the KeyDown and KeyUp events, except those of
course use the KeyEventArgs class instead of KeyPressEventArgs class.

Pete
 
L

Luc The Perverse

Peter Duniho said:
[...]
Two things. How am I supposed to test for normal codes like this?

It depends on what you want. The KeyDown event is particular to specific
physical keys on the keyboard. There's no "*" key per se...that's the 8
key with the SHIFT key held down (on a standard keyboard). So to detect
that you'd look for the "8" key, also checking the state of the SHIFT key.

Additionally, you shouldn't be casting the KeyValue property to a char,
because that's not what it is. Simply use the values from the enumeration
in your switch statement. In other words, in your example you'd use
Keys.X and Keys.D8 for the case statements, and in the Keys.D8 statement
you'd only execute the code if the KeyEventArgs.Shift property was true.

Alternatively, if all you really care about is truly the ASCII code
generated by the user, rather than the physical key being pressed, handle
the KeyPress event instead. That does give you the final ASCII code that
results from the keyboard input and you don't have to mess with the
parsing the key+modifier combinations yourself.
Is there a way to capture things for the entire form without making a
KeyDown handler for every item in the form?

As the documentation points out:

To handle keyboard events only at the form level and not
enable other controls to receive keyboard events, set
the KeyPressEventArgs.Handled property in your form's
KeyPress event-handling method to true

In other words, handle the event in the form and mark it as handled so
that it doesn't propogate down to the child controls.

The same thing applies for the KeyDown and KeyUp events, except those of
course use the KeyEventArgs class instead of KeyPressEventArgs class.

Pete

private void list_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e){
if(e.KeyChar=='*')
System.Windows.Forms.MessageBox.Show("Hooray!");
}

Um . . . Hooray!

Thanks dude :)
 
P

Peter Duniho

private void list_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e){
if(e.KeyChar=='*')
System.Windows.Forms.MessageBox.Show("Hooray!");
}

Um . . . Hooray!

Thanks dude :)

You're welcome...thanks for letting us know it helped. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top