views:

201

answers:

3
+1  Q: 

Keycode for @ char

could someone please tell me the identifier for the @ key?

for example keys.Escape is for the ESC key. i would like to know what it is for @

thank you.

+2  A: 

On my keyboard it is Keys.D2 because @ is the same key as digit 2.

However, if you handle KeyPress event you get KeyPressEventArgs in the argument that has KeyPressEventArgs.KeyChar property. And this property contains a character rather than keycode.

yu_sha
that didnt work. sorry.say s 2 not @
iEisenhower
i mean keys.D2 did not work. trying the next now.
iEisenhower
Well, I guess you could just do MessageBox.Show(keycode.ToString()) and find what it is.
yu_sha
how do you do a keycode? i only get keychar!
iEisenhower
Keychar is a character. If @ was pressed it's going to be character '@' and you don't need anything else.KeyDown and KeyUp give you keys. KeyPress gives you character.
yu_sha
+4  A: 

The KeyCode is what you get in KeyDown and KeyUp events. Since this is a shifted character, it depends on the keyboard layout. As far as I know, most keyboards have it above the digit 2, and that means checcking for e.Shift && e.KeyCode == Keys.D2 (WinForms). But on some most international keyboards, this will not work.

But note that handling the KeyPress event is much more reliable: e.KeyChar == '@'

Edit: I took a quick look at this page, and most international keyboards have the " over the 2 key and use Alt-something to get a @.

Henk Holterman
+1  A: 

You should not use the KeyDown event to recognize typing keys like @. The translation from virtual key code (KeyEventArgs.KeyData) to a typing key is dependent on the keyboard layout. Which is probably different in the UK from the one in the USA, you've got a pound to squeeze in somewhere. And surely different on a keyboard in a far East location.

Use the KeyPressed event instead.

Hans Passant