VB如何在窗体中显示按下键盘按键的字母
5个回答
推荐于2018-03-08 · 知道合伙人软件行家
yfcp
知道合伙人软件行家
向TA提问 私信TA
知道合伙人软件行家
采纳数:1748
获赞数:5545
有多年网站建设相关工作经验。熟悉ASP、ASP.net、VB、JavaScript、HTML等语言和CSS、Ajax等相关技术。
向TA提问 私信TA
关注
展开全部
VB在窗体中显示按下键盘按键的字母可以通过Form_KeyDown获取按键的KeyCode,然后再把KeyCode转换为相应的键的名字。
代码如下:
Private Sub Form_Load()
Me.KeyPreview = True '使窗体可以获取键盘事件
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Print KeyCodeToStr(KeyCode) '获取按键信息
End Sub
Public Function KeyCodeToStr(vbKeycode As Integer) '按键的KeyCode转为相应键盘键
If vbKeycode > 47 And vbKeycode < 91 Then
KeyCodeToStr = Chr(vbKeycode)
Exit Function
ElseIf vbKeycode > 111 And vbKeycode < 124 Then
KeyCodeToStr = "F" & vbKeycode - 111
Exit Function
End If
Select Case vbKeycode
Case 8: KeyCodeToStr = "Back"
Case 9: KeyCodeToStr = "Tab"
Case 12: KeyCodeToStr = "Clear"
Case 13: KeyCodeToStr = "Enter"
Case 16: KeyCodeToStr = "Shift"
Case 17: KeyCodeToStr = "Ctrl"
Case 18: KeyCodeToStr = "Alt"
Case 19
KeyCodeToStr = "Pause"
Case 20
KeyCodeToStr = "Caps Lock"
Case 27
KeyCodeToStr = "Esc"
Case 32
KeyCodeToStr = "Space"
Case 33
KeyCodeToStr = "Page Up"
Case 34
KeyCodeToStr = "Page Down"
Case 35
KeyCodeToStr = "End"
Case 36
KeyCodeToStr = "Home"
Case 41
KeyCodeToStr = "Select"
Case 42
KeyCodeToStr = "Print Screen"
Case 43
KeyCodeToStr = "Execute"
Case 44
KeyCodeToStr = "SnapShot"
Case 45
KeyCodeToStr = "Insert"
Case 46
KeyCodeToStr = "Delete"
Case 47
KeyCodeToStr = "Help"
Case 144
KeyCodeToStr = "Num Lock"
Case 189
KeyCodeToStr = "-_"
Case 187
KeyCodeToStr = "=+"
Case 255
KeyCodeToStr = "Unknown"
Case 192
KeyCodeToStr = "`~"
Case 37
KeyCodeToStr = "Left Arrow"
Case 38: KeyCodeToStr = "Up Arrow"
Case 39
KeyCodeToStr = "Right Arrow"
Case 40
KeyCodeToStr = "Dowm Arrow"
Case 219
KeyCodeToStr = "[{"
Case 221
KeyCodeToStr = "]}"
Case 186
KeyCodeToStr = ";:"
Case 222
KeyCodeToStr = "'"""
Case 220
KeyCodeToStr = "\|"
Case 188
KeyCodeToStr = ",<"
Case 190
KeyCodeToStr = ".>"
Case 191
KeyCodeToStr = "/?"
Case 193
KeyCodeToStr = "\"
Case Else
KeyCodeToStr = "Unknown"
End Select
End Function
展开全部
这个很简单的用keydown 下面是代码:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Print "你按下了:" + Chr(KeyCode)
End Sub
键盘中每个按键都有个与之对应的ASCII码
用chr函数将你按下的那个键的ASCII码转换成字符。
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Print "你按下了:" + Chr(KeyCode)
End Sub
键盘中每个按键都有个与之对应的ASCII码
用chr函数将你按下的那个键的ASCII码转换成字符。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Form_KeyPress(KeyAscii As Integer)
Cls
Print "你按下的是:" & Chr(KeyAscii)
End Sub
Cls
Print "你按下的是:" & Chr(KeyAscii)
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Form_KeyPress(KeyAscii As Integer)
Print "你按下了" & Chr(KeyAscii)
End Sub
Print "你按下了" & Chr(KeyAscii)
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
简单得很:
做个键盘hook,然后返回keykode就行。你懒得做就留个邮箱,做个dll给你发过去,引用一下就行
做个键盘hook,然后返回keykode就行。你懒得做就留个邮箱,做个dll给你发过去,引用一下就行
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询