且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测是否按下了特定键?

更新时间:2023-12-03 09:55:22

您应该使用KeyPress事件而不是Change事件:

You should use KeyPress event instead of Change event:

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/

Full list of keycodes you can find here: http://www.asciitable.com/