I need to get the event args as a char
, but when I try casting the Key enum I get completely different letters and symbols than what was passed in.
我需要将事件args作为一个char,但是当我尝试释放键enum时,我得到的字母和符号与传入的完全不同。
How do you properly convert the Key to a char?
如何正确地将密钥转换为字符?
This is what I've tried
这是我试过的
ObserveKeyStroke(this, new ObervableKeyStrokeEvent((char)((KeyEventArgs)e.StagingItem.Input).Key));
Edit: I also don't have the KeyCode property on the args. I'm getting them from the InputManager.Current.PreNotifyInput event.
编辑:我在args上也没有KeyCode属性。我从inputmanager。current。PreNotifyInput事件。
5 个解决方案
#1
11
See How to convert a character in to equivalent System.Windows.Input.Key Enum value? Use KeyInterop.VirtualKeyFromKey
instead.
查看如何将字符转换为等效的System.Windows.Input。键枚举值吗?使用KeyInterop。VirtualKeyFromKey代替。
#2
2
It takes a little getting used to, but you can just use the key values themselves. If you're trying to limit input to alphanumerics and maybe a little extra, the code below may help.
这需要一点时间去适应,但是你可以只使用键值本身。如果您试图将输入限制为字母数字,或者稍微多一点,下面的代码可能会有所帮助。
private bool bLeftShiftKey = false;
private bool bRightShiftKey = false;
private bool IsValidDescriptionKey(Key key)
{
//KEYS ALLOWED REGARDLESS OF SHIFT KEY
//various editing keys
if (
key == Key.Back ||
key == Key.Tab ||
key == Key.Up ||
key == Key.Down ||
key == Key.Left ||
key == Key.Right ||
key == Key.Delete ||
key == Key.Space ||
key == Key.Home ||
key == Key.End
) {
return true;
}
//letters
if (key >= Key.A && key <= Key.Z)
{
return true;
}
//numbers from keypad
if (key >= Key.NumPad0 && key <= Key.NumPad9)
{
return true;
}
//hyphen
if (key == Key.OemMinus)
{
return true;
}
//KEYS ALLOWED CONDITITIONALLY DEPENDING ON SHIFT KEY
if (!bLeftShiftKey && !bRightShiftKey)
{
//numbers from keyboard
if (key >= Key.D0 && key <= Key.D9)
{
return true;
}
}
return false;
}
private void cboDescription_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftShift)
{
bLeftShiftKey = true;
}
if (e.Key == Key.RightShift)
{
bRightShiftKey = true;
}
if (!IsValidDescriptionKey(e.Key))
{
e.Handled = true;
}
}
private void cboDescription_PreviewKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftShift)
{
bLeftShiftKey = false;
}
if (e.Key == Key.RightShift)
{
bRightShiftKey = false;
}
}
#3
2
That work for me:
这工作对我来说:
Based on the last entry i found that in WPF there is no such event PreNotifyInput
, but i found and equivalent PreviewTextInput
根据上一个条目,我发现在WPF中没有这样的事件PreNotifyInput,但是我找到了和等效的PreviewTextInput
First I try with a RegExp
, but I cant make it work, then I use a simple indexOf
.
首先我尝试使用RegExp,但是我不能使它正常工作,然后我使用一个简单的indexOf。
private bool ValidChar(string _char)
{
string Lista = @" ! "" # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ";
return Lista.IndexOf(_char.ToUpper()) != -1;
//System.Text.RegularExpressions.Regex RegVal = new System.Text.RegularExpressions.Regex(@"(?<LETRAS>[A-Z]+)+(?<NUMERO>[0-9]+)+(?<CAR>[!|""|#|$|%|&|'|(|)|*|+|,|\-|.|/|:|;|<|=|>|?|@]+)+");
//return RegVal.IsMatch(_char);
}
private void textBoxDescripcion_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!ValidChar(e.Text))
e.Handled = true;
}
#4
1
I know this is old, but none of the answers seem to actually answer the question. The reason a different char is coming back is because when you just try to cast it to a char
you are casting the enum value to a 'char'. However:
我知道这是一个古老的问题,但似乎没有一个答案能真正回答这个问题。另一个char返回的原因是,当您尝试将它转换为char时,您将enum值转换为“char”。然而:
var keyPressed = e.key.ToString();
Works great. Returns the key pressed as a string. Then you check the length. If it's == 1 then it's a char, number or symbol. If it's greater than 1 it's a special key.
伟大的工作。返回按为字符串的键。然后检查长度。如果它是= 1,那么它就是一个字符、数字或符号。如果大于1,它就是一个特殊的键。
If you just want the char you can then do keyPressed[0];
如果你只是想要字符,你可以做按键[0];
This is how I do it.
我就是这么做的。
private void scrollViewer_KeyDown(object sender, KeyEventArgs e)
{
if (!e.IsRepeat)
{
var keyPressed = e.Key.ToString();
if(keyPressed.Length == 1)
CharKeyPressed(keyPressed[0]);
else if(keyPressed.Length > 1)
HandleSpecialKey(keyPressed)
}
}
#5
0
Inside your PreNotifyInput handler, try something like this:
在您的PreNotifyInput处理程序中,尝试如下方法:
if (e.StagingItem.Input is System.Windows.Input.TextCompositionEventArgs)
{
if (!String.IsNullOrEmpty((e.StagingItem.Input as System.Windows.Input.TextCompositionEventArgs).Text))
{
Char c = (e.StagingItem.Input as System.Windows.Input.TextCompositionEventArgs).Text[0];
}
}
It raises multiple times for the different routed events, so you may want to filter for a particular one.
它会为不同的路由事件引发多次事件,因此您可能希望对特定的事件进行筛选。
#1
11
See How to convert a character in to equivalent System.Windows.Input.Key Enum value? Use KeyInterop.VirtualKeyFromKey
instead.
查看如何将字符转换为等效的System.Windows.Input。键枚举值吗?使用KeyInterop。VirtualKeyFromKey代替。
#2
2
It takes a little getting used to, but you can just use the key values themselves. If you're trying to limit input to alphanumerics and maybe a little extra, the code below may help.
这需要一点时间去适应,但是你可以只使用键值本身。如果您试图将输入限制为字母数字,或者稍微多一点,下面的代码可能会有所帮助。
private bool bLeftShiftKey = false;
private bool bRightShiftKey = false;
private bool IsValidDescriptionKey(Key key)
{
//KEYS ALLOWED REGARDLESS OF SHIFT KEY
//various editing keys
if (
key == Key.Back ||
key == Key.Tab ||
key == Key.Up ||
key == Key.Down ||
key == Key.Left ||
key == Key.Right ||
key == Key.Delete ||
key == Key.Space ||
key == Key.Home ||
key == Key.End
) {
return true;
}
//letters
if (key >= Key.A && key <= Key.Z)
{
return true;
}
//numbers from keypad
if (key >= Key.NumPad0 && key <= Key.NumPad9)
{
return true;
}
//hyphen
if (key == Key.OemMinus)
{
return true;
}
//KEYS ALLOWED CONDITITIONALLY DEPENDING ON SHIFT KEY
if (!bLeftShiftKey && !bRightShiftKey)
{
//numbers from keyboard
if (key >= Key.D0 && key <= Key.D9)
{
return true;
}
}
return false;
}
private void cboDescription_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftShift)
{
bLeftShiftKey = true;
}
if (e.Key == Key.RightShift)
{
bRightShiftKey = true;
}
if (!IsValidDescriptionKey(e.Key))
{
e.Handled = true;
}
}
private void cboDescription_PreviewKeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.LeftShift)
{
bLeftShiftKey = false;
}
if (e.Key == Key.RightShift)
{
bRightShiftKey = false;
}
}
#3
2
That work for me:
这工作对我来说:
Based on the last entry i found that in WPF there is no such event PreNotifyInput
, but i found and equivalent PreviewTextInput
根据上一个条目,我发现在WPF中没有这样的事件PreNotifyInput,但是我找到了和等效的PreviewTextInput
First I try with a RegExp
, but I cant make it work, then I use a simple indexOf
.
首先我尝试使用RegExp,但是我不能使它正常工作,然后我使用一个简单的indexOf。
private bool ValidChar(string _char)
{
string Lista = @" ! "" # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ";
return Lista.IndexOf(_char.ToUpper()) != -1;
//System.Text.RegularExpressions.Regex RegVal = new System.Text.RegularExpressions.Regex(@"(?<LETRAS>[A-Z]+)+(?<NUMERO>[0-9]+)+(?<CAR>[!|""|#|$|%|&|'|(|)|*|+|,|\-|.|/|:|;|<|=|>|?|@]+)+");
//return RegVal.IsMatch(_char);
}
private void textBoxDescripcion_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!ValidChar(e.Text))
e.Handled = true;
}
#4
1
I know this is old, but none of the answers seem to actually answer the question. The reason a different char is coming back is because when you just try to cast it to a char
you are casting the enum value to a 'char'. However:
我知道这是一个古老的问题,但似乎没有一个答案能真正回答这个问题。另一个char返回的原因是,当您尝试将它转换为char时,您将enum值转换为“char”。然而:
var keyPressed = e.key.ToString();
Works great. Returns the key pressed as a string. Then you check the length. If it's == 1 then it's a char, number or symbol. If it's greater than 1 it's a special key.
伟大的工作。返回按为字符串的键。然后检查长度。如果它是= 1,那么它就是一个字符、数字或符号。如果大于1,它就是一个特殊的键。
If you just want the char you can then do keyPressed[0];
如果你只是想要字符,你可以做按键[0];
This is how I do it.
我就是这么做的。
private void scrollViewer_KeyDown(object sender, KeyEventArgs e)
{
if (!e.IsRepeat)
{
var keyPressed = e.Key.ToString();
if(keyPressed.Length == 1)
CharKeyPressed(keyPressed[0]);
else if(keyPressed.Length > 1)
HandleSpecialKey(keyPressed)
}
}
#5
0
Inside your PreNotifyInput handler, try something like this:
在您的PreNotifyInput处理程序中,尝试如下方法:
if (e.StagingItem.Input is System.Windows.Input.TextCompositionEventArgs)
{
if (!String.IsNullOrEmpty((e.StagingItem.Input as System.Windows.Input.TextCompositionEventArgs).Text))
{
Char c = (e.StagingItem.Input as System.Windows.Input.TextCompositionEventArgs).Text[0];
}
}
It raises multiple times for the different routed events, so you may want to filter for a particular one.
它会为不同的路由事件引发多次事件,因此您可能希望对特定的事件进行筛选。