forked from jaburns/NintendoSpy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeybindings.cs
executable file
·172 lines (151 loc) · 6.48 KB
/
Keybindings.cs
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Windows.Input;
using System.Windows.Markup;
using NintendoSpy.Readers;
namespace NintendoSpy
{
public class Keybindings
{
class Binding
{
readonly public ushort OutputKey;
readonly public IReadOnlyList <string> RequiredButtons;
public bool CurrentlyDepressed;
public Binding (ushort outputKey, IReadOnlyList <string> requiredButtons) {
OutputKey = outputKey;
RequiredButtons = requiredButtons;
}
}
public const string XML_FILE_PATH = "keybindings.xml";
IControllerReader _reader;
List <Binding> _bindings = new List <Binding> ();
public Keybindings (string xmlFilePath, IControllerReader reader)
{
var xmlPath = Path.Combine (Environment.CurrentDirectory, xmlFilePath);
if (! File.Exists (xmlPath)) {
throw new ConfigParseException ("Could not find "+XML_FILE_PATH);
}
var doc = XDocument.Load (xmlPath);
foreach (var binding in doc.Root.Elements ("binding"))
{
var outputKey = readKeybinding (binding.Attribute ("output-key").Value);
if (outputKey == 0) continue;
List <string> requiredButtons = new List <string> ();
foreach (var input in binding.Elements ("input")) {
requiredButtons.Add (input.Attribute ("button").Value);
}
if (requiredButtons.Count < 1) continue;
_bindings.Add (new Binding (outputKey, requiredButtons));
}
_reader = reader;
_reader.ControllerStateChanged += reader_ControllerStateChanged;
}
public void Finish ()
{
_reader.ControllerStateChanged -= reader_ControllerStateChanged;
}
void reader_ControllerStateChanged (IControllerReader reader, ControllerState state)
{
foreach (var binding in _bindings)
{
bool allRequiredButtonsDown = true;
foreach (var requiredButton in binding.RequiredButtons) {
allRequiredButtonsDown &= state.Buttons [requiredButton];
}
if (allRequiredButtonsDown && !binding.CurrentlyDepressed) {
SendKeys.PressKey (binding.OutputKey);
binding.CurrentlyDepressed = true;
}
else if (!allRequiredButtonsDown && binding.CurrentlyDepressed) {
SendKeys.ReleaseKey (binding.OutputKey);
binding.CurrentlyDepressed = false;
}
}
}
static ushort readKeybinding (string name)
{
var upperName = name.ToUpperInvariant ();
if (Regex.Match (upperName, "^[A-Z0-9]$").Success) {
return (ushort)upperName.ToCharArray()[0];
}
else if (VK_KEYWORDS.ContainsKey (upperName)) {
return VK_KEYWORDS [upperName];
}
else {
return 0;
}
}
static ushort vkConvert (Key key) {
return (ushort) KeyInterop.VirtualKeyFromKey (key);
}
static readonly IReadOnlyDictionary <string, ushort> VK_KEYWORDS = new Dictionary <string, ushort> {
{ "ENTER", vkConvert (Key.Enter) },
{ "TAB", vkConvert (Key.Tab) },
{ "ESC", vkConvert (Key.Escape) },
{ "ESCAPE", vkConvert (Key.Escape) },
{ "HOME", vkConvert (Key.Home) },
{ "END", vkConvert (Key.End) },
{ "LEFT", vkConvert (Key.Left) },
{ "RIGHT", vkConvert (Key.Right) },
{ "UP", vkConvert (Key.Up) },
{ "DOWN", vkConvert (Key.Down) },
{ "PGUP", vkConvert (Key.Prior) },
{ "PGDN", vkConvert (Key.Next) },
{ "NUMLOCK", vkConvert (Key.NumLock) },
{ "SCROLLLOCK", vkConvert (Key.Scroll) },
{ "PRTSC", vkConvert (Key.PrintScreen) },
{ "BREAK", vkConvert (Key.Cancel) },
{ "BACKSPACE", vkConvert (Key.Back) },
{ "BKSP", vkConvert (Key.Back) },
{ "BS", vkConvert (Key.Back) },
{ "CLEAR", vkConvert (Key.Clear) },
{ "CAPSLOCK", vkConvert (Key.Capital) },
{ "INS", vkConvert (Key.Insert) },
{ "INSERT", vkConvert (Key.Insert) },
{ "DEL", vkConvert (Key.Delete) },
{ "DELETE", vkConvert (Key.Delete) },
{ "HELP", vkConvert (Key.Help) },
{ "F1", vkConvert (Key.F1) },
{ "F2", vkConvert (Key.F2) },
{ "F3", vkConvert (Key.F3) },
{ "F4", vkConvert (Key.F4) },
{ "F5", vkConvert (Key.F5) },
{ "F6", vkConvert (Key.F6) },
{ "F7", vkConvert (Key.F7) },
{ "F8", vkConvert (Key.F8) },
{ "F9", vkConvert (Key.F9) },
{ "F10", vkConvert (Key.F10) },
{ "F11", vkConvert (Key.F11) },
{ "F12", vkConvert (Key.F12) },
{ "F13", vkConvert (Key.F13) },
{ "F14", vkConvert (Key.F14) },
{ "F15", vkConvert (Key.F15) },
{ "F16", vkConvert (Key.F16) },
{ "NUMPAD0", vkConvert (Key.NumPad0) },
{ "NUMPAD1", vkConvert (Key.NumPad1) },
{ "NUMPAD2", vkConvert (Key.NumPad2) },
{ "NUMPAD3", vkConvert (Key.NumPad3) },
{ "NUMPAD4", vkConvert (Key.NumPad4) },
{ "NUMPAD5", vkConvert (Key.NumPad5) },
{ "NUMPAD6", vkConvert (Key.NumPad6) },
{ "NUMPAD7", vkConvert (Key.NumPad7) },
{ "NUMPAD8", vkConvert (Key.NumPad8) },
{ "NUMPAD9", vkConvert (Key.NumPad9) },
{ "MULTIPLY", vkConvert (Key.Multiply) },
{ "*", vkConvert (Key.Multiply) },
{ "ADD", vkConvert (Key.Add) },
{ "+", vkConvert (Key.Add) },
{ "SUBTRACT", vkConvert (Key.Subtract) },
{ "-", vkConvert (Key.Subtract) },
{ "DIVIDE", vkConvert (Key.Divide) },
{ "/", vkConvert (Key.Divide) }
};
}
}