18

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.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
user13657
  • 745
  • 3
  • 17
  • 36

7 Answers7

33

Something like this will do:

if ((e.Key >= Key.A && e.Key <= Key.Z) || (e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))

Of course you will also have to check that no modifier keys like CTRL are pressed according to your requirements.

Pakman
  • 2,170
  • 3
  • 23
  • 41
Jon
  • 428,835
  • 81
  • 738
  • 806
  • That works fine, thank You. Now another problem - focus on datagrid - in code i used something like: if (e.key == Key.Down || Key.Up) dataGrid.selectedIndex = 0; DataGrid.Focus(), but now pushing up/down key didnt change index in datagrid.. – user13657 Feb 12 '13 at 14:08
  • 1
    @AdamTruszkowski: Perhaps that's meant to be `e.Key == Key.Down || e.Key == Key.Up`? In any case, please ask separate questions instead of putting them in comments. – Jon Feb 12 '13 at 14:10
  • Yes, it is e.Key == Key.Down || e.Key == Key.Down, but now i want to move up/down in datagrid when up/down is pressed – user13657 Feb 12 '13 at 14:11
  • Ok, i got it - just changing index in datagrid +1/-1 after keydown/up pressed && when selecteditems.count = 1 – user13657 Feb 12 '13 at 14:16
  • KeyDown event does not capture arrow keys, try PreviewKeyDown instead. – Felype Dec 05 '16 at 17:13
11

e.Key is giving you a member of the enum System.Windows.Input.Key

You should be able to do the following to determine whether it is a letter or a number:

var isNumber = e.Key >= Key.D0 && e.Key <= Key.D9;
var isLetter = e.Key >= Key.A && e.Key <= Key.Z;
Khan
  • 17,904
  • 5
  • 47
  • 59
  • 1
    This doesn't identify numbers coming from the numpad. isNumber should be (e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) as used in Jon's answer. – Josh Oct 25 '20 at 07:50
6

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 string

var strKey = new KeyConverter().ConvertToString(e.Key);

You'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()

Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490
5

try this, it works.

    private void txbNumber_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key >= Key.D0 && e.Key <= Key.D9) ; // it`s number
        else if (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) ; // it`s number
        else if (e.Key == Key.Escape || e.Key == Key.Tab || e.Key == Key.CapsLock || e.Key == Key.LeftShift || e.Key == Key.LeftCtrl ||
            e.Key == Key.LWin || e.Key == Key.LeftAlt || e.Key == Key.RightAlt || e.Key == Key.RightCtrl || e.Key == Key.RightShift ||
            e.Key == Key.Left || e.Key == Key.Up || e.Key == Key.Down || e.Key == Key.Right || e.Key == Key.Return || e.Key == Key.Delete ||
            e.Key == Key.System) ; // it`s a system key (add other key here if you want to allow)
        else
            e.Handled = true; // the key will sappressed
    }
Matrix
  • 125
  • 2
  • 6
0

Add a reference to Microsoft.VisualBasic and use the VB IsNumeric function, combined with char.IsLetter().

Barracoder
  • 3,696
  • 2
  • 28
  • 31
0

bit of a cludge but it works :)

 private void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
        Regex R = new Regex("^([A-Z]|[0-9]){1}$");

        var strKey = new KeyConverter().ConvertToString(e.Key);
        if(strKey.Length > 1 )
        {
            strKey = strKey.Replace("NumPad", "").Replace("D", "");
        }
        if (strKey.Length == 1)
        {
            if (!R.IsMatch(strKey))
            {
                e.Handled = true;
            }
        }
        else
        {
            e.Handled = true;
        }
    }
  • This doesn't cover the case of letters, and it doesn't cover non-numpad numbers (D0-D9). – Josh Oct 25 '20 at 07:44
  • 1
    Serves me right for skim reading :-) Updated to a slightly better cludge although other answers are probably more appropriate. – Jon Bridger Oct 26 '20 at 10:04
-1

Can you put some code to show what you intend? Shouldn't this work for you

      if(e.key.ToString().Length==1)

    `Char.IsLetter(e.key.ToString()[0])`
    else

//
Igoy
  • 2,942
  • 22
  • 23
  • e.key.tostring()[0] - so when i push LeftCtrl - char is L which is letter, that's why it doesnt work :) – user13657 Feb 12 '13 at 14:09