vb6模拟键盘输入的代码是什么?

 我来答
匿名用户
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
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式