6

I use these following codes to work with numpad keys.

if (e.KeyCode == Keys.NumPad0 || e.KeyCode == Keys.D0)
{
   MessageBox.Show("You have pressed numpad0");
}
if (e.KeyCode == Keys.NumPad1 || e.KeyCode == Keys.D1)
{
   MessageBox.Show("You have pressed numpad1");
}

And also for the other numpad keys. But I want to know how I can to this for "+" , "*" , "/" , " -" , " . " which located next to the numpad keys.

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47
  • Oops, sorry about the close vote, I figured out the question after voting. – zmbq May 16 '12 at 21:04
  • Can you write an app with a text box and a key pressed event, and print out the value of e.KeyCode? That will tell you exactly the values you need. – carlosfigueira May 16 '12 at 21:18

4 Answers4

14

Check out the entire Keys enum . You have Keys.Mutiply, Keys.Add, and so forth.

Note that Keys.D0 is not the numpad 0, it's the non-numpad 0.

zmbq
  • 38,013
  • 14
  • 101
  • 171
  • Further to that, the only numpad key that isn't represented is the `Enter` key. See here for how to get around that: http://stackoverflow.com/questions/5989380/sending-specific-keys-on-the-numpad-like-or-enter-simulating-a-keypress – yamen May 16 '12 at 21:07
  • - is Keys.Minus. I don't know what the dot is. Perhaps Keys.Separator? – zmbq May 16 '12 at 21:09
  • Keys.OemMinus but not working with - which located next to the numpad. we should press Shift + DASH for working. Keys.Sperator not working for "." – Ali Vojdanian May 16 '12 at 21:13
  • 1
    `Keys.OemMinus` is the 'regular' minus key near backspace. `Keys.Subtract` is the numpad minus key. – Dave Cousineau Dec 13 '17 at 21:30
  • 1
    `Key.Decimal` is the "." on the Numeric Pad. – Étienne Laneville May 30 '20 at 17:54
  • I think he used `D0` too because he was also capturing the standard number keysvand not just the number pad keys. – Andrew Truckle May 25 '21 at 20:51
6

For "+" , "*" , "/" , we can use KeyDown event and for "-" , "." we can use KeyPress event.

Here are the codes :

private void button1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Add)
        {
            MessageBox.Show("You have Pressed '+'");
        }
        else if (e.KeyCode == Keys.Divide)
        {
            MessageBox.Show("You have Pressed '/'");
        }
        else if (e.KeyCode == Keys.Multiply)
        {
            MessageBox.Show("You have Pressed '*'");
        }
    }
private void button1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == '.')
        {
            MessageBox.Show("You have pressed '.'");
        }
        else if (e.KeyChar == '-')
        {
            MessageBox.Show("You have pressed '-'");
        }
    }
Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47
0

I used switch to make mine work.

I am making a calculator and created a KeyDown even on the target textbox. I then used:

switch (e.KeyCode)
{
    case Keys.NumPad1:
        tbxDisplay.Text = tbxDisplay.Text + "1";
    break;
    case Keys.NumPad2:
        tbxDisplay.Text = tbxDisplay.Text + "2";
    break;
    case Keys.NumPad3:
        tbxDisplay.Text = tbxDisplay.Text + "3";
    break;
}

etc.

The other thing to consider is that if the user then clicked an on screen button, the focus would be lost from the textbox and the key entries would no longer work. But thats easy fixed with a .focus() on the buttons.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
0

There is a simple way to learn all the keycodes without checking the manuals.

Create a form and then go to KeyDown event of the form then add this

private void Form1_KeyDown(object sender, KeyEventArgs e)
   {
      MessageBox.Show(e.KeyCode.ToString());
   }

Then you will get the name of the any key you have pressed on your keyboard