将PreviewKeyDown中收到的密钥转换为字符串 [英] Convert received keys in PreviewKeyDown to a string
问题描述
我在窗口上使用PreviewKeyDown事件接收条形码扫描仪的所有键. KeyEventArgs是一个枚举,没有给我实际的字符串.我不想使用TextInput,因为某些键可能会由控件本身处理,并且可能不会冒泡到TextInput事件.
I am using PreviewKeyDown event on a window to receive all the keys from a barcode scanner. The KeyEventArgs is an enumeration and does not given me the actual string. I dont want to use TextInput as some of the keys may get handled by the control itself and may not bubble up to the TextInput event.
我正在寻找一种将在PreviewKeyDown中获得的键转换为实际字符串的方法. 我看了一下InputManager,TextCompositionManager等,但没有找到我给出按键列表的方法,它返回了一个字符串. TextCompositionManager或某些东西必须将这些Key转换为TextInput中可用的字符串.
I am looking for a way to convert the the Keys that I get in PreviewKeyDown to actual string. I looked at the InputManager, TextCompositionManager etc but I am not finding a way where I give the list of keys and it comes back with a string. TextCompositionManager or something must be converting these Keys to a string which is what is available in TextInput.
推荐答案
这是我正在使用的事件. KeyDown获取键,PreviewTextInput获取实际文本.因此,键之间的某处将转换为文本.
Here is the event that I am using. The KeyDown gets the keys and the PreviewTextInput gets the actual text. So somewhere in between the keys are getting converted to text.
public Window1()
{
InitializeComponent();
TextCompositionManager.AddPreviewTextInputStartHandler(this, new TextCompositionEventHandler(Window_PreviewTextInput));
this.AddHandler(Window.KeyDownEvent, new System.Windows.Input.KeyEventHandler(Window_KeyDown), true);
}
private void Window_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
}
private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
}
这篇关于将PreviewKeyDown中收到的密钥转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!