vb6模拟键盘输入的代码是什么?
1个回答
2013-11-30
展开全部
用下面代码可以显示键盘所有ASCII码
Private Declare Function GetWindowThreadProcessId& Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long)
Private Declare Function GetKeyboardLayout& Lib "user32" (ByVal dwLayout As Long)
Private Declare Function MapVirtualKeyEx Lib "user32" Alias "MapVirtualKeyExA" (ByVal uCode As Long, ByVal uMapType As Long, ByVal dwhkl As Long) As Long
Private Function CharFromKeyCode(k As Integer) As String
Dim s As String
Select Case k
Case vbKeyBack: s = "BackSpace"
Case vbKeyTab: s = "Tab"
Case vbKeyClear: s = "Clear"
Case vbKeyReturn: s = "Enter"
Case vbKeyShift: s = "Shift"
Case vbKeyControl: s = "Ctrl"
Case vbKeyMenu: s = "Alt"
Case vbKeyPause: s = "Pause"
Case vbKeyCapital: s = "CapsLock"
Case vbKeyEscape: s = "ESC"
Case vbKeySpace: s = "SPACEBAR"
Case vbKeyPageUp: s = "PAGE UP"
Case vbKeyPageDown: s = "PAGE DOWN"
Case vbKeyEnd: s = "END"
Case vbKeyHome: s = "HOME"
Case vbKeyLeft: s = "LEFT ARROW"
Case vbKeyUp: s = "UP ARROW"
Case vbKeyRight: s = "RIGHT ARROW"
Case vbKeyDown: s = "DOWN ARROW"
Case vbKeySelect: s = "SELECT"
Case vbKeyPrint: s = "PRINT SCREEN"
Case vbKeyExecute: s = "EXECUTE"
Case vbKeySnapshot: s = "SNAPSHOT"
Case vbKeyInsert: s = "INS"
Case vbKeyDelete: s = "DEL"
Case vbKeyHelp: s = "HELP"
Case vbKeyNumlock: s = "NUM LOCK"
Case vbKey0 To vbKey9: s = Chr$(k)
Case vbKeyA To vbKeyZ: s = Chr$(MapVirtualKeyEx(k, 2, GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow, 0))))
Case vbKeyF1 To vbKeyF16: s = "F" & CStr(k - 111)
Case vbKeyNumpad0 To vbKeyNumpad9: s = "Numpad " & CStr(k - 95)
Case vbKeyMultiply: s = "Numpad {*}"
Case vbKeyAdd: s = "Numpad {+}"
Case vbKeySeparator: s = "Numpad {ENTER}"
Case vbKeySubtract: s = "Numpad {-}"
Case vbKeyDecimal: s = "Numpad {.}"
Case vbKeyDivide: s = "Numpad {/}"
Case Else
s = Chr$(MapVirtualKeyEx(k, 2, GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow, 0))))
End Select
CharFromKeyCode = s
End Function
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim s As String
s = "KeyCode " & KeyCode
s = s + " " & CharFromKeyCode(KeyCode)
If Shift = vbShiftMask Then s = s & " + Shift "
If Shift = vbCtrlMask Then s = s & " + Ctrl "
If Shift = vbAltMask Then s = s & " + Alt "
Print vbCrLf & s & " Down "
End Sub
Private Declare Function GetWindowThreadProcessId& Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long)
Private Declare Function GetKeyboardLayout& Lib "user32" (ByVal dwLayout As Long)
Private Declare Function MapVirtualKeyEx Lib "user32" Alias "MapVirtualKeyExA" (ByVal uCode As Long, ByVal uMapType As Long, ByVal dwhkl As Long) As Long
Private Function CharFromKeyCode(k As Integer) As String
Dim s As String
Select Case k
Case vbKeyBack: s = "BackSpace"
Case vbKeyTab: s = "Tab"
Case vbKeyClear: s = "Clear"
Case vbKeyReturn: s = "Enter"
Case vbKeyShift: s = "Shift"
Case vbKeyControl: s = "Ctrl"
Case vbKeyMenu: s = "Alt"
Case vbKeyPause: s = "Pause"
Case vbKeyCapital: s = "CapsLock"
Case vbKeyEscape: s = "ESC"
Case vbKeySpace: s = "SPACEBAR"
Case vbKeyPageUp: s = "PAGE UP"
Case vbKeyPageDown: s = "PAGE DOWN"
Case vbKeyEnd: s = "END"
Case vbKeyHome: s = "HOME"
Case vbKeyLeft: s = "LEFT ARROW"
Case vbKeyUp: s = "UP ARROW"
Case vbKeyRight: s = "RIGHT ARROW"
Case vbKeyDown: s = "DOWN ARROW"
Case vbKeySelect: s = "SELECT"
Case vbKeyPrint: s = "PRINT SCREEN"
Case vbKeyExecute: s = "EXECUTE"
Case vbKeySnapshot: s = "SNAPSHOT"
Case vbKeyInsert: s = "INS"
Case vbKeyDelete: s = "DEL"
Case vbKeyHelp: s = "HELP"
Case vbKeyNumlock: s = "NUM LOCK"
Case vbKey0 To vbKey9: s = Chr$(k)
Case vbKeyA To vbKeyZ: s = Chr$(MapVirtualKeyEx(k, 2, GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow, 0))))
Case vbKeyF1 To vbKeyF16: s = "F" & CStr(k - 111)
Case vbKeyNumpad0 To vbKeyNumpad9: s = "Numpad " & CStr(k - 95)
Case vbKeyMultiply: s = "Numpad {*}"
Case vbKeyAdd: s = "Numpad {+}"
Case vbKeySeparator: s = "Numpad {ENTER}"
Case vbKeySubtract: s = "Numpad {-}"
Case vbKeyDecimal: s = "Numpad {.}"
Case vbKeyDivide: s = "Numpad {/}"
Case Else
s = Chr$(MapVirtualKeyEx(k, 2, GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow, 0))))
End Select
CharFromKeyCode = s
End Function
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim s As String
s = "KeyCode " & KeyCode
s = s + " " & CharFromKeyCode(KeyCode)
If Shift = vbShiftMask Then s = s & " + Shift "
If Shift = vbCtrlMask Then s = s & " + Ctrl "
If Shift = vbAltMask Then s = s & " + Alt "
Print vbCrLf & s & " Down "
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询