Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从System.Windows.Input.KeyEventArgs获取按下的字符?_C#_Wpf_Keyeventargs - Fatal编程技术网

C# 如何从System.Windows.Input.KeyEventArgs获取按下的字符?

C# 如何从System.Windows.Input.KeyEventArgs获取按下的字符?,c#,wpf,keyeventargs,C#,Wpf,Keyeventargs,我有System.Windows.Input.KeyEventArgse变量。我想得到真正的char。例如,我按下键盘上的}按钮。通常它会返回类似于oem..的字符串,但我想得到}char。怎么办 [编辑]我在文本框中使用它。您需要在按键事件中进行处理,而不是在KeyDown或KeyUp中。KeyEventArgs表示实际按下的键,而不是对应的字符。某些键没有关联的事件字符。但是,KeyPressEventArgs将有一个关联的字符,因为没有为无字符键(ctrl、up等)触发KeyPress事件

我有
System.Windows.Input.KeyEventArgs
e变量。我想得到真正的char。例如,我按下键盘上的
}
按钮。通常它会返回类似于
oem..
的字符串,但我想得到
}
char。怎么办


[编辑]我在文本框中使用它。

您需要在按键事件中进行处理,而不是在KeyDown或KeyUp中。KeyEventArgs表示实际按下的键,而不是对应的字符。某些键没有关联的事件字符。但是,KeyPressEventArgs将有一个关联的字符,因为没有为无字符键(ctrl、up等)触发KeyPress事件。

您有e.Key属性可用于此。这是一个例子

编辑:
我不知道你需要真正的字符。你可以看一看,他们说在WPF中,你可以使用一些Win32 API,而在Silverlight中,这似乎相当困难。此外,您还可以查看KeyInterop.VirtualKeyFromKey—您可以将WPF密钥枚举转换为WinForms密钥枚举,这会为您提供更多信息。我还没有尝试过任何解决方案,因此我不知道其中是否有任何一种会起作用。
还有最后一件事。为什么需要KeyDown/Up事件中的字符?您确定不能改用TextChanged事件吗?如果可以的话,获得准确的字符会容易得多。

请参阅本文

它有一个名为
GetCharFromKey(Key-Key)
的实用函数,该函数从键盘事件args的键中获取特定于语言环境的字符

非常有用

char c = (char)e.KeyValue;
这将以大写字母或数字形式返回按下的键
这不适用于特殊字符

有时您只需要用大锤解决问题

char KeyToChar(Key key) {

    if (Keyboard.IsKeyDown(Key.LeftAlt)  ||
        Keyboard.IsKeyDown(Key.RightAlt) ||
        Keyboard.IsKeyDown(Key.LeftCtrl) ||
        Keyboard.IsKeyDown(Key.RightAlt))
    {
        return '\x00';
    }

    bool caplock  = Console.CapsLock;
    bool shift    = Keyboard.IsKeyDown(Key.LeftShift) || 
                            Keyboard.IsKeyDown(Key.RightShift);
    bool iscap    = (caplock && !shift) || (!caplock && shift);

        switch(key) {
            case Key.Enter:           return '\n';
            case Key.A:               return (iscap ? 'A' : 'a');
            case Key.B:               return (iscap ? 'B' : 'b');
            case Key.C:               return (iscap ? 'C' : 'c');
            case Key.D:               return (iscap ? 'D' : 'd');
            case Key.E:               return (iscap ? 'E' : 'e');
            case Key.F:               return (iscap ? 'F' : 'f');
            case Key.G:               return (iscap ? 'G' : 'g');
            case Key.H:               return (iscap ? 'H' : 'h');
            case Key.I:               return (iscap ? 'I' : 'i');
            case Key.J:               return (iscap ? 'J' : 'j');
            case Key.K:               return (iscap ? 'K' : 'k');
            case Key.L:               return (iscap ? 'L' : 'l');
            case Key.M:               return (iscap ? 'M' : 'm');
            case Key.N:               return (iscap ? 'N' : 'n');
            case Key.O:               return (iscap ? 'O' : 'o');
            case Key.P:               return (iscap ? 'P' : 'p');
            case Key.Q:               return (iscap ? 'Q' : 'q');
            case Key.R:               return (iscap ? 'R' : 'r');
            case Key.S:               return (iscap ? 'S' : 's');
            case Key.T:               return (iscap ? 'T' : 't');
            case Key.U:               return (iscap ? 'U' : 'u');
            case Key.V:               return (iscap ? 'V' : 'v');
            case Key.W:               return (iscap ? 'W' : 'w');
            case Key.X:               return (iscap ? 'X' : 'x');
            case Key.Y:               return (iscap ? 'Y' : 'y');
            case Key.Z:               return (iscap ? 'Z' : 'z');
            case Key.D0:              return (shift ? ')' : '0');
            case Key.D1:              return (shift ? '!' : '1');
            case Key.D2:              return (shift ? '@' : '2');
            case Key.D3:              return (shift ? '#' : '3');
            case Key.D4:              return (shift ? '$' : '4');
            case Key.D5:              return (shift ? '%' : '5');
            case Key.D6:              return (shift ? '^' : '6');
            case Key.D7:              return (shift ? '&' : '7');
            case Key.D8:              return (shift ? '*' : '8');
            case Key.D9:              return (shift ? '(' : '9');
            case Key.OemPlus:         return (shift ? '+' : '=');
            case Key.OemMinus:        return (shift ? '_' : '-');
            case Key.OemQuestion:     return (shift ? '?' : '/');
            case Key.OemComma:        return (shift ? '<' : ',');
            case Key.OemPeriod:       return (shift ? '>' : '.');
            case Key.OemOpenBrackets: return (shift ? '{' : '[');
            case Key.OemQuotes:       return (shift ? '"' : '\'');
            case Key.Oem1:            return (shift ? ':' : ';');
            case Key.Oem3:            return (shift ? '~' : '`');                   
            case Key.Oem5:            return (shift ? '|' : '\\');
            case Key.Oem6:            return (shift ? '}' : ']');
            case Key.Tab:             return '\t';
            case Key.Space:           return ' ';

            // Number Pad
            case Key.NumPad0:         return '0';
            case Key.NumPad1:         return '1';
            case Key.NumPad2:         return '2';
            case Key.NumPad3:         return '3';
            case Key.NumPad4:         return '4';
            case Key.NumPad5:         return '5';
            case Key.NumPad6:         return '6';
            case Key.NumPad7:         return '7';
            case Key.NumPad8:         return '8';
            case Key.NumPad9:         return '9';
            case Key.Subtract:        return '-';
            case Key.Add:             return '+';
            case Key.Decimal:         return '.';
            case Key.Divide:          return '/';
            case Key.Multiply:        return '*';

            default:                  return '\x00';
    }
}
char-KeyToChar(键){
if(Keyboard.IsKeyDown(Key.LeftAlt)||
键盘.IsKeyDown(Key.righalt)||
键盘.IsKeyDown(Key.LeftCtrl)||
键盘.IsKeyDown(Key.RightAlt))
{
返回“\x00”;
}
bool caplock=Console.CapsLock;
bool shift=键盘.IsKeyDown(Key.LeftShift)|
键盘.IsKeyDown(Key.RightShift);
bool iscap=(caplock&&shift)| |(!caplock&&shift);
开关(钥匙){
大小写键。输入:返回“\n”;
case Key.A:return(iscap?'A':'A');
case Key.B:return(iscap?'B':'B');
case Key.C:return(iscap?'C':'C');
case Key.D:return(iscap'D':'D');
case Key.E:return(iscap?'E':'E');
case Key.F:return(iscap?'F':'F');
case Key.G:return(iscap'G':'G');
case Key.H:return(iscap?'H':'H');
case Key.I:return(iscap?'I':'I');
case Key.J:return(iscap?'J':'J');
case Key.K:return(iscap?'K':'K');
case Key.L:return(iscap?'L':'L');
case Key.M:return(iscap?'M':'M');
case Key.N:return(iscap?'N':'N');
case Key.O:return(iscap'O':'O');
case Key.P:return(iscap?'P':'P');
case Key.Q:return(iscap?'Q':'Q');
case Key.R:return(iscap?'R':'R');
case Key.S:return(iscap'S':'S');
case Key.T:return(iscap'T':'T');
case Key.U:return(iscap?'U':'U');
case Key.V:return(iscap?'V':'V');
case Key.W:return(iscap?'W':'W');
case Key.X:return(iscap?'X':'X');
case Key.Y:return(iscap?'Y':'Y');
case Key.Z:return(iscap?'Z':'Z');
case Key.D0:返回(shift?):“0”);
案例键D1:返回(shift?!:“1”);
case Key.D2:返回(shift?@:“2”);
case Key.D3:return(shift?#?:“3”);
case Key.D4:返回(shift?“$”:“4”);
case Key.D5:返回(移位?'%:'5');
case Key.D6:返回(shift?“^”:“6”);
case Key.D7:返回(shift?’&’:“7”);
case Key.D8:返回(shift?'*':'8');
case Key.D9:返回(shift?'(':'9');
case Key.OemPlus:返回(shift?'+':'=');
case Key.OemMinus:return(shift?'.':'-');
case Key.OemQuestion:return(shift?'?':'/);
case Key.OemComma:返回(移位?“”:);
case Key.oemopenbrakets:return(shift?'{':'[');
case Key.OemQuotes:返回(shift?“:”\”;
case Key.Oem1:返回(shift?':':';);
case Key.Oem3:返回(shift?“~”:“`”);
case Key.Oem5:return(shift?“|”):“\\”;
case Key.Oem6:返回(shift?'}':']');
case Key.Tab:返回'\t';
大小写键。空格:返回“”;
//数字键盘
case Key.NumPad0:返回“0”;
case Key.NumPad1:返回“1”;
case Key.NumPad2:返回“2”;
case Key.NumPad3:返回'3';
case Key.numpa4:返回'4';
case Key.NumPad5:返回'5';
case Key.NumPad6:返回“6”;
case Key.NumPad7:返回'7';
case Key.NumPad8:返回'8';
案例
public struct IoCmd_t {
    public Key    key;
    public bool   printable;
    public char   character;
    public bool   shift;
    public bool   ctrl;
    public bool   alt;
    public int    type; //sideband
    public string s;    //sideband
};

public void KeyToChar(Key key, ref IoCmd_t KeyDecode) {
    bool iscap;
    bool caplock;
    bool shift;

    KeyDecode.key   = key;

    KeyDecode.alt   = Keyboard.IsKeyDown(Key.LeftAlt) || 
                      Keyboard.IsKeyDown(Key.RightAlt);

    KeyDecode.ctrl  = Keyboard.IsKeyDown(Key.LeftCtrl) ||
                      Keyboard.IsKeyDown(Key.RightCtrl);

    KeyDecode.shift = Keyboard.IsKeyDown(Key.LeftShift) ||
                      Keyboard.IsKeyDown(Key.RightShift);

    if (KeyDecode.alt || KeyDecode.ctrl) {
        KeyDecode.printable = false;                
        KeyDecode.type      = 1;                
    }
    else {
        KeyDecode.printable = true;
        KeyDecode.type      = 0;
    }

    shift    = KeyDecode.shift;
    caplock  = Console.CapsLock; //Keyboard.IsKeyToggled(Key.CapsLock);
    iscap    = (caplock && !shift) || (!caplock && shift);

    switch(key) {
        case Key.Enter:           KeyDecode.character = '\n'; return;
        case Key.A:               KeyDecode.character = (iscap ? 'A' : 'a');  return;
        case Key.B:               KeyDecode.character = (iscap ? 'B' : 'b');  return;
        case Key.C:               KeyDecode.character = (iscap ? 'C' : 'c');  return;
        case Key.D:               KeyDecode.character = (iscap ? 'D' : 'd');  return;
        case Key.E:               KeyDecode.character = (iscap ? 'E' : 'e');  return;
        case Key.F:               KeyDecode.character = (iscap ? 'F' : 'f');  return;
        case Key.G:               KeyDecode.character = (iscap ? 'G' : 'g');  return;
        case Key.H:               KeyDecode.character = (iscap ? 'H' : 'h');  return;
        case Key.I:               KeyDecode.character = (iscap ? 'I' : 'i');  return;
        case Key.J:               KeyDecode.character = (iscap ? 'J' : 'j');  return;
        case Key.K:               KeyDecode.character = (iscap ? 'K' : 'k');  return;
        case Key.L:               KeyDecode.character = (iscap ? 'L' : 'l');  return;
        case Key.M:               KeyDecode.character = (iscap ? 'M' : 'm');  return;
        case Key.N:               KeyDecode.character = (iscap ? 'N' : 'n');  return;
        case Key.O:               KeyDecode.character = (iscap ? 'O' : 'o');  return;
        case Key.P:               KeyDecode.character = (iscap ? 'P' : 'p');  return;
        case Key.Q:               KeyDecode.character = (iscap ? 'Q' : 'q');  return;
        case Key.R:               KeyDecode.character = (iscap ? 'R' : 'r');  return;
        case Key.S:               KeyDecode.character = (iscap ? 'S' : 's');  return;
        case Key.T:               KeyDecode.character = (iscap ? 'T' : 't');  return;
        case Key.U:               KeyDecode.character = (iscap ? 'U' : 'u');  return;
        case Key.V:               KeyDecode.character = (iscap ? 'V' : 'v');  return;
        case Key.W:               KeyDecode.character = (iscap ? 'W' : 'w');  return;
        case Key.X:               KeyDecode.character = (iscap ? 'X' : 'x');  return;
        case Key.Y:               KeyDecode.character = (iscap ? 'Y' : 'y');  return;
        case Key.Z:               KeyDecode.character = (iscap ? 'Z' : 'z');  return;
        case Key.D0:              KeyDecode.character = (shift ? ')' : '0');  return;
        case Key.D1:              KeyDecode.character = (shift ? '!' : '1');  return;
        case Key.D2:              KeyDecode.character = (shift ? '@' : '2');  return;
        case Key.D3:              KeyDecode.character = (shift ? '#' : '3');  return;
        case Key.D4:              KeyDecode.character = (shift ? '$' : '4');  return;
        case Key.D5:              KeyDecode.character = (shift ? '%' : '5');  return;
        case Key.D6:              KeyDecode.character = (shift ? '^' : '6');  return;
        case Key.D7:              KeyDecode.character = (shift ? '&' : '7');  return;
        case Key.D8:              KeyDecode.character = (shift ? '*' : '8');  return;
        case Key.D9:              KeyDecode.character = (shift ? '(' : '9');  return;
        case Key.OemPlus:         KeyDecode.character = (shift ? '+' : '=');  return;
        case Key.OemMinus:        KeyDecode.character = (shift ? '_' : '-');  return;
        case Key.OemQuestion:     KeyDecode.character = (shift ? '?' : '/');  return;
        case Key.OemComma:        KeyDecode.character = (shift ? '<' : ',');  return;
        case Key.OemPeriod:       KeyDecode.character = (shift ? '>' : '.');  return;
        case Key.OemOpenBrackets: KeyDecode.character = (shift ? '{' : '[');  return;
        case Key.OemQuotes:       KeyDecode.character = (shift ? '"' : '\''); return;
        case Key.Oem1:            KeyDecode.character = (shift ? ':' : ';');  return;
        case Key.Oem3:            KeyDecode.character = (shift ? '~' : '`');  return;
        case Key.Oem5:            KeyDecode.character = (shift ? '|' : '\\'); return;
        case Key.Oem6:            KeyDecode.character = (shift ? '}' : ']');  return;
        case Key.Tab:             KeyDecode.character = '\t'; return;
        case Key.Space:           KeyDecode.character = ' '; return;

        // Number Pad
        case Key.NumPad0:         KeyDecode.character = '0'; return;
        case Key.NumPad1:         KeyDecode.character = '1'; return;
        case Key.NumPad2:         KeyDecode.character = '2'; return;
        case Key.NumPad3:         KeyDecode.character = '3'; return;
        case Key.NumPad4:         KeyDecode.character = '4'; return;
        case Key.NumPad5:         KeyDecode.character = '5'; return;
        case Key.NumPad6:         KeyDecode.character = '6'; return;
        case Key.NumPad7:         KeyDecode.character = '7'; return;
        case Key.NumPad8:         KeyDecode.character = '8'; return;
        case Key.NumPad9:         KeyDecode.character = '9'; return;
        case Key.Subtract:        KeyDecode.character = '-'; return;
        case Key.Add:             KeyDecode.character = '+'; return;
        case Key.Decimal:         KeyDecode.character = '.'; return;
        case Key.Divide:          KeyDecode.character = '/'; return;
        case Key.Multiply:        KeyDecode.character = '*'; return;

        default:
            KeyDecode.type      = 1;
            KeyDecode.printable = false;
            KeyDecode.character = '\x00'; 
            return;
    } //switch          
} // function
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    var theKeyAsAString = e.KeyCode.ToString();
    var theKeyAsAChar = Convert.ToChar(theKeyAsAString);
}
textbox_previewTextInput(object sender, TextCompositionEventArgs e){
  var theChar = Convert.ToChar(e.Text);
  if (theChar.Equals('{'))
  {
    // Do something with the char
    e.Handled = true;
  }
  else
  {
     e.Handled = false;
  }
}