KeyEventArgs Event Chain. Altered Event data when another event with the same shared class is raised (no snapshot?)

  • Thread starter Thread starter Erythana
  • Start date Start date
E

Erythana

Guest
Hello!

I am overriding an PreviewKeyDown Event and listening for Keystrokes with an Modifier-Key. When i hit the desired key i call a method which evaluates the input and shows a dialog (which requires a user action).
The Problem here is that, whenever another (input) event is raised (in my example a MouseEventArgs), the modifier key from the first event in the KeyboardDevice class gets altered (Even though they don't even share that class in the EventArgs?). When i hold the modifier key and click on the button on the dialog, i receive the modifier key on the code below.


I didn't figure out why that would happen at all. According to a book (https://www.oreilly.com/library/view/programming-wpf-2nd/9780596510374/ch04.html ---- Chapter 4-13) there should be some sort of snapshot from the time the event has been raised.
Some Events share the event data among each other (https://docs.microsoft.com/en-us/dotnet/api/system.windows.input.keyeventargs?view=netcore-3.1), so i guess that is also the case here.


What is the expected behaviour regarding modifiers on the first event being raised?

Here i narrowed down the problem, it is still the same event being raised, not altered by me:

Tested with .NET Framework 4.7.1, 4.8 and .NET Core:

protected override void OnPreviewKeyDown(KeyEventArgs e)
{
base.OnPreviewKeyDown(e);
if (e.Key == Key.S && e.KeyboardDevice.Modifiers == ModifierKeys.Control)
{
Debug.WriteLine($@"Debugging Event. Before Validator-Chain. Hash: {e.GetHashCode()}, Key-Modifier: {e.KeyboardDevice.Modifiers}"); //Debugging Event. Before Validator-Chain. Hash: 64833183, Key-Modifier: Control
MessageBox.Show("test", "", MessageBoxButton.YesNo);
Debug.WriteLine($@"Debugging Event. After Validator-Chain. Hash: {e.GetHashCode()}, Key-Modifier: {e.KeyboardDevice.Modifiers}"); //Debugging Event. After Validator-Chain. Hash: 64833183, Key-Modifier: None
}
}



The problem with this beviour (in my case) is that i alter a textbox, save with the Gesture (also happens with KeyBinding) Ctrl+S and the modifier gets removed during validation and the framework further down the chain seems to see the keystroke "Ctrl+S" as a pure "S" keystroke and therefor alters and overwrites the content of a textbox.

If you need further information, please let me know as this is my first issue...

Edit: I think this is a .NET Topic (as this also happens with VB.NET), but i got refered here first..

Continue reading...
 
Back
Top