9

When our WPF application starts, it uses Single Sign On to log in. To allow testers to simulate other users, we'd like to detect the Control button being held down on startup, and pop up a login dialog.

James L
  • 16,456
  • 10
  • 53
  • 70

4 Answers4

7

This is what I use in a WPF app to check if the control key is being held down in the constructor of the main Window. It uses System.Windows.Input.Keyboard

if ((Keyboard.Modifiers & ModifierKeys.Control) > 0)
    PromptForMarketSelection();

EDIT - corrected bug pointed out by Coincoin

BrandonAGr
  • 5,827
  • 5
  • 47
  • 72
1

A universal solution would be to p/invoke GetAsyncKeyState(VK_CONTROL), in case you can't find anything built into .NET.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

Take a look at this article it used ModifierKeys to find out what you are looking for

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
  • Link is down. Check http://web.archive.org/web/20141020154537/http://tech.pro/tutorial/760/winforms-accessing-mouse-and-keyboard-state for a mirror to the past. – Natrium May 02 '22 at 12:16
0

Check out the following link...scroll down to look at the answer by Jeff Wain.

How to detect the currently pressed key?

Community
  • 1
  • 1
Dave Ferguson
  • 1,456
  • 13
  • 17