如何检测是否按下了特定键? [英] How to detect if a specific key was pressed?

查看:129
本文介绍了如何检测是否按下了特定键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以检测是否按下了特定键(例如退格键).这就是我要拍摄的内容:

I'm wondering is there a way to detect if a specific key (like backspace) was pressed. This is what I'm shooting for:

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事件:

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/

这篇关于如何检测是否按下了特定键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆