主页
文章
分类
系列
标签
关于
友链
掌握按键精灵:高效查询键盘按键码和名称
【按键精灵】查询按键码、按键名!
发布于: 2023-12-3   更新于: 2024-1-3   收录于: 按键精灵
文章字数: 1681   阅读时间: 4 分钟   阅读量:

【按键精灵】查询按键码、按键名!

在这篇详细指南中,我们将探索如何使用按键精灵来有效查询键盘的按键码和名称。通过实用的VB编程代码,您将能够快速掌握这一技能,无论是为了提高编程效率还是解决键盘功能问题,这篇文章都将是您的理想选择。

query-key-codes-names-with-key-wizard.png


基本环境


实现代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Declare Function GetKeyNameText Lib "user32" Alias "GetKeyNameTextA" (ByVal lParam As Long, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Long
Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar As Byte) As Long
Declare Function GetKeyState Lib "user32" ALIAS "GetKeyState" (ByVal nVirtKey As Long) As Integer
Function 查询按键名(按键码)
    key = String(100, Chr(0))
    ajm = 0 + 按键码
    GetKeyNameText MapVirtualKey(ajm, 0) * 65536, key, 100
    Key = Left(key, InStr(key, Chr(0)) - 1)
    Select Case ajm
    Case 91
        key = "Win"
    Case 93
        key = "Right Mouse"
    Case 44
        key = "Print Screen"
    Case 19
        key = "Pause/Break"
    Case 144
        key = "Num Lock"
    Case 110
        Key = "Num ."
    Case 111
        Key = "Num /"
    Case 45
        key = "Insert"
    Case 46
        key = "Delete"
    Case 36
        key = "Home"
    Case 35
        key = "End"
    Case 33
        key = "PageUp"
    Case 34
        key = "PageDown"
    Case 37
        key = "Left"
    Case 38
        key = "Up"
    Case 39
        key = "Right"
    Case 40
        key = "Down"
    End Select
    查询按键名 = key
End Function
Function 查询按键码(按键名)
    Dim 控制键, 辅助键, 功能键, 方向键, 字母键, 数字键, 符号键, 组合键
    Dim rep, i, tmp, MatchEs, Match, shuliang, SubMatches, 子匹配数量, 子匹配
    控制键 = "CTRL 17,ALT 18,SHIFT 16,LEFT CTRL 162,LEFT ALT 164,LEFT SHIFT 160,RIGHT CTRL 163,RIGHT ALT 165,RIGHT SHIFT 161,WIN 91,RIGHT MOUSE 93"
    功能键 = "F1 112,F2 113,F3 114,F4 115,F5 116,F6 117,F7 118,F8 119,F9 120,F10 121,F11 122,F12 123,HOME 36,END 35,PAGEDOWN 34,PAGEUP 33,ESC 27,ENTER 13,SPACE 32,INSERT 45,DELETE 46,PRINT SCREEN 44,SCROLL LOCK 145,PAUSE/BREAK 19,BACKSPACE 8,TAB 9,CAPS LOCK 20"
    方向键 = "DOWN 40,UP 38,LEFT 37,RIGHT 39"
    字母键 = "A 65,B 66,C 67,D 68,E 69,F 70,G 71,H 72,I 73,J 74,K 75,L 76,M 77,N 78,O 79,P 80,Q 81,R 82,S 83,T 84,U 85,V 86,W 87,X 88,Y 89,Z 90"
    数字键 = "0 48,1 49,2 50,3 51,4 52,5 53,6 54,7 55,8 56,9 57"
    符号键 = "` 192,- 189,= 187,[ 219,] 221,\ 220,; 186,' 222,, 188,. 190,/ 191"
    小键盘 = "NUM LOCK 144,NUM 0 96,NUM 1 97,NUM 2 98,NUM 3 99,NUM 4 100,NUM 5 101,NUM 6 102,NUM 7 103,NUM 8 104,NUM 9 105,NUM * 106,NUM + 107,NUM - 109,NUM . 110,NUM / 111"
    多媒体 = "搜索 170,收藏 171,浏览器 172,静音 173,音量减 174,音量加 175,停止 179,邮件 180"
    组合键 = "," & 控制键 & "," & 功能键 & "," & 方向键 & "," & 字母键 & "," & 数字键 & "," & 符号键 & "," & 小键盘 & ","
    按键名 = Replace(按键名, "\", "\\")
    按键名 = Replace(按键名, ".", "\.")
    按键名 = Replace(按键名, "*", "\*")
    按键名 = Replace(按键名, "+", "\+")
    按键名 = Replace(按键名, "[", "\[")
    按键名 = Replace(按键名, "]", "\]")
    按键名 = Replace(按键名, " ", "\s")
    Set rep = New RegExp
    rep.IgnoreCase = True
    rep.[Global] = True
    rep.Pattern = ","& 按键名 &"\s(\d+),"
    Set MatchEs = rep.Execute(组合键)
    shuliang = Matches.count
    For Each Match In MatchEs
        tmp = tmp & Match.Value
        Set SubMatches = Match.SubMatches  '创建子匹配对象
        子匹配数量 = SubMatches.Count
        For i = 0 To 子匹配数量-1
            子匹配 = SubMatches.Item(i)
        Next
    Next
    查询按键码 = 0 + 子匹配
End Function

调用代码

1
2
3
4
5
6
7
'通过按键码查询按键名
For i = 0 To 255
    If 查询按键名(i) <> "" Then TracePrint i & " = " & 查询按键名(i)
Next

'通过按键名查询按键码
TracePrint 查询按键码("A")



微信公众号:梦痕编程




文章标题:掌握按键精灵:高效查询键盘按键码和名称
本文作者:神梦无痕
本文链接:https://smwhff.com/posts/query-key-codes-names-with-key-wizard/
版权声明:本人所有文章除特别声明外,都遵守 BY-NC-SA 协议条款。转载请注明出处!