Check if Keys is Letter/Digit/Special Symbol

2019-02-09 17:39发布

I override ProcessCmdKey and when I get Keys argument, I want to check if this Keys is Letter or Digit or Special Symbol.

I have this snippet

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

Everything works for letters and digits. but when I press F1-F12 it converts them to letters.

Maybe someone knows better way to solve this task?

标签: c# key
6条回答
▲ chillily
2楼-- · 2019-02-09 17:58

Try using keyData.KeyCode and maybe even testing within a range instead of using the Char.IsLetterOrDigit. e.g.

if (keyData.KeyCode >= Keys.D0 && keyData.KeyCode <= Keys.Z) {
  ...
}
查看更多
We Are One
3楼-- · 2019-02-09 17:59

Try

if( !(keyData >= Keys.F1 && keyData <= Keys.F12))
{
    char key = (char)keyData;
    if(char.IsLetterOrDigit(key))
    {
        Console.WriteLine(key);
        return false;
    }

}
return base.ProcessCmdKey(ref msg, keyData);
查看更多
\"骚年 ilove
4楼-- · 2019-02-09 18:05
if (keyData >= Keys.F1 && keyData <= Keys.F12)
{
     //one of the key between F1~F12 is pressed
}
查看更多
可以哭但决不认输i
5楼-- · 2019-02-09 18:15

I have tried the following code but for some reason char.IsLetter() method is recognising the following keys as Letters???

F1, F8, F9, F11, F12, RightShift, LeftShift, RightAlt, RightCtrl, LeftCtrl, LeftWin, RightWin, NumLock.

This method doesn't seem to be that full proof regarding what it thinks is a letter.

if(char.IsLetter((char)e.Key) || char.IsDigit((char)e.Key))
查看更多
神经病院院长
6楼-- · 2019-02-09 18:15

you need either a giant switch/case statement or check for ranges. You may find it easier to check for the keys you want to exclude, depending on which there is fewer of. Look at this for all the possible values. http://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx

if (keyData >= Keys.A && keyData <= Keys.Z)
   // do something

or

switch(keyData) {
case Keys.Add:
case Keys.Multiply:
// etc.
   // do something
   break;
}
查看更多
The star\"
7楼-- · 2019-02-09 18:17

Override the form's OnKeyPress method instead. The KeyPressEventArgs provides a KeyChar property which allows you to utilize the static methods on char.

As mentioned by Cody Gray in the comments, this method only fires on key strokes that have character information. Other key strokes such as F1-F12 should be processed in OnKeyDown or OnKeyUp, depending on your situation.

From MSDN:

Key events occur in the following order:

The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.

Example

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
  }
}
查看更多
登录 后发表回答