Get keyboard status in WPF application in Main-Method at startup

When my application starts in the static void Main method, I want to determine if the Alt or Ctrl key is pressed, and then run the application in some Option mode. How do I know if a key is pressed during startup?

I already found some samples, but they all import windows dll, which I do not want to do.

+3
source share
3 answers

Using the static Keyboard.IsKeyDown () method will help you check the status of the keys that interest you.

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 } 
+7
source

If you really need to do this in the main method, you will have to use

 [DllImport("user32.dll")] public static extern int GetKeyboardState(byte [] lpKeyState); 

docs here

because the static keyboard members that you usually use do not work at this point:

Keyboard.Modifiers Keyboard.IsKeyDown

But you could try to connect to the Application.Startup event and test the keyboard there.

+2
source

You might want to check out this question [SO]

I had the same problem and I ended up checking keyboard modifiers in the Loaded event ...

0
source

All Articles