Skip to content

Commit 9c814af

Browse files
committed
Added KeyCodeConversions class
1 parent a4c97ba commit 9c814af

2 files changed

Lines changed: 303 additions & 0 deletions

File tree

Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
1+
//Copyright (c) 2025 Bruska Technologies LLC (James Bruska)
2+
3+
//This file is part of PsyForge.
4+
//PsyForge is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5+
//PsyForge is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
6+
//You should have received a copy of the GNU General Public License along with PsyForge. If not, see <https://www.gnu.org/licenses/>.
7+
8+
using UnityEngine;
9+
using UnityEngine.InputSystem;
10+
11+
namespace PsyForge.Utilities {
12+
13+
// This class should be avoided at all costs
14+
internal static class KeyCodeConversions {
15+
16+
internal static Key KeyCodeToKey(KeyCode keyCode, Key unknownKey = Key.None, Key mouseKey = Key.None, Key joystickKey = Key.None) {
17+
switch (keyCode) {
18+
case KeyCode.None: return Key.None;
19+
case KeyCode.Backspace: return Key.Backspace;
20+
case KeyCode.Delete: return Key.Delete;
21+
case KeyCode.Tab: return Key.Tab;
22+
case KeyCode.Clear: return unknownKey; // Conversion unknown.
23+
case KeyCode.Return: return Key.Enter;
24+
case KeyCode.Pause: return Key.Pause;
25+
case KeyCode.Escape: return Key.Escape;
26+
case KeyCode.Space: return Key.Space;
27+
case KeyCode.Keypad0: return Key.Numpad0;
28+
case KeyCode.Keypad1: return Key.Numpad1;
29+
case KeyCode.Keypad2: return Key.Numpad2;
30+
case KeyCode.Keypad3: return Key.Numpad3;
31+
case KeyCode.Keypad4: return Key.Numpad4;
32+
case KeyCode.Keypad5: return Key.Numpad5;
33+
case KeyCode.Keypad6: return Key.Numpad6;
34+
case KeyCode.Keypad7: return Key.Numpad7;
35+
case KeyCode.Keypad8: return Key.Numpad8;
36+
case KeyCode.Keypad9: return Key.Numpad9;
37+
case KeyCode.KeypadPeriod: return Key.NumpadPeriod;
38+
case KeyCode.KeypadDivide: return Key.NumpadDivide;
39+
case KeyCode.KeypadMultiply: return Key.NumpadMultiply;
40+
case KeyCode.KeypadMinus: return Key.NumpadMinus;
41+
case KeyCode.KeypadPlus: return Key.NumpadPlus;
42+
case KeyCode.KeypadEnter: return Key.NumpadEnter;
43+
case KeyCode.KeypadEquals: return Key.NumpadEquals;
44+
case KeyCode.UpArrow: return Key.UpArrow;
45+
case KeyCode.DownArrow: return Key.DownArrow;
46+
case KeyCode.RightArrow: return Key.RightArrow;
47+
case KeyCode.LeftArrow: return Key.LeftArrow;
48+
case KeyCode.Insert: return Key.Insert;
49+
case KeyCode.Home: return Key.Home;
50+
case KeyCode.End: return Key.End;
51+
case KeyCode.PageUp: return Key.PageUp;
52+
case KeyCode.PageDown: return Key.PageDown;
53+
case KeyCode.F1: return Key.F1;
54+
case KeyCode.F2: return Key.F2;
55+
case KeyCode.F3: return Key.F3;
56+
case KeyCode.F4: return Key.F4;
57+
case KeyCode.F5: return Key.F5;
58+
case KeyCode.F6: return Key.F6;
59+
case KeyCode.F7: return Key.F7;
60+
case KeyCode.F8: return Key.F8;
61+
case KeyCode.F9: return Key.F9;
62+
case KeyCode.F10: return Key.F10;
63+
case KeyCode.F11: return Key.F11;
64+
case KeyCode.F12: return Key.F12;
65+
case KeyCode.F13: return unknownKey; // Conversion unknown.
66+
case KeyCode.F14: return unknownKey; // Conversion unknown.
67+
case KeyCode.F15: return unknownKey; // Conversion unknown.
68+
case KeyCode.Alpha0: return Key.Digit0;
69+
case KeyCode.Alpha1: return Key.Digit1;
70+
case KeyCode.Alpha2: return Key.Digit2;
71+
case KeyCode.Alpha3: return Key.Digit3;
72+
case KeyCode.Alpha4: return Key.Digit4;
73+
case KeyCode.Alpha5: return Key.Digit5;
74+
case KeyCode.Alpha6: return Key.Digit6;
75+
case KeyCode.Alpha7: return Key.Digit7;
76+
case KeyCode.Alpha8: return Key.Digit8;
77+
case KeyCode.Alpha9: return Key.Digit9;
78+
case KeyCode.Exclaim: return unknownKey; // Conversion unknown.
79+
case KeyCode.DoubleQuote: return unknownKey; // Conversion unknown.
80+
case KeyCode.Hash: return unknownKey; // Conversion unknown.
81+
case KeyCode.Dollar: return unknownKey; // Conversion unknown.
82+
case KeyCode.Percent: return unknownKey; // Conversion unknown.
83+
case KeyCode.Ampersand: return unknownKey; // Conversion unknown.
84+
case KeyCode.Quote: return Key.Quote;
85+
case KeyCode.LeftParen: return unknownKey; // Conversion unknown.
86+
case KeyCode.RightParen: return unknownKey; // Conversion unknown.
87+
case KeyCode.Asterisk: return unknownKey; // Conversion unknown.
88+
case KeyCode.Plus: return Key.None; // TODO
89+
case KeyCode.Comma: return Key.Comma;
90+
case KeyCode.Minus: return Key.Minus;
91+
case KeyCode.Period: return Key.Period;
92+
case KeyCode.Slash: return Key.Slash;
93+
case KeyCode.Colon: return unknownKey; // Conversion unknown.
94+
case KeyCode.Semicolon: return Key.Semicolon;
95+
case KeyCode.Less: return Key.None;
96+
case KeyCode.Equals: return Key.Equals;
97+
case KeyCode.Greater: return unknownKey; // Conversion unknown.
98+
case KeyCode.Question: return unknownKey; // Conversion unknown.
99+
case KeyCode.At: return unknownKey; // Conversion unknown.
100+
case KeyCode.LeftBracket: return Key.LeftBracket;
101+
case KeyCode.Backslash: return Key.Backslash;
102+
case KeyCode.RightBracket: return Key.RightBracket;
103+
case KeyCode.Caret: return Key.None; // TODO
104+
case KeyCode.Underscore: return unknownKey; // Conversion unknown.
105+
case KeyCode.BackQuote: return Key.Backquote;
106+
case KeyCode.A: return Key.A;
107+
case KeyCode.B: return Key.B;
108+
case KeyCode.C: return Key.C;
109+
case KeyCode.D: return Key.D;
110+
case KeyCode.E: return Key.E;
111+
case KeyCode.F: return Key.F;
112+
case KeyCode.G: return Key.G;
113+
case KeyCode.H: return Key.H;
114+
case KeyCode.I: return Key.I;
115+
case KeyCode.J: return Key.J;
116+
case KeyCode.K: return Key.K;
117+
case KeyCode.L: return Key.L;
118+
case KeyCode.M: return Key.M;
119+
case KeyCode.N: return Key.N;
120+
case KeyCode.O: return Key.O;
121+
case KeyCode.P: return Key.P;
122+
case KeyCode.Q: return Key.Q;
123+
case KeyCode.R: return Key.R;
124+
case KeyCode.S: return Key.S;
125+
case KeyCode.T: return Key.T;
126+
case KeyCode.U: return Key.U;
127+
case KeyCode.V: return Key.V;
128+
case KeyCode.W: return Key.W;
129+
case KeyCode.X: return Key.X;
130+
case KeyCode.Y: return Key.Y;
131+
case KeyCode.Z: return Key.Z;
132+
case KeyCode.LeftCurlyBracket: return unknownKey; // Conversion unknown.
133+
case KeyCode.Pipe: return unknownKey; // Conversion unknown.
134+
case KeyCode.RightCurlyBracket: return unknownKey; // Conversion unknown.
135+
case KeyCode.Tilde: return unknownKey; // Conversion unknown.
136+
case KeyCode.Numlock: return Key.NumLock;
137+
case KeyCode.CapsLock: return Key.CapsLock;
138+
case KeyCode.ScrollLock: return Key.ScrollLock;
139+
case KeyCode.RightShift: return Key.RightShift;
140+
case KeyCode.LeftShift: return Key.LeftShift;
141+
case KeyCode.RightControl: return Key.RightCtrl;
142+
case KeyCode.LeftControl: return Key.LeftCtrl;
143+
case KeyCode.RightAlt: return Key.RightAlt;
144+
case KeyCode.LeftAlt: return Key.LeftAlt;
145+
case KeyCode.LeftCommand: return Key.LeftCommand;
146+
// case KeyCode.LeftApple: (same as LeftCommand)
147+
case KeyCode.LeftWindows: return Key.LeftWindows;
148+
case KeyCode.RightCommand: return Key.RightCommand;
149+
// case KeyCode.RightApple: (same as RightCommand)
150+
case KeyCode.RightWindows: return Key.RightWindows;
151+
case KeyCode.AltGr: return Key.AltGr;
152+
case KeyCode.Help: return unknownKey; // Conversion unknown.
153+
case KeyCode.Print: return Key.PrintScreen;
154+
case KeyCode.SysReq: return unknownKey; // Conversion unknown.
155+
case KeyCode.Break: return unknownKey; // Conversion unknown.
156+
case KeyCode.Menu: return Key.ContextMenu;
157+
case KeyCode.Mouse0:
158+
case KeyCode.Mouse1:
159+
case KeyCode.Mouse2:
160+
case KeyCode.Mouse3:
161+
case KeyCode.Mouse4:
162+
case KeyCode.Mouse5:
163+
case KeyCode.Mouse6:
164+
return mouseKey; // Not supported anymore.
165+
166+
// All other keys are joystick keys which do not
167+
// exist anymore in the new input system.
168+
default:
169+
return joystickKey; // Not supported anymore.
170+
}
171+
}
172+
173+
internal static KeyCode KeyToKeyCode(Key key, KeyCode unknownKey = KeyCode.None) {
174+
switch (key) {
175+
case Key.None: return KeyCode.None;
176+
case Key.Space: return KeyCode.Space;
177+
case Key.Enter: return KeyCode.Return;
178+
case Key.Tab: return KeyCode.Tab;
179+
case Key.Backquote: return KeyCode.BackQuote;
180+
case Key.Quote: return KeyCode.Quote;
181+
case Key.Semicolon: return KeyCode.Semicolon;
182+
case Key.Comma: return KeyCode.Comma;
183+
case Key.Period: return KeyCode.Period;
184+
case Key.Slash: return KeyCode.Slash;
185+
case Key.Backslash: return KeyCode.Backslash;
186+
case Key.LeftBracket: return KeyCode.LeftBracket;
187+
case Key.RightBracket: return KeyCode.RightBracket;
188+
case Key.Minus: return KeyCode.Minus;
189+
case Key.Equals: return KeyCode.Equals;
190+
case Key.A: return KeyCode.A;
191+
case Key.B: return KeyCode.B;
192+
case Key.C: return KeyCode.C;
193+
case Key.D: return KeyCode.D;
194+
case Key.E: return KeyCode.E;
195+
case Key.F: return KeyCode.F;
196+
case Key.G: return KeyCode.G;
197+
case Key.H: return KeyCode.H;
198+
case Key.I: return KeyCode.I;
199+
case Key.J: return KeyCode.J;
200+
case Key.K: return KeyCode.K;
201+
case Key.L: return KeyCode.L;
202+
case Key.M: return KeyCode.M;
203+
case Key.N: return KeyCode.N;
204+
case Key.O: return KeyCode.O;
205+
case Key.P: return KeyCode.P;
206+
case Key.Q: return KeyCode.Q;
207+
case Key.R: return KeyCode.R;
208+
case Key.S: return KeyCode.S;
209+
case Key.T: return KeyCode.T;
210+
case Key.U: return KeyCode.U;
211+
case Key.V: return KeyCode.V;
212+
case Key.W: return KeyCode.W;
213+
case Key.X: return KeyCode.X;
214+
case Key.Y: return KeyCode.Y;
215+
case Key.Z: return KeyCode.Z;
216+
case Key.Digit1: return KeyCode.Alpha1;
217+
case Key.Digit2: return KeyCode.Alpha2;
218+
case Key.Digit3: return KeyCode.Alpha3;
219+
case Key.Digit4: return KeyCode.Alpha4;
220+
case Key.Digit5: return KeyCode.Alpha5;
221+
case Key.Digit6: return KeyCode.Alpha6;
222+
case Key.Digit7: return KeyCode.Alpha7;
223+
case Key.Digit8: return KeyCode.Alpha8;
224+
case Key.Digit9: return KeyCode.Alpha9;
225+
case Key.Digit0: return KeyCode.Alpha0;
226+
case Key.LeftShift: return KeyCode.LeftShift;
227+
case Key.RightShift: return KeyCode.RightShift;
228+
case Key.LeftAlt: return KeyCode.LeftAlt;
229+
case Key.RightAlt: return KeyCode.RightAlt;
230+
case Key.LeftCtrl: return KeyCode.LeftControl;
231+
case Key.RightCtrl: return KeyCode.RightControl;
232+
case Key.LeftCommand: return KeyCode.LeftCommand;
233+
case Key.RightCommand: return KeyCode.RightCommand;
234+
case Key.ContextMenu:
235+
return unknownKey;
236+
case Key.Escape: return KeyCode.Escape;
237+
case Key.LeftArrow: return KeyCode.LeftArrow;
238+
case Key.RightArrow: return KeyCode.RightArrow;
239+
case Key.UpArrow: return KeyCode.UpArrow;
240+
case Key.DownArrow: return KeyCode.DownArrow;
241+
case Key.Backspace: return KeyCode.Backspace;
242+
case Key.PageDown: return KeyCode.PageDown;
243+
case Key.PageUp: return KeyCode.PageUp;
244+
case Key.Home: return KeyCode.Home;
245+
case Key.End: return KeyCode.End;
246+
case Key.Insert: return KeyCode.Insert;
247+
case Key.Delete: return KeyCode.Delete;
248+
case Key.CapsLock: return KeyCode.CapsLock;
249+
case Key.NumLock: return KeyCode.Numlock;
250+
case Key.PrintScreen: return KeyCode.Print;
251+
case Key.ScrollLock: return KeyCode.ScrollLock;
252+
case Key.Pause: return KeyCode.Pause;
253+
case Key.NumpadEnter: return KeyCode.KeypadEnter;
254+
case Key.NumpadDivide: return KeyCode.KeypadDivide;
255+
case Key.NumpadMultiply: return KeyCode.KeypadMultiply;
256+
case Key.NumpadPlus: return KeyCode.KeypadPlus;
257+
case Key.NumpadMinus: return KeyCode.KeypadMinus;
258+
case Key.NumpadPeriod: return KeyCode.KeypadPeriod;
259+
case Key.NumpadEquals: return KeyCode.KeypadEquals;
260+
case Key.Numpad0: return KeyCode.Keypad0;
261+
case Key.Numpad1: return KeyCode.Keypad1;
262+
case Key.Numpad2: return KeyCode.Keypad2;
263+
case Key.Numpad3: return KeyCode.Keypad3;
264+
case Key.Numpad4: return KeyCode.Keypad4;
265+
case Key.Numpad5: return KeyCode.Keypad5;
266+
case Key.Numpad6: return KeyCode.Keypad6;
267+
case Key.Numpad7: return KeyCode.Keypad7;
268+
case Key.Numpad8: return KeyCode.Keypad8;
269+
case Key.Numpad9: return KeyCode.Keypad9;
270+
case Key.F1: return KeyCode.F1;
271+
case Key.F2: return KeyCode.F2;
272+
case Key.F3: return KeyCode.F3;
273+
case Key.F4: return KeyCode.F4;
274+
case Key.F5: return KeyCode.F5;
275+
case Key.F6: return KeyCode.F6;
276+
case Key.F7: return KeyCode.F7;
277+
case Key.F8: return KeyCode.F8;
278+
case Key.F9: return KeyCode.F9;
279+
case Key.F10: return KeyCode.F10;
280+
case Key.F11: return KeyCode.F11;
281+
case Key.F12: return KeyCode.F12;
282+
case Key.OEM1:
283+
return unknownKey;
284+
case Key.OEM2:
285+
return unknownKey;
286+
case Key.OEM3:
287+
return unknownKey;
288+
case Key.OEM4:
289+
return unknownKey;
290+
case Key.OEM5:
291+
return unknownKey;
292+
case Key.IMESelected:
293+
return unknownKey;
294+
default:
295+
return unknownKey;
296+
}
297+
}
298+
299+
}
300+
301+
}

Runtime/Scripts/Utilities/KeyCodeConversions.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)