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.
Asked
Active
Viewed 3,569 times
4 Answers
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
-
3Might be better with (Keyboard.Modifiers & ModifierKeys.Control) != 0 – Coincoin Jun 13 '11 at 19:40
-
1For .NET 3.5+ if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control)) PromptForMarketSelection(); – Robear May 27 '16 at 17:53
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.

Community
- 1
- 1

Dave Ferguson
- 1,456
- 13
- 17