Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InputKeyboardSource.GetKeyStateForCurrentThread is often wrong #4017

Open
stevenbrix opened this issue Dec 4, 2023 · 3 comments
Open

InputKeyboardSource.GetKeyStateForCurrentThread is often wrong #4017

stevenbrix opened this issue Dec 4, 2023 · 3 comments

Comments

@stevenbrix
Copy link

Describe the bug

the InputKeyboardSource.GetKeyStateForCurrentThread is often wrong and reporting keys as being Locked when they aren't

InputKeyboardSourceRepro.zip

Steps to reproduce the bug

  1. download attached zip and build/launch project
  2. observe text block saying "Unclicked"
  3. click the button
  4. see text block change from "Unclicked" to "Locked"

play around with the project and you'll notice that it's inconsistent. so if the above steps don't immediately cause a repro, just try a few more times and you should hit it

Expected behavior

the textblock to accurately reflect the state of the key

Screenshots

input.keyboard.repro.mp4

NuGet package version

Windows App SDK 1.4.3: 1.4.231115000

Packaging type

Packaged (MSIX)

Windows version

Windows 11 version 22H2 (22621, 2022 Update)

IDE

No response

Additional context

No response

@hawkerm
Copy link

hawkerm commented Nov 26, 2024

I was trying to use this class and also noticing odd behavior. I figured I should just use the events on InputKeyboardSource as well, but not sure how to access them as I don't have a ContentIsland; I just want to use them from my WinUI 3 app and respond to keyboard events within my controls/application.

@hawkerm
Copy link

hawkerm commented Nov 26, 2024

Found out how to use InputKeyboardSource directly: #2685 (reply in thread)

@castorix
Copy link

...or by modifying code from @stevenbrix :

    private void myButton_Click(object sender, RoutedEventArgs e)
    {
        bool bAltDown = (Microsoft.UI.Input.InputKeyboardSource.GetKeyStateForCurrentThread(Windows.System.VirtualKey.Menu).HasFlag(Windows.UI.Core.CoreVirtualKeyStates.Down));
        MenuKeyState.Text = bAltDown ? ("Alt Down") : ("Alt Up");
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants