我有这个片段
protected overrIDe bool ProcessCmdKey(ref Message msg,Keys keyData) { char key = (char)keyData; if(char.IsLetterOrDigit(key) { Console.Writeline(key); } return base.ProcessCmdKey(ref msg,keyData); }
一切都适用于字母和数字.但是当我按下F1-F12时,它会将它们转换成字母.
也许有人知道更好的方法来解决这个任务?
解决方法 改为覆盖表单的OnKeyPress方法. KeyPressEventArgs提供了一个KeyChar
属性,允许您在char上使用静态方法. 正如Cody Gray在评论中所提到的,这种方法只会触发具有角色信息的击键.其他击键如F1-F12应在OnKeyDown或OnKeyUp中处理,具体取决于您的情况.
从MSDN开始:
Key events occur in the following
07002 07003 07004
order:The KeyPress event is not raised by
noncharacter keys; however,the
noncharacter keys do raise the KeyDown
and KeyUp events.
例
protected overrIDe voID OnKeyPress(KeyPressEventArgs e){ base.OnKeyPress(e); if (char.IsLetter(e.KeyChar)) { // char is letter } else if (char.IsDigit(e.KeyChar)) { // char is digit } else { // char is neither letter or digit. // there are more methods you can use to determine the // type of char,e.g. char.IsSymbol }}总结
以上是内存溢出为你收集整理的c# – 检查键是否是字母/数字/特殊符号全部内容,希望文章能够帮你解决c# – 检查键是否是字母/数字/特殊符号所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)