Caps Lock off

ansalc 436 Reputation points
2020-04-27T07:40:48.227+00:00

In Visual Basic, how do I check if the Caps Lock key is locked (on) ?

The following do not work

  1. If Window.Current.CoreWindow.GetKeyState(VirtualKey.CapitalLock).Locked Then... evaluated as True always
  2. If Window.Current.CoreWindow.GetKeyState(VirtualKey.CapitalLock).HasFlag(VirtualKey.Down) Then... throws an error
Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 32,731 Reputation points Microsoft Vendor
    2020-04-28T03:19:34.33+00:00

    Hello,

    Welcome to Micorsoft Q&A!

    You need to check both VirtualKey.state and CoreVirtualKeyStates to confirm if the Caps Lock button is on.

    And for another question - switch the caps lock key's states, you need to add InjectedInputKeyOptions for the InputInjector object

    You could use the following code:

            // detect the caps lock key state  
            Dim keystate = Window.Current.CoreWindow.GetKeyState(VirtualKey.CapitalLock)  
            Dim isNumLocked = (keystate And CoreVirtualKeyStates.Locked) <> 0  
    
    
            // inject a caps lock key press no matter what's the state of it.  
        Dim inputInjector As InputInjector = InputInjector.TryCreate()  
    
        Dim info = New InjectedInputKeyboardInfo()  
        info.VirtualKey = VirtualKey.CapitalLock  
    
        'press the key  
        info.KeyOptions = InjectedInputKeyOptions.ExtendedKey  
        inputInjector.InjectKeyboardInput({info})  
        Await Task.Delay(100)  
    
        'release the caps lock key. If you do not release the key, the cap lock will remain pressed state  
        info.KeyOptions = InjectedInputKeyOptions.KeyUp  
        inputInjector.InjectKeyboardInput({info})  
        Await Task.Delay(100)  
    

    Thank you!


3 additional answers

Sort by: Most helpful
  1. ansalc 436 Reputation points
    2020-04-29T09:13:42.91+00:00

    For me it does not work. If I have the Caps Lock on and run the following code by pressing Button1 the Caps Lock, The Caps Lock continues as On.

    It will start working fine from them on.

    It is as if the app does not capture the initial state of the Caps Lock key. Only after it has been refreshed by the app.

    Private Async Sub Button1_Click(sender As Object, e As RoutedEventArgs)
    
        Dim inputInfo As New InjectedInputKeyboardInfo
        Dim info As New InjectedInputKeyboardInfo()
        Dim inputInjector As InputInjector = InputInjector.TryCreate()
    
    
        Dim keystate = Window.Current.CoreWindow.GetKeyState(VirtualKey.CapitalLock)
        Dim isNumLocked = (keystate And Windows.UI.Core.CoreVirtualKeyStates.Locked) <> 0
    
    
        If isNumLocked Then
    
            info.VirtualKey = VirtualKey.CapitalLock
    
            'press the key
            info.KeyOptions = InjectedInputKeyOptions.ExtendedKey
            inputInjector.InjectKeyboardInput({info})
            Await Task.Delay(100)
    
            'release the caps lock key. If you do not release the key, the cap lock will remain pressed state
            info.KeyOptions = InjectedInputKeyOptions.KeyUp
            inputInjector.InjectKeyboardInput({info})
            Await Task.Delay(100)
    
        End If
    
    End Sub
    

  2. ansalc 436 Reputation points
    2020-04-30T09:38:05.63+00:00

    I've found (almost) a solution, to put the code in the MainPage loaded event. It is not ideal because the timing of the Caps Lock status change is not exactly when needed

    Why does it not work if it is in the Button clicked event? Hard to understand

    Private Async Sub MainPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
    
        Dim inputInfo As New InjectedInputKeyboardInfo
        Dim info As New InjectedInputKeyboardInfo()
        Dim inputInjector As InputInjector = InputInjector.TryCreate()
    
        If Window.Current.CoreWindow.GetKeyState(VirtualKey.CapitalLock).HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Locked) Then
    
            info.VirtualKey = VirtualKey.CapitalLock
    
            info.KeyOptions = InjectedInputKeyOptions.None
            inputInjector.InjectKeyboardInput({info})
            Await Task.Delay(100)
    
    
            info.KeyOptions = InjectedInputKeyOptions.KeyUp
            inputInjector.InjectKeyboardInput({info})
            Await Task.Delay(100)
    
        End If
    
    
    End Sub
    
    0 comments No comments

  3. ansalc 436 Reputation points
    2020-05-01T23:14:52.133+00:00

    I've found the reason for my trouble.

    Before the Caps Lock check I had:

    Await Task.Delay(5000).ConfigureAwait(False)

    I've changed it to:

    Await Task.Delay(5000).ConfigureAwait(True)

    And all is working fine. This is due to True continuing on Captured Context. With False it did not capture the Window.Current.CoreWindow status.

    I've found this out, because I wrote my project in C# and C# alerted me that there was no instance of Window.Current.CoreWindow, which brought my attention to the ConfigureAwait above.

    The Visual Basic compiler did not alert me of the problem.

    0 comments No comments