Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I want to check the key entries of number pad3 and 1 using && but its not working

My code :
C#
private void Form1KeyDown(object sender, KeyEventArgs e)
{
  if (e.KeyCode == Keys.NumPad1 && e.KeyCode == Keys.NumPad3)
  {
    MessageBox.Show("The device is not yet connected");
  }
}
Posted
Updated 18-Mar-11 6:30am
v2
Comments
jim lahey 18-Mar-11 12:18pm    
Do you want to check if NumPad1 and NumPad3 are pressed simultaneously?
Eduard Keilholz 18-Mar-11 12:31pm    
Restyled the code a little so it's more readable
Sergey Alexandrovich Kryukov 18-Mar-11 21:39pm    
Jim, when the two are pressed at the same time, the condition will be false, too.
--SA
Albin Abel 18-Mar-11 21:58pm    
It is not going to happen,I think combined key events not there for two character keys. The events occur one by one. So we have take account of two squential events. Your answer I found very useful for OP
Sergey Alexandrovich Kryukov 19-Mar-11 1:46am    
This is what you say, they go one by one, so... the OP's condition is never true.
--SA

As Jim lahey said use if (e.KeyCode == Keys.NumPad1 || e.KeyCode == Keys.NumPad3) to pass the condition either NumPad1 or NumPad3
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Mar-11 21:36pm    
OK, you fix the condition, but it is not working probably because some other control grabs input focus.
--SA
First, you need to fix the check as Albin advised. This is not enough, because when you focus any other control, the keyboard input with go there and will not reach the Form.

One simple work-around would: in the code, traverse all child controls (using Control.Controls) recursively and add the same key press event handler.

However, the hole idea of using this key press suggests you need to improve your UI style. Why not simply having a button and/or menu item. It least they would be self-describing, but for your key press your will need to document this non-standard key press. Also, you can more successfully use menu hot key. It would not require an unnatural work-around.

—SA
 
Share this answer
 
v2
Comments
Albin Abel 18-Mar-11 21:54pm    
Very good advice and useful for OP. My 5
Sergey Alexandrovich Kryukov 18-Mar-11 22:00pm    
Thank you, Albin.
--SA
I don't know if you have solved your problem yet but the only method that I know of resolving multiple keypresses reliably is by the use of the Windows API.

Specifically the GetKetstate()[^] function. How to declare it in your program[^].

Use it something like:
C#
bool np1 = (((ushort) GetKeyState(VK_NUMPAD1)) & 0xffff) != 0;
bool np3  = (((ushort) GetKeyState(VK_NUMPAD3)) & 0xffff) != 0;

if (np1 && np3)
{
  // do whatever you want to do if they are both pressed
}


Hope this helps.

[Edit - Added information]
Take a look at When KeyPressed / KeyDown just isn't enough. An adventure in GetKeyboardState.[^] which shows how to use GetKeyState() and also has a small app that you can examine.
[/Edit]
 
Share this answer
 
v4
Comments
Albin Abel 19-Mar-11 1:15am    
Yes, that generally works. Not sure about what OP wants. My 5
vishnulalr 19-Mar-11 3:30am    
thanks henry its working thanks very much
vishnulalr 19-Mar-11 4:10am    
here is a small modification
vishnulalr 19-Mar-11 4:11am    
bool np1 = (((ushort)GetKeyState(VirtualKeyStates.VK_NUMPAD1)) & 0xffff) != 0;
bool np3 = (((ushort)GetKeyState(VirtualKeyStates.VK_NUMPAD3)) & 0xffff) != 0;

if (np1 && np3)
{
MessageBox.Show("The device is not yet connected");
// do whatever you want to do if they are both pressed
}
vishnulalr 19-Mar-11 6:27am    
the above code work properly for first time after that it will not
It will not enter your if statement because e.Key only references the most recently pressed key, so it cant be equal to 2 different keys at once.

Try this instead(if i understand you correctly)
Put a label in ti your form and see the outcome..

private List<Keys> pressedKeys = new List<Keys>();  
private void Form1_KeyDown(object sender, KeyEventArgs e) 
{    
   pressedKeys.Add(e.KeyCode);      
   printPressedKeys(); 
}  
private void Form1_KeyUp(object sender, KeyEventArgs e) 
{     
   pressedKeys.Remove(e.KeyCode);      
   printPressedKeys(); 
}  
private void printPressedKeys() 
{     
   label1.Text = string.Empty;     
   foreach (var key in pressedKeys)     
   {         
      label1.Text += key.ToString() + Environment.NewLine;     
   } 
} 
 
Share this answer
 
v2
Comments
Eduard Keilholz 18-Mar-11 12:35pm    
Good call!
vishnulalr 18-Mar-11 12:46pm    
super
Albin Abel 18-Mar-11 13:18pm    
Wondering why OP put a if condition instead he just want to display whatever key pressed. Also Pressedkeys in this solution always have only one entry. What is the need for the for loop? On key down one entry is added and on key up it is removed. In the pressed keys function the list will be having only one entry and the key printPressedKeys() function not necessary, because the entry is removed before that. So why don't just set the label text in the key down event itself?
Orcun Iyigun 18-Mar-11 14:10pm    
Well yes you are right the purpose of it is just to be more clear and explanatory on the answer. since it wasnt clear enough on the question if OP wants to see if the pressed keys are simultaneous or not.
Albin Abel 18-Mar-11 14:31pm    
Right , but what I am telling no need of all these. Your code is redundant. Only
private void Form1_KeyDown(object sender, KeyEventArgs e)
{ label1.Text += key.ToString() + Environment.NewLine; } this much do what all your code doing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900