VB 判断某键的状态是弹起或按下
大家好.请教个问题.VB中如何判断某键的状态是被按下或者弹起了.
应该不是在窗体的KeyDown事件中.是要全局判断的.
例如 这样:
使用了 TIMER 控件
if key F1 KeyDown then ....
[解决办法]
要完成这个功能你可以用一个API函数,GetKeyState()
打个比方说你要判断“A”这个键的状态用VB的代码可以这样写
dim result as long
dim keycodenu as long
keycodenu =ascii(A)
result =GetKeyState(keycodenu)
if result =1 then
msgbox"‘A键处于被按下状态’",vbokonly,"The state of the specified key"
else
msgbox"'A未被按下'",vbokonly,"The state of the specified key"
end if
不知道对不对希望对你有帮助
[解决办法]
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Long
Private Sub Form_Load()
Timer1.Interval = 10: Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyF1) Then Me.Caption = "F1 被按下了"
If GetAsyncKeyState(vbKeyF2) Then Me.Caption = "F2 被按下了"
If GetAsyncKeyState(vbKeyF3) Then Me.Caption = "F3 被按下了"
End Sub