27

In my KeyDown EventHandler I need to know what is the KeyCode for "," and ".".

I can't find them thats why I ask. Thanks!

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Bosak
  • 2,103
  • 4
  • 24
  • 43
  • Are you sure you want to use the `KeyDown` and not the `KeyPress` handler? – CodesInChaos Jan 18 '12 at 18:51
  • 4
    The KeyDown event handler handles key strokes, not their representation in system, what depends on regional settings. To handle charakter, use KeyPress event. – pistipanko Jan 18 '12 at 18:52

6 Answers6

32

A key and a character are not the same thing. The keyboard layout transforms between them, and that transform isn't trivial. Probably you're doing the wrong thing when using KeyDown. If you want to know which character a user entered you should use KeyPress, which gives the the already translated character.

For example Keys.Decimal is a key on the numpad that corresponds to . on the US layout, and , on the German layout. Keys.Oemcomma and OemPeriod are likely , and . belows the letters. But on other layouts that may be different.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • Keys.Deciaml is for numpad what for second decimal?? – Mohammad Faizan khan May 22 '14 at 13:27
  • 1
    There is no "second decimal". There is a key for *comma* and one for *period*, but which of them produces the decimal separator depends on the locale. I'm also quite sure that asking this question means you're already on the wrong track. You should work with characters, not keys. – CodesInChaos May 22 '14 at 13:32
9

Oemcomma and OemPeriod look like good candidates.

Look at the Keys enumeration on MSDN.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
4

I did this:

  1. Created a WinForm project with a single textbox.
  2. Added the keydown event handler.
  3. Put a break point in it
  4. Got this:

enter image description here

jgauffin
  • 99,844
  • 45
  • 235
  • 372
2

Check out the decimal value, that's your key code.

http://www.asciitable.com/

Shaun07776
  • 1,052
  • 1
  • 10
  • 16
1

Use Keys.Oemcomma and Keys.OemPeriod

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

Have you tried this "."c

A float filter:

Select Case e.KeyChar
    Case "0"c To "9"c
    Case "."c
        If .Text.Contains(".") Then
            e.Handled = True
        End If
    Case ChrW(Keys.Delete), ChrW(Keys.Back)
    Case Else
        e.Handled = True
End Select
thor
  • 21,418
  • 31
  • 87
  • 173
Champu
  • 1