注册 登录
编程论坛 VB6论坛

身份证输入框的事件,用keyUp好还是keyPress好?

不懂才问 发布于 2021-05-08 07:53, 1814 次点击
Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 8 Then Exit Sub
    If KeyAscii = 13 Then Exit Sub
    If KeyAscii = 88 Then Exit Sub
    If KeyAscii = 120 Then Exit Sub
    If KeyAscii > 57 Or KeyAscii < 48 Then KeyAscii = 0
End Sub

或者

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 8 or KeyAscii = 13 or KeyAscii = 88 or KeyAscii = 120 Then Exit Sub
    If KeyAscii > 57 Or KeyAscii < 48 Then KeyAscii = 0
End Sub

身份证输入框的事件,用keyUp好还是keyPress好?
3 回复
#2
apull2021-05-08 08:43

keydown:当用户按下键盘上的任意键时触发,如果按住不放的话,会重复触发此事件;
keypress:当用户按下键盘上的字符键时触发,如果按住不让的话,会重复触发此事件;
keyup:当用户释放键盘上的字符键时触发。
这三个的事件顺序是,keydown,keypress,keyup
keypress读取的是ASCII码(数字和字符并且区分大小写,不能读取控制键如:ctrl)

所以只是输入身份证号的话用keypress
#3
不懂才问2021-05-09 14:32
回复 2楼 apull
解释的非常详细,一下就懂了。非常感谢
#4
luyigoog2021-05-19 15:53
进来看看,学习一下。
1