· 5 years ago · Feb 23, 2021, 08:20 AM
1--- The Keys API provides a table of numerical codes corresponding to keyboard
2-- keys, suitable for decoding key events.
3--
4-- The Minecraft wiki [has a list of key
5-- codes](http://www.minecraftwiki.net/wiki/Key_codes). It is recommended that
6-- you use the constants provided by this file, rather than the underlying
7-- numerical values.
8--
9-- @module keys
10
11local expect = dofile("rom/modules/main/cc/expect.lua").expect
12
13local tKeys = {
14 nil, "one", "two", "three", "four", -- 1
15 "five", "six", "seven", "eight", "nine", -- 6
16 "zero", "minus", "equals", "backspace", "tab", -- 11
17 "q", "w", "e", "r", "t", -- 16
18 "y", "u", "i", "o", "p", -- 21
19 "leftBracket", "rightBracket", "enter", "leftCtrl", "a", -- 26
20 "s", "d", "f", "g", "h", -- 31
21 "j", "k", "l", "semiColon", "apostrophe", -- 36
22 "grave", "leftShift", "backslash", "z", "x", -- 41
23 "c", "v", "b", "n", "m", -- 46
24 "comma", "period", "slash", "rightShift", "multiply", -- 51
25 "leftAlt", "space", "capsLock", "f1", "f2", -- 56
26 "f3", "f4", "f5", "f6", "f7", -- 61
27 "f8", "f9", "f10", "numLock", "scrollLock", -- 66
28 "numPad7", "numPad8", "numPad9", "numPadSubtract", "numPad4", -- 71
29 "numPad5", "numPad6", "numPadAdd", "numPad1", "numPad2", -- 76
30 "numPad3", "numPad0", "numPadDecimal", nil, nil, -- 81
31 nil, "f11", "f12", nil, nil, -- 86
32 nil, nil, nil, nil, nil, -- 91
33 nil, nil, nil, nil, "f13", -- 96
34 "f14", "f15", nil, nil, nil, -- 101
35 nil, nil, nil, nil, nil, -- 106
36 nil, "kana", nil, nil, nil, -- 111
37 nil, nil, nil, nil, nil, -- 116
38 "convert", nil, "noconvert", nil, "yen", -- 121
39 nil, nil, nil, nil, nil, -- 126
40 nil, nil, nil, nil, nil, -- 131
41 nil, nil, nil, nil, nil, -- 136
42 "numPadEquals", nil, nil, "circumflex", "at", -- 141
43 "colon", "underscore", "kanji", "stop", "ax", -- 146
44 nil, nil, nil, nil, nil, -- 151
45 "numPadEnter", "rightCtrl", nil, nil, nil, -- 156
46 nil, nil, nil, nil, nil, -- 161
47 nil, nil, nil, nil, nil, -- 166
48 nil, nil, nil, nil, nil, -- 171
49 nil, nil, nil, "numPadComma", nil, -- 176
50 "numPadDivide", nil, nil, "rightAlt", nil, -- 181
51 nil, nil, nil, nil, nil, -- 186
52 nil, nil, nil, nil, nil, -- 191
53 nil, "pause", nil, "home", "up", -- 196
54 "pageUp", nil, "left", nil, "right", -- 201
55 nil, "end", "down", "pageDown", "insert", -- 206
56 "delete", -- 211
57}
58
59local keys = _ENV
60for nKey, sKey in pairs(tKeys) do
61 keys[sKey] = nKey
62end
63
64keys["return"] = keys.enter --- @local
65--backwards compatibility to earlier, typo prone, versions
66keys.scollLock = keys.scrollLock --- @local
67keys.cimcumflex = keys.circumflex --- @local
68
69--- Translates a numerical key code to a human-readable name. The human-readable
70-- name is one of the constants in the keys API.
71--
72-- @tparam number code The key code to look up.
73-- @treturn string|nil The name of the key, or `nil` if not a valid key code.
74function getName(code)
75 expect(1, code, "number")
76 return tKeys[code]
77end