473,326 Members | 2,076 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,326 software developers and data experts.

Key Press Event

Tim
Hi Guys,

I want to know how to tell if the shift key is held down when another
key is pressed.

I am using the following code to make sure that only numbers, backspace
and decimal are allowed in a text box. I want to use the decimal on the
key, OemDecimal.


It can tell that the key is pressed but if the user holds the shift key
down then a > is inserted into the text box. How do I tell?

Thanks

Tim

// Determine whether the keystroke is a number from the top of the keyboard.
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
// Determine whether the keystroke is a backspace.
if(e.KeyCode != Keys.Back && e.KeyCode != Keys.Decimal)
{
// A non-numerical keystroke was pressed.
// Set the flag to true.
nonNumberEntered = true;
}
}
}
Nov 17 '05 #1
4 12804
Tim,

You might want to catch the KeyDown or the KeyUp event. These will pass
an KeyEventArgs instance to you which has a Shift property indicating if the
shift key was pressed (as well as ALT and CTRL).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tim" <ti**@peopletogo.com> wrote in message
news:In******************@news20.bellglobal.com...
Hi Guys,

I want to know how to tell if the shift key is held down when another key
is pressed.

I am using the following code to make sure that only numbers, backspace
and decimal are allowed in a text box. I want to use the decimal on the
key, OemDecimal.


It can tell that the key is pressed but if the user holds the shift key
down then a > is inserted into the text box. How do I tell?

Thanks

Tim

// Determine whether the keystroke is a number from the top of the
keyboard.
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
// Determine whether the keystroke is a backspace.
if(e.KeyCode != Keys.Back && e.KeyCode != Keys.Decimal)
{
// A non-numerical keystroke was pressed.
// Set the flag to true.
nonNumberEntered = true;
}
}
}

Nov 17 '05 #2
Tim
Thanks Nicholas, that was it.

Nicholas Paldino [.NET/C# MVP] wrote:
Tim,

You might want to catch the KeyDown or the KeyUp event. These will pass
an KeyEventArgs instance to you which has a Shift property indicating if the
shift key was pressed (as well as ALT and CTRL).

Hope this helps.

Nov 17 '05 #3
Tim
Nicholas,

It only detects one key at a time. I need to detect if a key is pressed
while the shift key is held down. Any ideas?

Nicholas Paldino [.NET/C# MVP] wrote:
Tim,

You might want to catch the KeyDown or the KeyUp event. These will pass
an KeyEventArgs instance to you which has a Shift property indicating if the
shift key was pressed (as well as ALT and CTRL).

Hope this helps.

Nov 17 '05 #4
Tim,

Nicholas was on the right track...the KeyEventArgs that is passed in each
time the KeyUp or KeyDown event is raised has a property called Shift that
returns a boolean value representing whether the shift key was pressed. It
also has properties for the Control and Alt keys as well.
--
Chris Wunn
Software Engineer
Siemens Electronics Assembly Systems
Norcross, Georgia
"Tim" wrote:
Nicholas,

It only detects one key at a time. I need to detect if a key is pressed
while the shift key is held down. Any ideas?

Nicholas Paldino [.NET/C# MVP] wrote:
Tim,

You might want to catch the KeyDown or the KeyUp event. These will pass
an KeyEventArgs instance to you which has a Shift property indicating if the
shift key was pressed (as well as ALT and CTRL).

Hope this helps.

Jan 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mukesh | last post by:
Hi I have a datagrid in which i want to capture the key press event and find which row i am in. Tried with the key press event provided by datagrid, but it does not fire. What could be wrong ...
9
by: supster | last post by:
Hello, I'm trying to capture when users press the tab key. This is scrolling through different fields with tab is broken in my form. I'm not sure why, but it may have something to do with the fact...
3
by: birarai | last post by:
How do i get the 'keypress' event to capture only the 'enter' key event? The 'keypress' event captures all key press events. input.addEventListener('keypress', sendButtonPressed, true); ...
1
by: donald | last post by:
I got a ASP.net text box called textbox1 and a label called label1. When a key is press in a text box i want it to up date the label with what is in the textbox. Label1.Text = TextBox1.Text; ...
3
by: Kürþat | last post by:
Hi, Is it possible to know whether a keyboard message is generated by a phisical key press or an application. If possible, how? Thanks in advance.
6
by: salo | last post by:
Can anybody say me for writing javascript code for textbox key press event such a way that it should allow only alphabets,dot and spaces
10
by: ofiras | last post by:
Hii, I saw a lot of programs, that can be in tray (in the bottom right corner), and they can get Key Press event although they are not in focus. Is there a way to do it in C# windows application?...
3
by: fieldling | last post by:
I've got a command button on a form that I would also like to work if I press, for example, F5. (Sounds pointless having a command button I know but there you go). I've tried using the On Key...
1
by: Sendil kumar | last post by:
When I do a Ctrl-C(copy) on a text, the text was not copied. By narrowing down the problem I found that VC++ message map is not capturing the Ctrl-C key press event(Only captures the first key press,...
7
by: thomask | last post by:
Hello my friends , How to implement key press event for checking whether the value entered in the text box is integer.(ie if we press any alphabetic letter in the text box, at the key press it...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.