473,395 Members | 2,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 13821
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kiteman - Canada | last post by:
Is there an event triggered when a form is dragged by its titlebar? I have two forms on screen - the main form is draggable and I need the secondary form to automatically reposition itself...
5
by: Alex Stevens | last post by:
Hi All, I'm writing a launcher program which checks for updates and copies them down to the client. However, I would like the user to be able to modify the settings for the launcher. As this...
1
by: Kejpa | last post by:
Hi! How can I detect if Shift (and/or Ctrl and/or Alt) is pressed in the click event of a ToolBar button? TIA /Kejpa
5
by: Boni | last post by:
Dear all, How do I get information if Shift is pressed in a mouse wheel handler. Thanks, Boni Sub MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) 'mouse whill...
2
by: sytemper | last post by:
I write a keydown KeyDown event to catch if tab is press or tab+shift is press.And i already override the IsInputKey method. But noe i only can get the tab but no shift+tab. When shift+tab is...
1
by: Frank Rizzo | last post by:
I'd like to detect a condition when a user presses a predefined keyboard combination. For instance, left shift + right control + 5 on the number pad. I've tried the KeyDown event on the form. ...
4
by: Johnny J. | last post by:
How do you check if the Control, Shift or Alt key is pressed at any given time (not necessarily in a keyevent)? Cheers, Johnny J.
4
by: hui11 | last post by:
Hi, According to the doc at mozilla, http://developer.mozilla.org/en/docs/DOM:event.charCode, the charCode takes shift into consideration when pressed. I found that to be true for other cases...
2
by: bobh | last post by:
Hi All, In AccessXP I have a form, it has a form header, detail and footer sections. When done entering text in a txtbox(bxReqst) in the footer the user wants to press the Tab key and have the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.