- 积分
- 2399
- 明经币
- 个
- 注册时间
- 2014-1-22
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2016-6-9 20:22:44
|
显示全部楼层
在你的代码中加入循环检测按键
用法
DO
IF chkKeyDown( "Esc") THEN
DEBUG.PRINT "按下了ESC键"
EXIT DO
ENDIF
LOOP
下面是模块中的代码
Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Private Declare Function GetKeyNameText Lib "user32" Alias "GetKeyNameTextA" (ByVal lParam As Long, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Dim arrKeyName(255) As String, bReadKeyNameFlag As Boolean
Public Function chkKeyDown(ByVal sKeyName As String) As Boolean
Dim r As Long
Dim Key(0 To 255) As Byte
Dim KeyCode As Long
If Not bReadKeyNameFlag Then Call LoadKeyName '如果没有载入键名,载入键名
KeyCode = KeyNameToCode(sKeyName)
r = GetKeyboardState(Key(0)) '获取当前按键状态
If Key(KeyCode) And &H80 Then '如果指定键按下
chkKeyDown = True
End If
End Function
Private Sub LoadKeyName()
Dim iCode As Long
Dim sTxt As String
For iCode = 1 To 127
sTxt = String(128, Chr(0))
GetKeyNameText MapVirtualKey(iCode, 0) * &H10000, sTxt, 128
If InStr(sTxt, Chr(0)) > 0 Then
sTxt = Left(sTxt, InStr(sTxt, Chr(0)) - 1)
arrKeyName(iCode) = sTxt
End If
Next
For iCode = 1 To 127
Debug.Print iCode, arrKeyName(iCode), Len(arrKeyName(iCode))
Next
bReadKeyNameFlag = True
End Sub
Private Function KeyNameToCode(ByVal sKeyName As String) As Long
Dim iCode As Long
For iCode = 1 To 127
If arrKeyName(iCode) = sKeyName Then
KeyNameToCode = iCode
Exit For
End If
Next
End Function
|
|