How do I know if the value in a KeyEventArgs is printable?

  • Thread starter Thread starter **Developer**
  • Start date Start date
D

**Developer**

How do I know if the value in a KeyEventArgs is printable?

Basicacally, in KeyUp I'd like to know if the character is printable, as
opposed to say, a backspace.

I suppose I could check KeyValue against some integers which I could get
from an ASCII table.

Isn't there a better way?



Thanks
 
This is just off the top of my head, so I don't know whether it would be
useful to you.

The KeyPress event fires on printable characters. You might record the key
press argument in the form's instance data, and inspect it in the KeyUp
event (clear it in the KeyUp event after you inspect it so that it won't
linger for the next keyboard input). If the KeyPress event stored nothing
before KeyUp occurred, you can assume that the key input you're handling was
not printable.

Tom Dacon
Dacon Software Consulting
 
Thanks, I'll try it now.


Tom Dacon said:
This is just off the top of my head, so I don't know whether it would be
useful to you.

The KeyPress event fires on printable characters. You might record the key
press argument in the form's instance data, and inspect it in the KeyUp
event (clear it in the KeyUp event after you inspect it so that it won't
linger for the next keyboard input). If the KeyPress event stored nothing
before KeyUp occurred, you can assume that the key input you're handling
was
not printable.

Tom Dacon
Dacon Software Consulting
 
**Developer** said:
How do I know if the value in a KeyEventArgs is printable?

Basicacally, in KeyUp I'd like to know if the character is printable, as
opposed to say, a backspace.

If you use the 'KeyPress' event instead of 'KeyUp', the character will be
passed as part of the 'e' parameter, which is of type 'KeyPressEventArgs':

\\\
If Char.IsWhiteSpace(e.KeyChar) OrElse Char.IsControl(e.KeyChar) Then
...
Else
...
End If
///
 
Back
Top