本文介绍了UWP - InjectedInputKeyboardInfo 如何发送非英文按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Visual Studio 2017 中有一个 UWP 应用.我正在尝试制作多语言屏幕键盘.目前,英文击键工作正常,但是来自其他语言的任何字母都会抛出 System.ArgumentException: 'Value does not fall in the expected range.'

I have a UWP app in Visual Studio 2017. I'm trying to make a multi-language on-screen keyboard.Currently the English keystrokes are working fine, however any letter from other languages throws System.ArgumentException: 'Value does not fall within the expected range.'

这是发送击键的代码:

public void SendKey(ushort keyCode)
{
    List<InjectedInputKeyboardInfo> inputs = new List<InjectedInputKeyboardInfo>();
    InjectedInputKeyboardInfo myinput = new InjectedInputKeyboardInfo();

    myinput.VirtualKey = keyCode;

    inputs.Add(myinput);
    var injector = InputInjector.TryCreate();

    WebViewDemo.Focus(FocusState.Keyboard);
    injector.InjectKeyboardInput(inputs); // exception throws here
}

我将如何注入其他语言的字母?

How would I inject letters from other languages?

推荐答案

诀窍在于 InputInjector 不是注入文本(字符),而是注入键盘上的击键.这意味着输入将不是 VirtualKey 值包含的名称值,而是 给定的键在用户当前使用的键盘上代表什么.

The trick is that InputInjector isn't injecting text (characters), but actually it is injecting key strokes on the keyboard. That means the input will be not what the VirtualKey value contains as the name value, but what the given key represents on the keyboard the user is currently using.

例如在捷克语中,我们使用最上面的数字行来写ě"、š"等字符.所以当你按下键盘上的数字 3 时,捷克语键盘会写š".

For example in Czech language we use the top numeric row to write characters like "ě", "š" and so on. So when you press number 3 on the keyboard, Czech keyboard writes "š".

如果我使用带有 Number3 值的代码:

If I use your code with Number3 value:

SendKey( (ushort)VirtualKey.Number3 );

我得到š"作为输出.同样的事情也适用于日语,例如 VirtualKey.A 实际上会映射到あ".

I get "š" as the output. The same thing holds for Japanese for example where VirtualKey.A will actually map to "あ".

这使得键盘的 InputInjector 使用起来有点不方便,因为您无法预测用户实际使用的是哪种语言以及正在发生哪种键盘键映射,但是经过反思它是有意义的这样,因为它不是注入文本,而是模拟实际的键盘击键.

That makes InputInjector for keyboard a bit inconvenient to use, because you cannot predict which language the user is actually using a which keyboard key mapping is taking place, but after reflection it makes sense it is implemented this way, because it is not injection of text, but simulation of actual keyboard keystrokes.

这篇关于UWP - InjectedInputKeyboardInfo 如何发送非英文按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 11:34