Re: Trouble With KeyDown Event in Form (Noob)

Tech-Archive recommends: Speed Up your PC by fixing your registry



"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:op.tqky7bei8jd0ej@xxxxxxxxxxxxxxxxxxxxxxx
On Tue, 10 Apr 2007 11:05:16 -0700, Luc The Perverse
<sll_noSpamlicious_z_XXX_m@xxxxxxxxxx> wrote:

[...]
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 :)

--
LTP

:)


.



Relevant Pages

  • Re: MDI form Keyboard Events
    ... Click) but not keyboard events ... g. KeyDown or KeyPress). ... > Normal or Child forms have these events - why not MDI forms? ...
    (microsoft.public.vb.general.discussion)
  • Re: Trap keystrokes/keyboard events in controls
    ... In KeyPress if no modifier found in keydown then just print the ... As Mike says, you can preview key input to deal with situations where some control is consuming the input, but you still want to see it. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Trap keystrokes/keyboard events in controls
    ... Modifier are flagged in the KeyDown event ... In KeyPress if no modifier found in keydown then just print the ... instead of from each control. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: how to limit textbox to text only
    ... You should use the KeyPress or KeyDown ... The following code has been taken from the from MSDN on KeyPress event: ... // Boolean flag used to determine when a character other than a number ... // Determine whether the keystroke is a number from the keypad. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: event handler in doesnt work
    ... keydown doesn't enter into this. ... keypress is where you would expect to deal with ... deal with it in keyup). ...
    (comp.lang.javascript)