-1

I know about KeyDown, KeyPress and KeyUp events, but i don't know how to detect the key that i pressed.

Is there a way to captute the value of key pressed?

For example: I press 'W' and some string gets the value of 'W'

Grokify
  • 15,092
  • 6
  • 60
  • 81
Tadumc421
  • 149
  • 2
  • 4
  • 12
  • Can you give a bit more context please? Do you need to know in a specific control, or for the app/form as a whole? – JayV Jun 11 '18 at 09:33
  • Could you give the code where you handle those events ? You may find useful information here : https://stackoverflow.com/questions/2752424/detecting-enter-keypress-on-vb-net – yunandtidus Jun 11 '18 at 09:37
  • Its simple, i need to configure A W S D keys to move a picture around in my program. First i'd like to know how to get the value of the key pressed and then i will be able to code my program using if statments. For example (just to demonstrate): If e.key = pressed Then 'execute code' – Tadumc421 Jun 11 '18 at 10:01
  • @Tadumc421 Take a look at https://stackoverflow.com/a/50546716/9365244, I think it will answer your question – JayV Jun 11 '18 at 10:14
  • I aleady got what i was looking for, ty anyways! – Tadumc421 Jun 11 '18 at 10:42

2 Answers2

5

This tells you what key was pressed:

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress 

        MsgBox(e.KeyChar) 

End Sub 

You should be able to rework it to suit your needs.

Edit:

If you need to detect non-character key presses such as F1 etc. you can't use the keypress event as it is not raised by non-character keys. Then you will have to use the KeyUp or KeyDown event. I prefer the KeyUp event for one simple reason, the KeyDown event fires as long as the key it kept down, so keep that in mind.

Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp

    MessageBox.Show(e.KeyValue)

End Sub

This will return the int number of the key that was pressed, but it does not distinguish between upper and lower case.

You should be able to detect these with something like this:

If Control.ModifierKeys = Keys.Shift Or Control.ModifierKeys = Keys.Control Then
        MsgBox("SHIFT or CTRL key pressed with " & e.KeyValue & ".")

    Else

        MessageBox.Show(e.KeyValue)

    End If

Example Usage: To see if the enter key for example was pressed:

Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp

    IF e.KeyValue = 13 Then

       MessageBox.Show("Enter Key Was Pressed")

    End If

End Sub

For a list of what value represents what key see:

Dec      Char                       Dec Char      Dec Char     Dec Char
--------------                      ---------     ---------     ----------
 0  NUL (null)                      32  SPACE     64  @         96  `
 1  SOH (start of heading)          33  !         65  A         97  a
 2  STX (start of text)             34  "         66  B         98  b
 3  ETX (end of text)               35  #         67  C         99  c
 4  EOT (end of transmission)       36  $         68  D        100  d
 5  ENQ (enquiry)                   37  %         69  E        101  e
 6  ACK (acknowledge)               38  &         70  F        102  f
 7  BEL (bell)                      39  '         71  G        103  g
 8  BS  (backspace)                 40  (         72  H        104  h
 9  TAB (horizontal tab)            41  )         73  I        105  i
10  LF  (NL line feed, new line)    42  *         74  J        106  j
11  VT  (vertical tab)              43  +         75  K        107  k
12  FF  (NP form feed, new page)    44  ,         76  L        108  l
13  CR  (carriage return)           45  -         77  M        109  m
14  SO  (shift out)                 46  .         78  N        110  n
15  SI  (shift in)                  47  /         79  O        111  o
16  DLE (data link escape)          48  0         80  P        112  p
17  DC1 (device control 1)          49  1         81  Q        113  q
18  DC2 (device control 2)          50  2         82  R        114  r
19  DC3 (device control 3)          51  3         83  S        115  s
20  DC4 (device control 4)          52  4         84  T        116  t
21  NAK (negative acknowledge)      53  5         85  U        117  u
22  SYN (synchronous idle)          54  6         86  V        118  v
23  ETB (end of trans. block)       55  7         87  W        119  w
24  CAN (cancel)                    56  8         88  X        120  x
25  EM  (end of medium)             57  9         89  Y        121  y
26  SUB (substitute)                58  :         90  Z        122  z
27  ESC (escape)                    59  ;         91  [        123  {
28  FS  (file separator)            60  <         92  \        124  |
29  GS  (group separator)           61  =         93  ]        125  }
30  RS  (record separator)          62  >         94  ^        126  ~
31  US  (unit separator)            63  ?         95  _        127  DEL

ASCII table was found at https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html

1

Use the Char.ConvertFromUtf32 function. So if you are using a text box and are looking at the keydown event for instance:

Private Sub txtData_Keydown(sender As Object, e As KeyEventArgs) handles txtData.keydown
    if e.keyvalue>64 and e.keyvalue <91 then 'if its a letter a-z A-Z keydown always gives uppercase A to Z
       Dim eChr As String = Char.ConvertFromUtf32(If(e.Shift, e.KeyValue, e.KeyValue + 32)) 
    endif

where e.shift indicates whether the shift key was pressed (uppercase vs lowercase). eChr will now be a string containing the '(L)letter' you pressed. With Keydown, you have to be careful to trap only the keys you are interested in. Pressing the shift key, arrow keys, functions etc will also trigger this event.

Georg
  • 404
  • 5
  • 7