-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppTray.cs
214 lines (179 loc) · 6.91 KB
/
AppTray.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using NAudio.Midi;
using System.Diagnostics;
using System.Xml.Serialization;
namespace QwertyToMIDI
{
public class AppTray : ApplicationContext
{
public int MidiDevice = -1;
public string settings_path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\QwertyToMIDI";
public string keys_settings_fileName = "\\KeysSettings.xml";
public globalKeyboardHook gkh = new globalKeyboardHook();
public settings SettingsValues = new settings();
private NotifyIcon trayIcon;
private ButtonsForm buttons_f;
private GridView gridview_f;
private SettingsForm settings_f;
private List<Form> openedForms = new List<Form>();
public AppTray()
{
trayIcon = new NotifyIcon()
{
Icon = Properties.Resources.RIcon,
ContextMenuStrip = new ContextMenuStrip()
{
Items = { new ToolStripMenuItem("Settings Hotkeys", null, SettingsHotkeys), new ToolStripMenuItem("Grid View", null, GridViewShow), new ToolStripMenuItem("Settings", null, SettingsShow), new ToolStripMenuItem("Exit", null, Exit) }
},
Visible = true
};
trayIcon.DoubleClick += new EventHandler(SettingsHotkeys);
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown);
gkh.KeyUp += new KeyEventHandler(gkh_KeyUp);
LoadFile();
LoadDevices();
}
void gkh_KeyDown(object sender, KeyEventArgs e)
{
if (MidiDevice > -1)
{
for (int i = 0; i < SettingsValues.SettingsKeys.Count; i++)
{
if (SettingsValues.SettingsKeys[i].key_status != "")
if ((SettingsValues.SettingsKeys[i].key == e.KeyCode) && (SettingsValues.SettingsKeys[i].key_status == "Key Down"))
{
MidiOut midiOut = new MidiOut(MidiDevice);
midiOut.Send(MidiMessage.ChangeControl(SettingsValues.SettingsKeys[i].midi1, SettingsValues.SettingsKeys[i].midi2, SettingsValues.SettingsKeys[i].midi3).RawData);
midiOut.Close();
Debug.WriteLine(SettingsValues.SettingsKeys[i].key);
}
}
}
}
void gkh_KeyUp(object sender, KeyEventArgs e)
{
if (MidiDevice > -1)
{
for (int i = 0; i < SettingsValues.SettingsKeys.Count; i++)
{
if (SettingsValues.SettingsKeys[i].key_status != "")
if ((SettingsValues.SettingsKeys[i].key == e.KeyCode) && (SettingsValues.SettingsKeys[i].key_status == "Key Up"))
{
MidiOut midiOut = new MidiOut(MidiDevice);
midiOut.Send(MidiMessage.ChangeControl(SettingsValues.SettingsKeys[i].midi1, SettingsValues.SettingsKeys[i].midi2, SettingsValues.SettingsKeys[i].midi3).RawData);
midiOut.Close();
Debug.WriteLine(SettingsValues.SettingsKeys[i].key);
}
}
}
}
void Exit(object? sender, EventArgs e)
{
trayIcon.Visible = false;
Application.Exit();
}
void SettingsHotkeys(object? sender, EventArgs e)
{
if (!IsFormOpened(typeof(ButtonsForm)))
{
if (IsFormOpened(typeof(GridView)))
{
gridview_f.Close();
}
buttons_f = new ButtonsForm(this);
buttons_f.FormClosed += (s, args) => openedForms.Remove(buttons_f);
openedForms.Add(buttons_f);
buttons_f.Show();
}
else
{
buttons_f.Focus();
}
}
void GridViewShow(object? sender, EventArgs e)
{
if (!IsFormOpened(typeof(GridView)))
{
if (IsFormOpened(typeof(ButtonsForm)))
{
buttons_f.Close();
}
gridview_f = new GridView(this);
gridview_f.FormClosed += (s, args) => openedForms.Remove(gridview_f);
openedForms.Add(gridview_f);
gridview_f.Show();
}
else
{
gridview_f.Focus();
}
}
void SettingsShow(object? sender, EventArgs e)
{
if (!IsFormOpened(typeof(SettingsForm)))
{
settings_f = new SettingsForm();
settings_f.FormClosed += (s, args) => openedForms.Remove(settings_f);
openedForms.Add(settings_f);
settings_f.Show();
}
else
{
settings_f.Focus();
}
}
public void SaveFile()
{
if (!Directory.Exists(settings_path))
{
Directory.CreateDirectory(settings_path);
}
string path = settings_path + keys_settings_fileName;
Stream stream = File.Open(path, FileMode.Create);
XmlSerializer serialiser = new XmlSerializer(typeof(settings));
serialiser.Serialize(stream, SettingsValues);
stream.Close();
}
private void LoadFile()
{
string path = settings_path + keys_settings_fileName;
if (File.Exists(path))
{
XmlSerializer serializer = new XmlSerializer(typeof(settings));
StreamReader reader = new StreamReader(path);
SettingsValues = serializer.Deserialize(reader) as settings;
reader.Close();
for (int i = 0; i < SettingsValues.SettingsKeys.Count; i++)
{
gkh.HookedKeys.Add(SettingsValues.SettingsKeys[i].key);
}
}
}
private void LoadDevices()
{
for (int device = 0; device < MidiOut.NumberOfDevices; device++)
{
if (SettingsValues.MidiDeviceName == MidiOut.DeviceInfo(device).ProductName)
{
MidiDevice = device;
}
}
}
private bool IsFormOpened(Type formType)
{
return openedForms.Exists(form => form.GetType() == formType);
}
}
public class settings
{
public List<settings_keys> SettingsKeys = new List<settings_keys>();
public string? MidiDeviceName { get; set; }
}
public class settings_keys
{
public Keys key { get; set; }
public string key_status { get; set; }
public int midi1 { get; set; }
public int midi2 { get; set; }
public int midi3 { get; set; }
}
}