471,319 Members | 2,527 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Application Starting - detecting Shift Key pressed

Hello,

I would like to cancel an action if the user holds the shift key while
the application is starting-up.
Would you know what is the best way to do so please?
Thanks,
Michael

--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/

Jan 23 '06 #1
5 13621
I'm assuming the main question is about the [Shift] key (not cancelling an
action):

Just test the Control modifiers:

if ((System.Windows.Forms.Control.ModifierKeys & Keys.Shift) == Keys.Shift)
{
System.Diagnostics.Debug.WriteLine("Shift down");
}

This appears to work even for console applications (if you adda a
reference), which is quite nice...

Marc
Jan 23 '06 #2
"Michael Moreno" <mi*********************@free.fr> wrote in
news:mn***********************@free.fr:
I would like to cancel an action if the user holds the shift key while
the application is starting-up.
Would you know what is the best way to do so please?


If you do it from within the Form class, you can use the 'ModifierKeys'.
If you want to do it from Main(...) or anywhere else outside of the Form,
then you can use PInvoke:

[DllImport("user32.dll")]
static extern short GetKeyState(VirtualKeyStates nVirtKey);

int ks = GetKeyState(VirtualKeyStates.VK_SHIFT);
if ((ks & 0x80) != 0)
{
// SHIFT KEY is pressed
}

BTW the VirtualKeyStates is an enum defined at pinvoke.net:

http://pinvoke.net/default.aspx/user32.GetKeyState

But if you wanted you could just set the PInvoke signature to use an int
and pass 0x10.

-mdb
Jan 23 '06 #3
See my previous post - you don't actually need the PInvoke step, regardless
of whether you are working with a Form subclass.

Marc
Jan 23 '06 #4
Great!!!
Thank you very much

--
Michael
----
http://michael.moreno.free.fr/
http://port.cogolin.free.fr/

Jan 23 '06 #5
"Marc Gravell" <mg******@rm.com> wrote in
news:eR*************@TK2MSFTNGP15.phx.gbl:
See my previous post - you don't actually need the PInvoke step,
regardless of whether you are working with a Form subclass.


Yeah I saw that... that's a good point - thanks for the tip.

-mdb
Jan 23 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Kiteman - Canada | last post: by
5 posts views Thread by Boni | last post: by
2 posts views Thread by sytemper | last post: by
1 post views Thread by Frank Rizzo | last post: by
4 posts views Thread by Johnny J. | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.