WPF Key is digit or number

前端 未结 7 686
感情败类
感情败类 2021-01-17 09:36

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,

7条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-17 10:19

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

提交回复
热议问题