Solved

Detect keys down on application startup?

Posted on 2010-11-09
11
357 Views
Last Modified: 2012-05-10
Hi,

I have written in C#. I would like the application to start in a "support mode" if the user is holding down Ctrl+Shift+S when the application starts.

As I need to detect the "support mode" keys before the application starts (i.e. the main form loads), I can't do it on the keydown event of the main form. This is because some of the things on the main form get initialised differently in support mode.

I can't find a way of getting the current keys pressed without responding to events.

How can I do this?
0
Comment
Question by:JimR123b
  • 6
  • 5
11 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34091979
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34091988
In there, you could set up a singleton GlobalConfig class that contains members that you want to init.
Your main form can then also reference the singleton, e.g. GlobalConfig.GetSingleton.somesetting
0
 

Author Comment

by:JimR123b
ID: 34091998
How do I get the current state of the keys pressed?

0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 58

Accepted Solution

by:
cyberkiwi earned 250 total points
ID: 34092057
http://www.switchonthecode.com/tutorials/winforms-accessing-mouse-and-keyboard-state

Ctrl-Shift-S :

if((Control.ModifierKeys == (Keys.Control | Keys.Shift)) && Keyboard.IsKeyDown(Keys.S))
{
  //Do stuff here
}
0
 

Author Comment

by:JimR123b
ID: 34092119
I perhaps should have said, I'm using .Net Framework 3.5.

It appears that the Keyboard class is .Net 4?... http://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.iskeydown.aspx

That's the kind of thing I'm looking for, but for .Net 3.5.

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34092132
Please see the above link.
The class is created in the link, for pre .Net 4.
0
 

Author Comment

by:JimR123b
ID: 34092134
...Also, I can't use Control.ModifierKeys because I don't have a handle on a control yet, as it is needed before the main form is created.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34092144
Control is a static reference to the Control class, not an instance of a control.
0
 

Author Comment

by:JimR123b
ID: 34092185
Ah, I see. I did try code completion on "Control." to see what came up, and it only came up with a few things, none of which were "ModifierKeys". I've tried it in another class now, and I see "ModifierKeys" :)

...but how do I get the state of Keys.S without the Keyboard class? Is there something similar in Control?

0
 

Author Comment

by:JimR123b
ID: 34092212
Sorry, didn't see the definition of the class Keyboard in http://www.switchonthecode.com/tutorials/winforms-accessing-mouse-and-keyboard-state

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34092215
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question