I have previewKeyDown
method in my window, and I'd like to know that pressed key is only A-Z
letter or 1-0
number (without anyF1..12, enter, ctrl, alt etc - just letter or number).
I've tried Char.IsLetter
, but i need to give the char, so e.key.ToString()[0]
doesn't work, because it is almost everytime a letter.
Add a reference to Microsoft.VisualBasic and use the VB IsNumeric function, combined with char.IsLetter().
Can you put some code to show what you intend? Shouldn't this work for you
e.Key
is giving you a member of theenum
System.Windows.Input.Key
You should be able to do the following to determine whether it is a letter or a number:
In your specific case the answer provided by Jon and Jeffery is probably best, however if you need to test your string for some other letter/number logic then you can use the KeyConverter class to convert a
System.Windows.Input.Key
to a stringYou'll still need to check to see if any modifier keys are being held down (Shift, Ctrl, and Alt), and it should also be noted that this only works for Letters and Numbers. Special characters (such as commas, quotes, etc) will get displayed the same as
e.Key.ToString()
try this, it works.
Something like this will do:
Of course you will also have to check that no modifier keys like CTRL are pressed according to your requirements.