question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to know when a modifier is pressed on KeyDown event? WinUI3 repview4

See original GitHub issue

In preview3 of WinUI3 I was using the following code to know whether the Shift key was pressed while listening to the KeyDown event

var cw = CoreWindow.GetForCurrentThread();
return cw.GetAsyncKeyState(VirtualKey.Shift) != CoreVirtualKeyStates.None;

This stopped working on preview4 (for Desktop)

The KeyRoutedEventArgs doesn’t come with a KeyModifiers property as the PointerRoutedEventArgs.

How should we detect shift or control pressed in KeyDown event?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
d-goodscommented, Oct 28, 2021

@sassy224 Microsoft.UI.Input.KeyboardInput.GetKeyStateForCurrentThread is now Microsoft.UI.Input.InputKeyboardSource.GetKeyStateForCurrentThread starting with Windows AppSDK 1.0.0-preview2

See other changes here, documented by .Morten: https://twitter.com/dotMorten/status/1445821064269860867

0reactions
sassy224commented, Oct 11, 2021

I found that using Microsoft.UI.Xaml.Input.KeyRoutedEventArgs is working, something like keyEventArgs.Key == Key.Control; is working for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

KeyboardAccelerator.Modifiers Property - Windows
A keyboard shortcut is invoked when the modifier keys associated with the shortcut are pressed and then the non-modifier key is pressed at...
Read more >
How to: Determine Which Modifier Key Was Pressed - ...
Use the bitwise AND operator with the ModifierKeys property and a value of the Keys enumeration to determine whether a particular modifier key ......
Read more >
c# - Detecting modifier key down?
The eventing behavior that I'm seeing for modifier keys is different than other keys --- it looks like the KeyDown event and PreviewKeyDown ......
Read more >
KeyboardEvent: getModifierState() method - Web APIs | MDN
The KeyboardEvent.getModifierState() method returns the current state of the specified modifier key: true if the modifier is active (that is ...
Read more >
How to detect the key events in JavaScript
The keydown event is fired repeatedly when the key is pressed for a long time without releasing. We can detect if the event...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found