-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHotkeysLoad.cs
150 lines (132 loc) · 5.04 KB
/
HotkeysLoad.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
using Microsoft.Toolkit.Uwp.Notifications;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using System.Diagnostics;
using Windows.System;
namespace tray_yeeter_sharp
{
internal partial class HotkeysLoad
{
private static readonly List<IntPtr> yeetedWindows = [];
private static readonly List<IntPtr> enumWindowsResult = [];
private static readonly List<VirtualKey> modifierKeys = [
VirtualKey.LeftControl,
VirtualKey.RightControl,
VirtualKey.Control,
VirtualKey.LeftShift,
VirtualKey.RightShift,
VirtualKey.Shift,
VirtualKey.LeftMenu,
VirtualKey.RightMenu,
VirtualKey.Menu,
VirtualKey.LeftWindows,
VirtualKey.RightWindows];
private static readonly SortedSet<VirtualKey> currentKeys = [];
private static readonly List<SortedSet<VirtualKey>> assignedHotkeys = [];
private static HotkeyEvent pointedEvent = delegate { };
public delegate void HotkeyEvent(int Id);
public static void RegisterHotkeys(HotkeyEvent hotkeyEvent)
{
assignedHotkeys.Clear();
pointedEvent = hotkeyEvent;
KeyboardHookHandler.RemoveHook();
ConfigParse();
KeyboardHookHandler.SetupHook(DownKeyEvent, UpKeyEvent);
}
private static bool DownKeyEvent(VirtualKey key)
{
int index = assignedHotkeys.FindIndex(set => set.SetEquals([key]));
if (index != -1 && !currentKeys.Intersect(modifierKeys).Any())
{
pointedEvent(index);
return true;
}
currentKeys.Add(key);
index = assignedHotkeys.FindIndex(set => set.SetEquals(currentKeys));
if (index != -1)
{
pointedEvent(index);
return true;
}
return false;
}
private static bool UpKeyEvent(VirtualKey key)
{
currentKeys.Remove(key);
return false;
}
private static void ConfigParse()
{
JSchema configSchema = JSchema.Parse(@"{
'type': 'object',
'properties': {
'yeet': {
'type': 'array',
'items': {
'type': 'string'
}
},
'unyeet': {
'type': 'array',
'items': {
'type': 'string'
}
},
'yeet_all': {
'type': 'array',
'items': {
'type': 'string'
}
},
'unyeet_all': {
'type': 'array',
'items': {
'type': 'string'
}
},
},
'required': ['yeet', 'unyeet', 'yeet_all', 'unyeet_all']
}");
JObject config = JObject.Parse(File.ReadAllText("config.json"));
if (!config.IsValid(configSchema))
{
new ToastContentBuilder()
.AddText("Invalid config ╯︿╰")
.AddText("Please check config.json and Github page to correct it.")
.Show();
return;
}
try
{
InitHotkey("yeet", config);
InitHotkey("unyeet", config);
InitHotkey("yeet_all", config);
InitHotkey("unyeet_all", config);
}
catch
{
new ToastContentBuilder()
.AddText("Tray Yeeter can't register your hotkeys (┬┬﹏┬┬)")
.AddText("Check your config.json for spelling errors then reload. If nothing is wrong and TY still can't register, please open an issue on Github.")
.Show();
return;
}
new ToastContentBuilder()
.AddText("Tray Yeeter has settled in (。・ω・。)")
.AddText("Yeet current focusing window: " + string.Join("+", config["yeet"]!.Select(v => (string?)v))
+ "\nUnyeet latest yeeted window: " + string.Join("+", config["unyeet"]!.Select(v => (string?)v))
+ "\nYeet everything: " + string.Join("+", config["yeet_all"]!.Select(v => (string?)v))
+ "\nUnyeet everything: " + string.Join("+", config["unyeet_all"]!.Select(v => (string?)v)))
.Show();
}
private static void InitHotkey(string name, JObject config)
{
SortedSet<VirtualKey> hotkey = [];
foreach (string? key in config[name]!.Select(v => (string?)v))
{
hotkey.Add(Enum.Parse<VirtualKey>(key!));
}
assignedHotkeys.Add(hotkey);
}
}
}