question

ansalc-1556 avatar image
0 Votes"
ansalc-1556 asked ·

Keyboard injection of symbols

How do I inject keyboard characters that are not alphabetical nor numerical, like the % or # ?

With the standard code below, or similar, I can inject alphabetical characters and numbers.

I have confirmed that info.ScanCode opens the possibility to inject other characters, but I can't find the way to map ScanCodes to Virtual Keys.

         foreach (char character in login0)
         {
             info.VirtualKey = (ushort)((VirtualKey)Enum.Parse(typeof(VirtualKey), character.ToString(), true));

             inputInjector.InjectKeyboardInput(new[] { info });
             await Task.Delay(40).ConfigureAwait(false);
         }

windows-uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RichardZhang-MSFT avatar image
0 Votes"
RichardZhang-MSFT answered ·

Hello,

Welcome to Microsoft Q&A!

Special characters cannot be directly mapped to a single VirtualKey. Taking "&" as an example, we use key combinations on most keyboards: Shift + 7. If you want to adapt to other characters, you need to write the key combination list yourself and match according to the input characters. This is a very tedious thing, and it is not universal.

But triggered from the perspective of text input, each character corresponds to a unicode, we can accurately identify each character through Unicode, no longer need to consider the combination of VirtualKey mapping.

In InputInjector, we can do this by setting KeyOptions:

 InputInjector inputInjector = InputInjector.TryCreate();
 foreach (var letter in "hello&yo")
 {
     var info = new InjectedInputKeyboardInfo();
     info.ScanCode = (ushort)letter;
     info.KeyOptions = InjectedInputKeyOptions.Unicode;
     inputInjector.InjectKeyboardInput(new[] { info });
     info.KeyOptions = InjectedInputKeyOptions.KeyUp;
     inputInjector.InjectKeyboardInput(new[] { info });
 }

In the input text, we include the special characters &, but through Unicode, there is no difference between entering special characters and entering ordinary letters, because they all have exclusive Unicode.

So this is a general method. You don't have to consider how to use VirtualKey for special characters, just use Unicode to recognize characters.

Thanks.

· 3 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes, I understand what you say and agree that it is a solution, which I do to capitalize letters.

However, it is not general. One has to program each single special character, that is too much coding.

I was looking for something that, given a special character gives me the ScanCode that I can use in VirtualKey.ScanCode

0 Votes 0 · ·

I'm sorry to get you confused. I have modified my answer please kindly check it.

0 Votes 0 · ·

Hi, have you solved the problem? If you have other questions, please feel free to ask.

0 Votes 0 · ·