0
private void txb_keydown(object sender, KeyEventArgs e)
{
    //check if A key was pressed
    if (e.KeyCode == Keys.A
    {
     //OK
     }
}

The above code works fine, but how can I do this for the % key?
Looked everywhere, I can check for the tilde key etc. but I can't seem to find the Keys enumeration code for the % key.
Any help will be much appreciated.

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by ddanbe
0

There is no % key, so it would be hard to find it. but what there is, would be a shift modified D5 key. So you look for a combination of those keys.

add this to your keydown event:

if (e.Modifiers == Keys.Shift && e.KeyCode == Keys.D5)
            {
                MessageBox.Show("% Key Pressed");
            }

NOTE:
for the alt or ctrl key, its not necessary to check the e.modifiers flag, but when using the shift key it is. I don't know why. I have just tried with it for a while and I ended up with this combination of event data to be the only one that worked.

But there you go, Hope this solves your problem.

0

You gave me insights! Thank you.
My keyboard on my Mac with VISTA is AZERTY, yours is probably QUERTY. (This Mac was practically a free gift from my boss so I could not refuse) I never found a way to type a ] character on it other than by copy and paste or CharMap, and to type {} I have to do weird things, but I can live with it. I seem to remember that on some QUERTY keyboards, the %key was indeed the shift D5 key. On my keyboard the %key is near the RETURN key! So your code snippet works if I press my D5key, but not if I press my %key.
Meanwhile I found out that if use the KeyPress event like this :

private void txtb_keypress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '%')
    {
        MessageBox.Show("%key");
    }
}

I get the functionality I want so I think I'm gonna stick to that.
Thanks again for your effort, I will mark this as solved.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.