Kartik Kartik - 4 months ago 13
C# Question

What is the Keycode for % sign?

I am using C#.Net. I have textbox which allow only number, decimal and percentage(%) sign. I have the keycode for all number and decimal, but what is the "%" sign's keycode?

How can I check the

keydown
event for %?

Answer

Something like this:

private void yourControl_KeyDown(object sender, KeyEventArgs e)
{
   if((e.KeyCode == Keys.D5) && e.Shift)
   {
      // User pressed '%' ...
   }
}

or

private void yourControl_KeyDown(object sender, KeyEventArgs e)
{
    switch(e.KeyCode)
    {
        //... 
        case Keys.D5:
            if(e.Shift)
            {
                // Handle '%'
            }
            else
            {
                // Handle '5'
            }
            break;
        // ...
    }
}

You want to check that the key being pressed is the 5 key, and that it has been modified by pressing the shift key.