如何检测 VB.NET 中按下的键

How can I detect the pressed key in VB.NET

我知道 KeyDown、KeyPress 和 KeyUp 事件,但我不知道如何检测我按下的键。

有没有办法捕获按键的值?

例如:我按 'W' 一些字符串得到 'W'

的值

这会告诉您按下了哪个键:

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

        MsgBox(e.KeyChar) 

End Sub 

您应该能够对其进行修改以满足您的需要。

编辑:

如果您需要检测非字符按键,例如 F1 等,您不能使用 keypress 事件,因为它不是由非字符键引发的。然后您将不得不使用 KeyUp 或 KeyDown 事件。出于一个简单的原因,我更喜欢 KeyUp 事件,KeyDown 事件会在按下该键时触发,因此请记住这一点。

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

    MessageBox.Show(e.KeyValue)

End Sub

这将return按下的键的int号,但不区分大小写。

你应该能够用这样的东西检测到这些:

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

示例用法:查看是否按下了回车键:

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

有关什么值代表什么键的列表,请参阅:

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 被发现于 https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html

使用 Char.ConvertFromUtf32 函数。因此,如果您正在使用文本框并正在查看 keydown 事件,例如:

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

其中 e.shift 表示是否按下了 shift 键(大写与小写)。 eChr 现在将是一个包含您按下的“(L) 字母”的字符串。 使用 Keydown 时,您必须小心地只捕获您感兴趣的键。按 shift 键、箭头键、功能等也会触发此事件。