none
Injecting capital case letters

    Question

  • With the code below, I am able to inject lower case characters.

    It does not work with numerals or special keys (like Shift, Ctrl, etc.), but these can be injected separately.

    How can I inject upper case letters?

    If I inject the Caps Lock virtual key all the remaining letters are injected in upper case even if I inject the Caps Lock virtual key a second time.

    Is there a more general mapping of characters to virtual keys than the one below with the (limited functionality) Parse/CType functions?

    Dim inputInjector As InputInjector = InputInjector.TryCreate() For Each letter In "hello" Dim info As New InjectedInputKeyboardInfo() info.VirtualKey = CType(CType([Enum].Parse(GetType(VirtualKey), letter.ToString(), True), VirtualKey), UShort) inputInjector.InjectKeyboardInput(New InjectedInputKeyboardInfo() {info}) Await Task.Delay(100) Next



    • Edited by AntSalc Wednesday, March 13, 2019 12:56 PM
    Wednesday, March 13, 2019 12:33 PM

Answers

  • Hi,

    If you want to use shift to actualize mixed alphabetic and case output, you can use InjectedInputKeyOptions.None and InjectedInputKeyOptions.KeyUp. Use None to capitalize letters and Keyup to cancel capitalization locking. The code is as follows.

    private async void ShowOne(string mystring) { show.Focus(FocusState.Programmatic); await Task.Delay(100); InputInjector injector = InputInjector.TryCreate(); List<InjectedInputKeyboardInfo> inputs = new List<InjectedInputKeyboardInfo>(); InjectedInputKeyboardInfo leftshiftkeydown = new InjectedInputKeyboardInfo(); for (int i=0;i<mystring.Length;i++) { if (i == 0) { leftshiftkeydown.VirtualKey = (ushort)((VirtualKey)Enum.Parse(typeof(VirtualKey), "LeftShift", true)); leftshiftkeydown.KeyOptions = InjectedInputKeyOptions.None; inputs.Add(leftshiftkeydown); await Task.Delay(100);

    InjectedInputKeyboardInfo hkey = new InjectedInputKeyboardInfo(); hkey.VirtualKey = (ushort)((VirtualKey)Enum.Parse(typeof(VirtualKey), mystring[i].ToString(), true)); hkey.KeyOptions = InjectedInputKeyOptions.None; inputs.Add(hkey); await Task.Delay(100);

    InjectedInputKeyboardInfo leftshiftkeyup = new InjectedInputKeyboardInfo(); leftshiftkeyup.VirtualKey = (ushort)((VirtualKey)Enum.Parse(typeof(VirtualKey), "LeftShift", true)); leftshiftkeyup.KeyOptions = InjectedInputKeyOptions.KeyUp; inputs.Add(leftshiftkeyup); await Task.Delay(100);

    injector.InjectKeyboardInput(inputs); } else { var info = new InjectedInputKeyboardInfo(); info.VirtualKey = (ushort)(VirtualKey)Enum.Parse(typeof(VirtualKey), mystring[i].ToString(), true); injector.InjectKeyboardInput(new[] { info }); await Task.Delay(100); } } }



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AntSalc 13 hours 41 minutes ago
    17 hours 5 minutes ago
    Moderator

All replies

  • Hi,

    From your description, I think you want to mix case and lowercase characters in a string of characters. Assuming the output is uppercase 'H' and lowercase 'ello,' the best I can think of is to simulate the keyboard output twice. The first time you print capital 'H', press the caps lock key. The second output of 'ello' unlocks the caps. InpurtInjector is used to simulate keyboard input, and it may not be feasible to get mixed uppercase and lowercase text all at once. The code is shown below. This is C# code and you could write VB code referencing it. 

    private async void SecondText()
            {
                show.Focus(FocusState.Programmatic);
                await Task.Delay(100); //we must yield the UI thread so that focus can be acquired
    
                InputInjector inputInjector = InputInjector.TryCreate();
                foreach (var letter in "ello")
                {
                    var info = new InjectedInputKeyboardInfo();
                    info.VirtualKey = (ushort)(VirtualKey)Enum.Parse(typeof(VirtualKey), letter.ToString(), true);
                    //info.VirtualKey = (ushort)(VirtualKey)Enum.Parse(typeof(VirtualKey), letter.ToString(), true);
                    inputInjector.InjectKeyboardInput(new[] { info });
                    await Task.Delay(100);
                }
            }
            private async void OneText()
            {
                show.Focus(FocusState.Programmatic);
                await Task.Delay(100); //we must yield the UI thread so that focus can be acquired
                string a = "H";
                InputInjector injector =  InputInjector.TryCreate();
                InjectedInputKeyboardInfo keyinfo = new InjectedInputKeyboardInfo();
                keyinfo.VirtualKey = (ushort)(VirtualKey)Enum.Parse(typeof(VirtualKey), a.ToString(), true); ;
                InjectedInputKeyboardInfo[] infos ={keyinfo};
                injector.InjectKeyboardInput(infos);
                await Task.Delay(100);
            }

    It might be a limitation for this API. You could request a feature request about this in user voice and share the link with me.

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 14, 2019 10:11 AM
    Moderator
  • Hi Roy,

    Thank you for your suggested solution. 

    It seems it requires human intervention, which is not ideal. I would imagine that injecting a mix of capital and lower case letters should be readily available. It is a basic need.

    I wonder if InjectedInputKeyboardInfo() can combine info.VirtualKey = VirtualKeyModifiers.Shift  with the info.VirtualKey = VirtualKey.H so that the two together produce the capital case H letter.

    I've tried several combinations without success



    • Edited by AntSalc Thursday, March 14, 2019 11:14 AM
    Thursday, March 14, 2019 11:12 AM
  • Hi,

    If you want to use shift to actualize mixed alphabetic and case output, you can use InjectedInputKeyOptions.None and InjectedInputKeyOptions.KeyUp. Use None to capitalize letters and Keyup to cancel capitalization locking. The code is as follows.

    private async void ShowOne(string mystring) { show.Focus(FocusState.Programmatic); await Task.Delay(100); InputInjector injector = InputInjector.TryCreate(); List<InjectedInputKeyboardInfo> inputs = new List<InjectedInputKeyboardInfo>(); InjectedInputKeyboardInfo leftshiftkeydown = new InjectedInputKeyboardInfo(); for (int i=0;i<mystring.Length;i++) { if (i == 0) { leftshiftkeydown.VirtualKey = (ushort)((VirtualKey)Enum.Parse(typeof(VirtualKey), "LeftShift", true)); leftshiftkeydown.KeyOptions = InjectedInputKeyOptions.None; inputs.Add(leftshiftkeydown); await Task.Delay(100);

    InjectedInputKeyboardInfo hkey = new InjectedInputKeyboardInfo(); hkey.VirtualKey = (ushort)((VirtualKey)Enum.Parse(typeof(VirtualKey), mystring[i].ToString(), true)); hkey.KeyOptions = InjectedInputKeyOptions.None; inputs.Add(hkey); await Task.Delay(100);

    InjectedInputKeyboardInfo leftshiftkeyup = new InjectedInputKeyboardInfo(); leftshiftkeyup.VirtualKey = (ushort)((VirtualKey)Enum.Parse(typeof(VirtualKey), "LeftShift", true)); leftshiftkeyup.KeyOptions = InjectedInputKeyOptions.KeyUp; inputs.Add(leftshiftkeyup); await Task.Delay(100);

    injector.InjectKeyboardInput(inputs); } else { var info = new InjectedInputKeyboardInfo(); info.VirtualKey = (ushort)(VirtualKey)Enum.Parse(typeof(VirtualKey), mystring[i].ToString(), true); injector.InjectKeyboardInput(new[] { info }); await Task.Delay(100); } } }



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AntSalc 13 hours 41 minutes ago
    17 hours 5 minutes ago
    Moderator
  • Wow! Barry, many thanks.

    It is solid coding and certainly works. I felt that it was something available. The .Add method above and the injection of combined added info are the key.

    It now seems logical and makes perfect sense, but I wished it had been easier to find out about it.

    Below is my VB code for capital a.

    Many thanks again.

    Antonio

     Dim capkeyInputs As New List(Of InjectedInputKeyboardInfo)
    
           
            Dim inputInjector As InputInjector = InputInjector.TryCreate()
    
            Dim leftshiftkeydown As New InjectedInputKeyboardInfo With {.KeyOptions = InjectedInputKeyOptions.None, .VirtualKey = VirtualKey.LeftShift}
    
            capkeyInputs.Add(leftshiftkeydown)
            Dim capitalizedkey As New InjectedInputKeyboardInfo With {.VirtualKey = VirtualKey.A}
            capkeyInputs.Add(capitalizedkey)
    
            Dim leftshiftkeyup As New InjectedInputKeyboardInfo With {.KeyOptions = InjectedInputKeyOptions.KeyUp, .VirtualKey = VirtualKey.LeftShift}
    
            capkeyInputs.Add(leftshiftkeyup)
    
            inputInjector.InjectKeyboardInput(capkeyInputs)

     
    13 hours 29 minutes ago