将 PreviewKeyDown 中收到的密钥转换为字符串

发布于 2024-08-05 23:40:30 字数 308 浏览 4 评论 0原文

我在窗口上使用 PreviewKeyDown 事件来接收来自条形码扫描仪的所有键。 KeyEventArgs 是一个枚举,没有给我实际的字符串。我不想使用 TextInput,因为某些键可能由控件本身处理,并且可能不会冒泡到 TextInput 事件。

我正在寻找一种方法将 PreviewKeyDown 中获得的键转换为实际字符串。 我查看了 InputManager、TextCompositionManager 等,但没有找到一种方法来给出键列表并返回一个字符串。 TextCompositionManager 或其他东西必须将这些键转换为 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒带 2024-08-12 23:40:30

这是我正在使用的事件。 KeyDown 获取按键,PreviewTextInput 获取实际文本。因此,在按键之间的某个位置正在转换为文本。

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

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)
            {
            }
独行侠 2024-08-12 23:40:30

按键->文本转换比您想象的要复杂得多,实际上没有办法将单个击键映射到单个字符,因为在某些语言和某些情况下,您需要多次击键才能组成单个字符。

由于您对条形码扫描仪的输入感兴趣(我假设只会生成 Windows 可以处理的一小部分,可能只有 ASCII,甚至更少),您可以自己构建转换表并将其硬编码到您的程序中 - 这非常多然后更容易处理 Windows 文本处理所做的所有疯狂事情(为了好玩,查找“死键”)。

Key -> Text conversion is much more complicated than you think, there is actually no way to map a single key stroke to a single character because in some languages and some cases you need multiple keystrokes to compose a single character.

Since you are interested in input from a barcode scanner (that I assume will only generate a small subset of what windows can handle, maybe only ASCII maybe even less) you can build the conversion table yourself and hard code it into your program - it's much easier then to handle all the craziness that Windows text handling does (for fun, lookup "dead keys").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文