WPF密钥是数字或数字 [英] WPF Key is digit or number

查看:64
本文介绍了WPF密钥是数字或数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的窗口中有previewKeyDown方法,我想知道所按下的键只是A-Z字母或1-0数字(不带anyF1..12,enter,ctrl,alt等-仅字母或数字).

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

我已经尝试过Char.IsLetter,但是我需要提供字符,所以e.key.ToString()[0]不起作用,因为它几乎每次都是字母.

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.

推荐答案

类似的事情将会做到:

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

当然,您还必须检查是否没有根据您的要求按下诸如CTRL之类的修饰键.

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

这篇关于WPF密钥是数字或数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆