3
votes

I have a WPF application that needs to respond when a modifier key (e.g. Alt or Control) is first pressed. The response needs to occur regardless of whether or not other keys are pressed at the same, so I basically need to be able to tell the instant any of those keys goes down.

The eventing behavior that I'm seeing for modifier keys is different than other keys --- it looks like the KeyDown event and PreviewKeyDown events actually fire when the button comes UP after being pressed, which isn't helpful.

While digging into the event firing issue, I created a timed background loop that polls all of the current key states. Even that isn't working --- if I press down a modifier key, and I don't release the key or press any other keys, this poll incorrectly says that the modifier key is not actually down (indefinitely, until another key gets pressed)

I feel like I must be missing something basic, but no luck so far. Any ideas?

2
Check Ray Burns explanation in that thread stackoverflow.com/questions/3099472/…nik

2 Answers

0
votes

Are you attaching to the "Keyboard.KeyDown" event on your main Window instance?

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Keyboard.KeyDown="Window_KeyDown">

</Window>
0
votes

Turns out this behavior is specific to my environment (running Windows & Visual Studio in a Parallels 6.0 VM). Parallels must be intercepting these events to preserve some low-level Mac OS functionality. I'm not sure about other VM setups, but this looks like a non-issue in native Windows environments.