UWP - InjectedInputKeyboardInfo how to send non-English keystrokes

杀马特。学长 韩版系。学妹 提交于 2020-01-25 07:34:12

问题


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.'

Here is the code that sends the keystrokes:

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?


回答1:


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.

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 "š".

If I use your code with Number3 value:

SendKey( (ushort)VirtualKey.Number3 );

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

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.




回答2:


The accepted answer is not true. You can send any unicode character.

InputInjector inputInjector = InputInjector.TryCreate();
var key = new InjectedInputKeyboardInfo();
key.ScanCode = (ushort)'Ä';
key.KeyOptions = InjectedInputKeyOptions.Unicode;

inputInjector.InjectKeyboardInput(new[] { key });

The InjectKeyboardInput method is using this function behind the scenes. Please note the that you require the inputInjectionBrokered capability in your app.



来源:https://stackoverflow.com/questions/48879320/uwp-injectedinputkeyboardinfo-how-to-send-non-english-keystrokes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!