-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapping.cs
111 lines (96 loc) · 3.46 KB
/
Mapping.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Keyboard_Usurper
{
public class KeyToKey
{
public Key From { get; }
public Key To { get; }
public int GroupId { get; }
public KeyToKey(Key from, Key to, int groupId)
{
From = from;
To = to;
GroupId = groupId;
}
}
public class Key
{
public IEnumerable<vkCode> Mods { get; }
public vkCode ActivationKey { get; }
public vkCode Code { get; }
public bool WithMods { get; }
public Key(IEnumerable<vkCode> mods, vkCode activationKey, vkCode code, bool withMods)
{
Mods = mods;
ActivationKey = activationKey;
Code = code;
WithMods = withMods;
}
}
public static class ConfigurationToMapping
{
// TODO: Make this an array?
public static List<KeyToKey> Convert(Configuration config)
{
List<KeyToKey> mappings = new();
vkCode[] modifiers = new vkCode[]{
vkCode.VK_LSHIFT,
vkCode.VK_RSHIFT,
vkCode.VK_SHIFT,
vkCode.VK_LWIN,
vkCode.VK_RWIN,
vkCode.VK_WIN,
vkCode.VK_LCONTROL,
vkCode.VK_RCONTROL,
vkCode.VK_CONTROL,
vkCode.VK_LMENU,
vkCode.VK_RMENU,
vkCode.VK_MENU
};
Func<string[], Key> createFromKey = delegate (string[] keys)
{
IEnumerable<vkCode> mods = keys
.Take(keys.Length - 1)
.Select(x => StringToCode.ConvertTo(x))
.Where(x => modifiers.Contains(x));
// TODO: A better way to do this?
vkCode activationKey = keys
.Take(keys.Length - 1)
.Select(x => StringToCode.ConvertTo(x))
.FirstOrDefault(x => !modifiers.Contains(x));
vkCode code = StringToCode.ConvertTo(keys.Last());
return new Key(mods, activationKey, code, false);
};
int count = 0;
Func<int> getGroupId = delegate ()
{
count++;
return count;
};
// TODO: Handle ---
foreach (BindingSet set in config.bindings)
{
int groupId = getGroupId();
mappings.Add(new KeyToKey(createFromKey(set.toggleBinding.Split('-')), null, groupId));
foreach (ConfigBinding binding in set.bindings)
{
string[] toKeys = binding.to.Split('-');
IEnumerable<vkCode> mods = toKeys
.Take(toKeys.Length - 1)
.Select(x => StringToCode.ConvertTo(x))
.Where(x => modifiers.Contains(x));
vkCode code = toKeys.Last()[0] == '+' ?
StringToCode.ConvertTo(toKeys.Last().Substring(1)) :
StringToCode.ConvertTo(toKeys.Last());
bool withMods = toKeys.Last()[0] == '+';
mappings.Add(new KeyToKey(createFromKey(binding.from.Split('-')), new Key(mods, vkCode.VK_NULL, code, withMods), groupId));
}
}
return mappings;
}
}
}