Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert e.KeyChar to an actual character value when handling keyboard events?

All I'm wanting to do is get the actual character that is pressed whenever they key is entered for the event.

The only option I'm seeing is e.KeyCharwhich will give me the position rather than the character itself.

What I'm wanting to do is limit the values of the key press to digits between 0-9. The way I'm doing it right now is just if((e.KeyChar < 48 || (e.KeyChar > 57)))

This seems a bit cheap to just put in the values for 0 and 9, and I'd love to know how to get the character value itself rather than its key.

EDIT: Thanks for the input; so in general is there no way to go from having the e.KeyCharvalue to the value of the input itself.

I'd really like to take my event, e, and access the character pressed directly as opposed to using e.KeyChar to get a numeric representation of that value.

like image 863
user1993843 Avatar asked Apr 04 '13 02:04

user1993843


1 Answers

Here is a simpler way of detecting if the key pressed is a digit or not:

if (char.IsDigit(e.KeyChar)) { // doSomething(); }
like image 117
e.beyer Avatar answered Sep 29 '22 11:09

e.beyer