简体   繁体   中英

How to check if a key was pressed in C#? (.NET 6)

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!

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
}

KeyEventArgs

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM