主题:如何在KeyPress事件中监控用户输入了大写字母?
设计了一个简单的键盘练习程序,代码如下:
Dim m As Integer
Const strs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Private Sub Form_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) = Label1.Caption Then m = m + 1
End Sub
Private Sub Form_Load()
Label1.AutoSize = True
Label1.FontSize = 16
Label1.Visible = False
Timer1.Interval = 1000
Timer2.Interval = 60000
End Sub
Private Sub Timer1_Timer()
Randomize
Label1.Visible = True
Label1.Left = Int(Width * Rnd)
Label1.Top = Int(Height * Rnd)
j = CInt(Rnd * Len(strs)) + 1
Label1.Caption = Mid(strs, j, 1)
End Sub
Private Sub Timer2_Timer()
MsgBox "你输入正确了" + Str(m) + "个字母"
Timer1.Enabled = False
End
End Sub
运行的时候倒是在窗体上大写和小写字母都能随机地显示出来,可是仔细一想,不对呵,怎么在KeyPress事件中监控用户输入了大写字母呢?当用户按下shfit键进行转换时,VB都将会认为是仅仅按下了shfit键,而不认为是在进行大小写转换呢!这可怎么好?[em10]
Dim m As Integer
Const strs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Private Sub Form_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) = Label1.Caption Then m = m + 1
End Sub
Private Sub Form_Load()
Label1.AutoSize = True
Label1.FontSize = 16
Label1.Visible = False
Timer1.Interval = 1000
Timer2.Interval = 60000
End Sub
Private Sub Timer1_Timer()
Randomize
Label1.Visible = True
Label1.Left = Int(Width * Rnd)
Label1.Top = Int(Height * Rnd)
j = CInt(Rnd * Len(strs)) + 1
Label1.Caption = Mid(strs, j, 1)
End Sub
Private Sub Timer2_Timer()
MsgBox "你输入正确了" + Str(m) + "个字母"
Timer1.Enabled = False
End
End Sub
运行的时候倒是在窗体上大写和小写字母都能随机地显示出来,可是仔细一想,不对呵,怎么在KeyPress事件中监控用户输入了大写字母呢?当用户按下shfit键进行转换时,VB都将会认为是仅仅按下了shfit键,而不认为是在进行大小写转换呢!这可怎么好?[em10]