I would like to check if the alt
key was pressed in c#. I could only find old information that didn't work. It would be helpful if you send an example with the alt
key. Thanks in advance!
Asked
Active
Viewed 1,125 times
0

Fej1Fun
- 13
- 2
-
Please, specify UI: Win Forms, WPF, something else? – Dmitry Bychenko Mar 04 '22 at 20:32
2 Answers
1
If you need to detect a modifier key outside of a KeyEventHandler
method, you can use the static Keyboard
class from System.Windows.Input
.
Example from my WPF .NET 6.0 Application on Windows
if (Keyboard.Modifiers.HasFlag(ModifierKeys.Alt))
{
// Do something
}

Fixation
- 969
- 6
- 12
0
There was a similar post that provides a solution for this. Look for the answer by Jim Mischel
You can use the KeyEventArgs Class
if (e.Alt)
{
// Alt key was pressed
}

jasonc
- 213
- 1
- 7
-
Not sure about earlier versions but this is not valid in .NET 6 and above – henry-js Dec 14 '22 at 09:57