forked from ppy/osu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidKeyboardHandler.cs
More file actions
99 lines (86 loc) · 4.93 KB
/
Copy pathAndroidKeyboardHandler.cs
File metadata and controls
99 lines (86 loc) · 4.93 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Android.Views;
using osu.Framework.Input.Handlers;
using osu.Framework.Input.StateChanges;
using osu.Framework.Platform;
using osu.Framework.Input;
namespace osu.Android.Input
{
public class AndroidKeyboardHandler : InputHandler
{
public override string Description => "Keyboard (Low Latency)";
public override bool IsActive => Enabled.Value;
// FrozenDictionary for maximum-performance O(1) key mapping.
// Built once at startup; faster than Dictionary for read-only lookups.
private static readonly FrozenDictionary<Keycode, Key> key_map = new Dictionary<Keycode, Key>
{
{ Keycode.A, Key.A }, { Keycode.B, Key.B }, { Keycode.C, Key.C }, { Keycode.D, Key.D },
{ Keycode.E, Key.E }, { Keycode.F, Key.F }, { Keycode.G, Key.G }, { Keycode.H, Key.H },
{ Keycode.I, Key.I }, { Keycode.J, Key.J }, { Keycode.K, Key.K }, { Keycode.L, Key.L },
{ Keycode.M, Key.M }, { Keycode.N, Key.N }, { Keycode.O, Key.O }, { Keycode.P, Key.P },
{ Keycode.Q, Key.Q }, { Keycode.R, Key.R }, { Keycode.S, Key.S }, { Keycode.T, Key.T },
{ Keycode.U, Key.U }, { Keycode.V, Key.V }, { Keycode.W, Key.W }, { Keycode.X, Key.X },
{ Keycode.Y, Key.Y }, { Keycode.Z, Key.Z },
{ Keycode.Num0, Key.Number0 }, { Keycode.Num1, Key.Number1 }, { Keycode.Num2, Key.Number2 },
{ Keycode.Num3, Key.Number3 }, { Keycode.Num4, Key.Number4 }, { Keycode.Num5, Key.Number5 },
{ Keycode.Num6, Key.Number6 }, { Keycode.Num7, Key.Number7 }, { Keycode.Num8, Key.Number8 },
{ Keycode.Num9, Key.Number9 },
{ Keycode.DpadUp, Key.Up }, { Keycode.DpadDown, Key.Down },
{ Keycode.DpadLeft, Key.Left }, { Keycode.DpadRight, Key.Right },
{ Keycode.Enter, Key.Enter }, { Keycode.Escape, Key.Escape },
{ Keycode.Space, Key.Space }, { Keycode.Tab, Key.Tab },
{ Keycode.Del, Key.BackSpace }, { Keycode.ForwardDel, Key.Delete },
{ Keycode.MoveHome, Key.Home }, { Keycode.MoveEnd, Key.End },
{ Keycode.PageUp, Key.PageUp }, { Keycode.PageDown, Key.PageDown },
{ Keycode.ShiftLeft, Key.ShiftLeft }, { Keycode.ShiftRight, Key.ShiftRight },
{ Keycode.CtrlLeft, Key.ControlLeft }, { Keycode.CtrlRight, Key.ControlRight },
{ Keycode.AltLeft, Key.AltLeft }, { Keycode.AltRight, Key.AltRight },
{ Keycode.CapsLock, Key.CapsLock },
{ Keycode.F1, Key.F1 }, { Keycode.F2, Key.F2 }, { Keycode.F3, Key.F3 },
{ Keycode.F4, Key.F4 }, { Keycode.F5, Key.F5 }, { Keycode.F6, Key.F6 },
{ Keycode.F7, Key.F7 }, { Keycode.F8, Key.F8 }, { Keycode.F9, Key.F9 },
{ Keycode.F10, Key.F10 }, { Keycode.F11, Key.F11 }, { Keycode.F12, Key.F12 },
{ Keycode.Grave, Key.Tilde }, { Keycode.Minus, Key.Minus }, { Keycode.Equals, Key.Plus },
{ Keycode.LeftBracket, Key.BracketLeft }, { Keycode.RightBracket, Key.BracketRight },
{ Keycode.Backslash, Key.BackSlash }, { Keycode.Semicolon, Key.Semicolon },
{ Keycode.Apostrophe, Key.Quote }, { Keycode.Comma, Key.Comma },
{ Keycode.Period, Key.Period }, { Keycode.Slash, Key.Slash },
}.ToFrozenDictionary();
public AndroidKeyboardHandler()
{
Enabled.Default = true;
Enabled.Value = true;
}
public override bool Initialize(GameHost host)
{
if (!base.Initialize(host))
return false;
return true;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HandleKeyEvent(KeyEvent e)
{
if (!Enabled.Value) return false;
if (e.KeyCode == Keycode.Back || e.KeyCode == Keycode.Home || e.KeyCode == Keycode.Menu ||
e.KeyCode == Keycode.VolumeUp || e.KeyCode == Keycode.VolumeDown || e.KeyCode == Keycode.VolumeMute ||
e.KeyCode == Keycode.AppSwitch)
return false;
if (!e.Source.HasFlag(InputSourceType.Keyboard) && !e.Source.HasFlag(InputSourceType.Mouse) && !e.Source.HasFlag(InputSourceType.Stylus) && e.Source != InputSourceType.Unknown)
{
var device = e.Device;
if (device == null || device.KeyboardType == global::Android.Views.InputKeyboardType.None)
return false;
}
if (!key_map.TryGetValue(e.KeyCode, out var key))
return false;
bool isDown = e.Action == KeyEventActions.Down;
if (e.RepeatCount > 0 && isDown) return true;
PendingInputs.Enqueue(new KeyboardKeyInput(key, isDown));
return true;
}
}
}