Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get keyboard status in WPF Application in Main-Method during startup

Tags:

c#

wpf

keyboard

When my application starts up in the static void Main method I want to determine if a key like Alt or Ctrl is pressed and then start the Application in some kind of Option-Mode. How can I find out if a key is pressed during startup?

I found some samples already but they all import a windows dll, something I don't want to do.

like image 863
TalkingCode Avatar asked Dec 14 '22 02:12

TalkingCode


1 Answers

Use Keyboard.IsKeyDown() static method will help you to check the state of the keys you're interested in.

if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)
       || Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt) )
{
    //Load in a special mode
}
else
{
    //Load standard mode
}
like image 99
Arsen Mkrtchyan Avatar answered Dec 16 '22 18:12

Arsen Mkrtchyan