Search code examples
c#uwpscreenshot

Win11 launch screen snipping in C# uwp works different


I have implemented to launch screen snipping tool in my C# uwp application and it works fine in windows 10 operation system. The expected behavior is to reach the state as if we pressed the combination windows + shift + s.

Here is the body of my command:

bool result = await Launcher.LaunchUriAsync(new Uri("ms- 
screenclip:edit?source=SOURCE"));

 if (result)
 {
     Clipboard.ContentChanged += Screenshot_Succeeded;
 }

In windows 11 operation system the same implementation just launches the snipping tool, and i need to click on new screenshot to reach the same state.

So the main problem is screenclip works as screensketch in win11.

Is that possible to reach the same functionallity just like in windows 10?


Solution

  • Can you help me how can i get the same functionallity as win10 operation system?

    A workaround is that you could try to inject combined key inputs from code so you could directly start the snipping function like the old behavior in Windows 10.

    Before we start to use the InputInjector Class, we will need to add inputInjectionBrokered restricted capability into the manifest file first.

    <Package
       ...
       xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
     IgnorableNamespaces="uap mp rescap">
    

    And in the Capabilities selection

    <Capabilities>
      <Capability Name="internetClient" />
      <rescap:Capability Name="inputInjectionBrokered" />
    </Capabilities>
    

    Then you could use the following code to inject keyboard input to directly start the snipping function.

      private  void Button_Click(object sender, RoutedEventArgs e)
        {
            //press key
            InputInjector inputInjector = InputInjector.TryCreate();
    
            var shift = new InjectedInputKeyboardInfo();
            shift.VirtualKey = (ushort)(VirtualKey.Shift);
            shift.KeyOptions = InjectedInputKeyOptions.ExtendedKey;
    
    
            var win = new InjectedInputKeyboardInfo();
            win.VirtualKey = (ushort)(VirtualKey.LeftWindows);
            win.KeyOptions = InjectedInputKeyOptions.ExtendedKey;
    
    
            var skey = new InjectedInputKeyboardInfo();
            skey.VirtualKey = (ushort)(VirtualKey.S);
            skey.KeyOptions = InjectedInputKeyOptions.None;
    
    
            inputInjector.InjectKeyboardInput(new[] { shift, win,skey });
    
            // release shift and win key
            shift.KeyOptions = InjectedInputKeyOptions.KeyUp;
            win.KeyOptions = InjectedInputKeyOptions.KeyUp;
            inputInjector.InjectKeyboardInput(new[] { shift, win });
    
    
            //bool result = await Launcher.LaunchUriAsync(new Uri("ms-screenclip: edit ? source = SOURCE"));
        }