Skip to content

Commit

Permalink
Support mapping more keys & fix controller D-Pad input issue in keyma…
Browse files Browse the repository at this point in the history
…p editor (#148)

* fix editor input issues

* abbreviate long keynames

* undo abbreviate page

* generate one dict from other two
  • Loading branch information
XuYicong authored Jun 28, 2024
1 parent 25f67ff commit 4ab81c5
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ public class EditorControllerEventAdapter: ControllerEventAdapter {
alias = dpadElement.left.aliases.first!
}
if dpadElement.yAxis.value > 0 {
alias = dpadElement.down.aliases.first!
} else if dpadElement.yAxis.value < 0 {
alias = dpadElement.up.aliases.first!
} else if dpadElement.yAxis.value < 0 {
alias = dpadElement.down.aliases.first!
}
}
if alias == "Direction Pad" {
return
}
EditorController.shared.setKey(alias)
}

Expand Down
265 changes: 101 additions & 164 deletions PlayTools/Keymap/KeyCodeNames.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Should match https://github.com/PlayCover/PlayCover/blob/develop/PlayCover/Model/KeyCodeNames.swift exactly
import GameController
class KeyCodeNames {
public static let defaultCode = -10

Expand All @@ -12,7 +13,7 @@ class KeyCodeNames {
public static let scrollWheelDrag = "ScrollWheelDrag"
public static let fakeMouse = "FakeMouse"

public static let keyCodes = [
private static let gcKeyCodeLiteral = [
-4: "cA",
-5: "cX",
-6: "cB",
Expand Down Expand Up @@ -59,7 +60,6 @@ class KeyCodeNames {
67: "F10",
68: "F11",
69: "F12",
// 100: "§",
30: "1",
31: "2",
32: "3",
Expand Down Expand Up @@ -108,87 +108,60 @@ class KeyCodeNames {
55: ".",
56: "/"
]
public static let virtualCodes: [UInt16: String] = [
0: "A",
11: "B",
8: "C",
2: "D",
14: "E",
3: "F",
5: "G",
4: "H",
34: "I",
38: "J",
40: "K",
37: "L",
46: "M",
45: "N",
31: "O",
35: "P",
12: "Q",
15: "R",
1: "S",
17: "T",
32: "U",
9: "V",
13: "W",
7: "X",
16: "Y",
6: "Z",
18: "1",
19: "2",
20: "3",
21: "4",
23: "5",
22: "6",
26: "7",
28: "8",
25: "9",
29: "0",
36: "Enter",
53: "Esc",
51: "Del",
48: "Tab",
49: "Spc",
27: "-",
24: "=",
33: "[",
30: "]",
42: "\\",
41: ";",
39: "'",
50: "`",
43: ",",
47: ".",
44: "/",
57: "Caps",
122: "F1",
120: "F2",
99: "F3",
118: "F4",
96: "F5",
97: "F6",
98: "F7",
100: "F8",
101: "F9",
109: "F10",
103: "F11",
111: "F12",
124: "Right",
123: "Left",
125: "Down",
126: "Up",
// §: "§",
56: "Lshft",
58: "LOpt",
55: "LCmd",
60: "Rshft",
61: "ROpt",
54: "RCmd",
59: "LCtrl",
62: "RCtrl"
]
public static let mapNSEventVirtualCodeToGCKeyCodeRawValue: [UInt16: Int] = [
public static let keyCodes: [Int: String] = {
// Dictionary initializer must be literal, so moving to a function
// Swift Lint said function was too long, so split into two parts
var gcCode = gcKeyCodeLiteral
gcCode[GCKeyCode.F17.rawValue] = "F17"
gcCode[GCKeyCode.keypadPeriod.rawValue] = "Keypad ."
gcCode[GCKeyCode.keypadAsterisk.rawValue] = "Keypad *"
gcCode[GCKeyCode.keypadPlus.rawValue] = "Keypad +"
gcCode[GCKeyCode.keypadNumLock.rawValue] = "NumLock"
gcCode[GCKeyCode.keypadSlash.rawValue] = "Keypad /"
gcCode[GCKeyCode.keypadEnter.rawValue] = "Keypad Enter"
gcCode[GCKeyCode.keypadHyphen.rawValue] = "Keypad -"
gcCode[GCKeyCode.F18.rawValue] = "F18"
gcCode[GCKeyCode.F19.rawValue] = "F19"
gcCode[GCKeyCode.keypadEqualSign.rawValue] = "Keypad ="
gcCode[GCKeyCode.keypad0.rawValue] = "Keypad 0"
gcCode[GCKeyCode.keypad1.rawValue] = "Keypad 1"
gcCode[GCKeyCode.keypad2.rawValue] = "Keypad 2"
gcCode[GCKeyCode.keypad3.rawValue] = "Keypad 3"
gcCode[GCKeyCode.keypad4.rawValue] = "Keypad 4"
gcCode[GCKeyCode.keypad5.rawValue] = "Keypad 5"
gcCode[GCKeyCode.keypad6.rawValue] = "Keypad 6"
gcCode[GCKeyCode.keypad7.rawValue] = "Keypad 7"
gcCode[GCKeyCode.F20.rawValue] = "F20"
gcCode[GCKeyCode.keypad8.rawValue] = "Keypad 8"
gcCode[GCKeyCode.keypad9.rawValue] = "Keypad 9"
gcCode[GCKeyCode.international3.rawValue] = "¥" // IntlYen
gcCode[GCKeyCode.international1.rawValue] = "" // IntlRo
gcCode[GCKeyCode.nonUSBackslash.rawValue] = "§" // 100
gcCode[GCKeyCode.nonUSPound.rawValue] = "Keypad ," // Doubt: IntlHash and keypad comma not same?
gcCode[GCKeyCode.LANG2.rawValue] = "英数 한자" // Eisu(alphanumeric) Hanja
gcCode[GCKeyCode.LANG1.rawValue] = "かな 한/영" // Kana Korean/English
gcCode[GCKeyCode.F13.rawValue] = "F13"
gcCode[GCKeyCode.F16.rawValue] = "F16"
gcCode[GCKeyCode.F14.rawValue] = "F14"
gcCode[GCKeyCode.application.rawValue] = "Ctx Menu"
gcCode[GCKeyCode.F15.rawValue] = "F15"
gcCode[GCKeyCode.insert.rawValue] = "Help"
gcCode[GCKeyCode.home.rawValue] = "Home"
gcCode[GCKeyCode.pageUp.rawValue] = "Page Up"
gcCode[GCKeyCode.deleteForward.rawValue] = "Del Fwd"
gcCode[GCKeyCode.end.rawValue] = "End"
gcCode[GCKeyCode.pageDown.rawValue] = "Page Down"
return gcCode
}()
}
// Swift lint said the class was too long, so split into two parts
extension KeyCodeNames {
public static let virtualCodes: [UInt16: String] = Dictionary(uniqueKeysWithValues:
mapNSEventVirtualCodeToGCKeyCodeRawValue.map({virtualCode, gcCode in
(virtualCode, keyCodes[gcCode]!)
})
)
private static let mapVirtualToGcLiteral: [UInt16: Int] = [
0: 4,
1: 22,
2: 7,
Expand Down Expand Up @@ -267,84 +240,48 @@ public static let virtualCodes: [UInt16: String] = [
125: 81,
126: 82
]
}
let mapGCKeyCodeRawValuetoNSEventVirtualCode = [
41: 53, // Esc
44: 49,
225: 0x38, // "Lshft",
57: 0x39, // "Caps",
43: 48,
227: 0x37, // "LCmd",
226: 0x3A, // "LOpt",
231: 0x36, // "RCmd",
230: 0x3D, // "ROpt",
40: 36,
42: 51,
229: 0x3C, // "Rshft",
80: 123,
79: 124,
82: 126,
81: 125,
58: 122, // "F1",
59: 120, // "F2",
60: 99, // "F3",
61: 118, // "F4",
62: 96, // "F5",
63: 97, // "F6",
64: 98, // "F7",
65: 100, // "F8",
66: 101,
67: 109,
68: 0x67, // "F11",
69: 111, // "F12",
// 100: "§",
30: 18, // "1",
31: 19, // "2",
32: 20, // "3",
33: 21, // "4",
34: 23, // "5",
35: 22, // "6",
36: 26, // "7",
37: 28, // "8",
38: 25, // "9",
39: 29, // -"0",
45: 27, // "-",
46: 24, // "=",
20: 12, // "Q",
26: 13, // "W",
8: 14, // "E",
21: 15, // "R",
23: 17, // u"T",
28: 16, // "Y",
24: 32, // "U",
12: 34, // "I",
18: 31, // "O",
19: 35, // "P",
47: 33, // "[",
48: 30, // "]",
4: 0, // "A",
22: 1, // "S",
7: 2, // "D",
9: 3, // "F",
10: 5, // "G",
11: 4, // "H",
13: 38, // "J",
14: 40, // "K",
15: 37, // "L",
51: 41, // ";",
52: 39, // "'",
49: 42, // "\\",
29: 6, // "Z",
53: 50, // "`",
27: 7, // "X",
6: 8, // "C",
25: 9, // "V",
5: 11, // "B",
17: 45, // "N",
16: 46, // "M",
54: 43, // ",",
55: 47, // ".",
56: 44, // "/"
224: 59, // "LCtrl",
228: 62 // "RCtrl",
]
public static let mapNSEventVirtualCodeToGCKeyCodeRawValue: [UInt16: Int] = {
var mapVirtualToGc = mapVirtualToGcLiteral
mapVirtualToGc[10] = GCKeyCode.nonUSBackslash.rawValue
mapVirtualToGc[64] = GCKeyCode.F17.rawValue
mapVirtualToGc[65] = GCKeyCode.keypadPeriod.rawValue
mapVirtualToGc[67] = GCKeyCode.keypadAsterisk.rawValue
mapVirtualToGc[69] = GCKeyCode.keypadPlus.rawValue
mapVirtualToGc[71] = GCKeyCode.keypadNumLock.rawValue
mapVirtualToGc[75] = GCKeyCode.keypadSlash.rawValue
mapVirtualToGc[76] = GCKeyCode.keypadEnter.rawValue
mapVirtualToGc[78] = GCKeyCode.keypadHyphen.rawValue
mapVirtualToGc[79] = GCKeyCode.F18.rawValue
mapVirtualToGc[80] = GCKeyCode.F19.rawValue
mapVirtualToGc[81] = GCKeyCode.keypadEqualSign.rawValue
mapVirtualToGc[82] = GCKeyCode.keypad0.rawValue
mapVirtualToGc[83] = GCKeyCode.keypad1.rawValue
mapVirtualToGc[84] = GCKeyCode.keypad2.rawValue
mapVirtualToGc[85] = GCKeyCode.keypad3.rawValue
mapVirtualToGc[86] = GCKeyCode.keypad4.rawValue
mapVirtualToGc[87] = GCKeyCode.keypad5.rawValue
mapVirtualToGc[88] = GCKeyCode.keypad6.rawValue
mapVirtualToGc[89] = GCKeyCode.keypad7.rawValue
mapVirtualToGc[90] = GCKeyCode.F20.rawValue
mapVirtualToGc[91] = GCKeyCode.keypad8.rawValue
mapVirtualToGc[92] = GCKeyCode.keypad9.rawValue
mapVirtualToGc[93] = GCKeyCode.international3.rawValue
mapVirtualToGc[94] = GCKeyCode.international1.rawValue
mapVirtualToGc[95] = GCKeyCode.nonUSPound.rawValue
mapVirtualToGc[102] = GCKeyCode.LANG2.rawValue
mapVirtualToGc[104] = GCKeyCode.LANG1.rawValue
mapVirtualToGc[105] = GCKeyCode.F13.rawValue
mapVirtualToGc[106] = GCKeyCode.F16.rawValue
mapVirtualToGc[107] = GCKeyCode.F14.rawValue
mapVirtualToGc[110] = GCKeyCode.application.rawValue
mapVirtualToGc[113] = GCKeyCode.F15.rawValue
mapVirtualToGc[114] = GCKeyCode.insert.rawValue
mapVirtualToGc[115] = GCKeyCode.home.rawValue
mapVirtualToGc[116] = GCKeyCode.pageUp.rawValue
mapVirtualToGc[117] = GCKeyCode.deleteForward.rawValue
mapVirtualToGc[119] = GCKeyCode.end.rawValue
mapVirtualToGc[121] = GCKeyCode.pageDown.rawValue
assert(keyCodes.count - 15 == mapVirtualToGc.count)
return mapVirtualToGc
}()
}// Swift lint said the file was too long so removing the unused reverse mapping

0 comments on commit 4ab81c5

Please sign in to comment.