I would like to detect a keypress event only when a non-modifier key has pressed. I then also would like to know which modifier keys have been pressed at the same time.
For Instance:
When I press Shift:_______ I would like to output nothing.
When I press Shift+A:_____ I would like to output "Shift A".
When I press Shift+Alt+A:__ I would like to output "Shift+Alt A"
When I press Alt+A:_______ I would like to output "Alt A"
However, "Alt A" does not show because without Shift/Ctrl pressed, Alt key down can't be detected with Keyboard.IsKeyDown
Following is the part of my test code.
private void Window_KeyDown(object sender, KeyEventArgs e)
{
Key key = e.Key;
if (key.ToString().Contains("Ctrl") | key.ToString().Contains("Shift") | key.ToString().Contains("Alt"))
return;
Label_Test.Content = "";
if (Keyboard.IsKeyDown(Key.LeftShift) | Keyboard.IsKeyDown(Key.RightShift))
{
if (Label_Test.Content.ToString() != "")
Label_Test.Content += "+";
Label_Test.Content += "Shift";
}
if (Keyboard.IsKeyDown(Key.LeftCtrl) | Keyboard.IsKeyDown(Key.RightCtrl))
{
if (Label_Test.Content.ToString() != "")
Label_Test.Content += "+";
Label_Test.Content += "Ctrl";
}
if (Keyboard.IsKeyDown(Key.LeftAlt) | Keyboard.IsKeyDown(Key.RightAlt))
{
if (Label_Test.Content.ToString() != "")
Label_Test.Content += "+";
Label_Test.Content += "Alt";
}
if (Keyboard.IsKeyDown(Key.System))
{
if (Label_Test.Content.ToString() != "")
Label_Test.Content += "+";
Label_Test.Content += "Alt";
}
if (Label_Test.Content.ToString() != "")
Label_Test.Content += " ";
Label_Test.Content += key.ToString();
}
I also tried
if (e.KeyboardDevice.Modifiers.ToString().Contains("Alt"))
but the result was same. How could I detect "Alt + A"?
EDIT: Thanks, guys for your help. I edited the last if statement which checks for Key.System like this and It just worked as I wanted.
if (e.Key == Key.System)
{
key = e.SystemKey;
if (e.SystemKey.HasFlag(Key.LeftAlt) | e.SystemKey.HasFlag(Key.RightAlt))
return;
Label_Test.Content = "Alt";
}