如何检测是否按下了特定的键?
How to detect if a specific key was pressed?
我想知道是否有一种方法可以检测是否按下了特定键(如退格键)。这就是我的目标:
Private Sub SomeTextBox_Change()
If len(Me.SomeTextBox.Value) = 3 and KEYPRESSED is NOT BACKSPACE Then
<.......Code Here>
Else
<.......Code Here>
End if
End Sub
您应该使用 KeyPress
事件而不是 Change
事件:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Len(Me.SomeTextBox.Value) = 3 And KeyAscii <> 8 Then 'Backspace has keycode = 8.
<.......Code Here>
Else
<.......Code Here>
End If
End Sub
您可以在此处找到完整的键码列表:http://www.asciitable.com/
此示例将 "InsertProc" 分配给键序列 CTRL+PLUS SIGN 并将 "SpecialPrintProc" 分配给键序列 SHIFT+CTRL+RIGHT ARROW。
Application.OnKey "^{+}", "InsertProc"
Application.OnKey "+^{RIGHT}","SpecialPrintProc"
有关更多示例和信息,请继续:https://msdn.microsoft.com/en-us/library/office/aa195807%28v=office.11%29.aspx
您应该改用 KeyPress
事件:
Private Sub SomeTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Len(Me.SomeTextBox.Value) = 3 And KeyAscii <> vbKeyBack Then
'<.......Code Here>
Else
'<.......Code Here>
End If
End Sub
并且您可以使用KeyAscii = 0
取消输入的密钥!
在此处查找所有 Ascii 值的列表 http://www.asciitable.com/
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode.Value = vbKeyF1 Then
MsgBox "F1 is pressed"
End If
End Sub
我想知道是否有一种方法可以检测是否按下了特定键(如退格键)。这就是我的目标:
Private Sub SomeTextBox_Change()
If len(Me.SomeTextBox.Value) = 3 and KEYPRESSED is NOT BACKSPACE Then
<.......Code Here>
Else
<.......Code Here>
End if
End Sub
您应该使用 KeyPress
事件而不是 Change
事件:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Len(Me.SomeTextBox.Value) = 3 And KeyAscii <> 8 Then 'Backspace has keycode = 8.
<.......Code Here>
Else
<.......Code Here>
End If
End Sub
您可以在此处找到完整的键码列表:http://www.asciitable.com/
此示例将 "InsertProc" 分配给键序列 CTRL+PLUS SIGN 并将 "SpecialPrintProc" 分配给键序列 SHIFT+CTRL+RIGHT ARROW。
Application.OnKey "^{+}", "InsertProc"
Application.OnKey "+^{RIGHT}","SpecialPrintProc"
有关更多示例和信息,请继续:https://msdn.microsoft.com/en-us/library/office/aa195807%28v=office.11%29.aspx
您应该改用 KeyPress
事件:
Private Sub SomeTextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If Len(Me.SomeTextBox.Value) = 3 And KeyAscii <> vbKeyBack Then
'<.......Code Here>
Else
'<.......Code Here>
End If
End Sub
并且您可以使用KeyAscii = 0
取消输入的密钥!
在此处查找所有 Ascii 值的列表 http://www.asciitable.com/
Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode.Value = vbKeyF1 Then
MsgBox "F1 is pressed"
End If
End Sub