-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
256 lines (225 loc) · 8.13 KB
/
Form1.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Input;
using System.Threading;
using Microsoft.Win32;
namespace Sader_Hotkey_Creator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Startup registry key and value
private static readonly string StartupKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
private static readonly string StartupValue = "Sader_Hotkey_Creator";
private static bool starting = false;
public static bool fully_exit = false;
private void Form1_Load(object sender, EventArgs e)
{
starting = true;
items_dataGridView.RowHeadersVisible = false;
foreach (V.CommandList Type in Enum.GetValues(typeof(V.CommandList)))
{
type_comboBox.Items.Add((Type).ToString());
}
type_comboBox.SelectedIndex = 0;
if (V.readConfig())
{
Pup_data();
}
if (Startup_Exist())
{
startup_checkBox.Checked = true;
}
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
Hide();
mini_notifyIcon.Visible = true;
mini_notifyIcon.ShowBalloonTip(100);
starting = false;
}
private void items_dataGridView_SelectionChanged(object sender, EventArgs e)
{
Thread.Sleep(100);
if (V.data_refresh) return;
int ndx = items_dataGridView.SelectedCells[0].RowIndex;
hold_label.Text = items_dataGridView.Rows[ndx].Cells[0].Value.ToString();
press_label.Text = items_dataGridView.Rows[ndx].Cells[1].Value.ToString();
type_comboBox.SelectedItem = items_dataGridView.Rows[ndx].Cells[2].Value.ToString();
command_textBox.Text = items_dataGridView.Rows[ndx].Cells[3].Value.ToString();
}
private void browse_button_Click(object sender, EventArgs e)
{
OpenFileDialog app = new OpenFileDialog();
if (app.ShowDialog() == DialogResult.OK)
{
command_textBox.Text = app.FileName;
}
}
private void hold_button_Click(object sender, EventArgs e)
{
hold_label.Text = "Click";
V.k1_cap = true;
}
private void press_button_Click(object sender, EventArgs e)
{
press_label.Text = "Click";
V.k2_cap = true;
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (V.k1_cap)
{
hold_label.Text = e.KeyCode.ToString();
V.k1_cap = false;
}
if (V.k2_cap)
{
press_label.Text = e.KeyCode.ToString();
V.k2_cap = false;
}
}
private void add_button_Click(object sender, EventArgs e)
{
Key key1;
if (Enum.TryParse(hold_label.Text, out key1))
if (!Enum.IsDefined(typeof(Key), key1)) return;
Keys key2;
if (Enum.TryParse(press_label.Text, out key2))
if (!Enum.IsDefined(typeof(Keys), key2)) return;
V.CommandList type;
if (Enum.TryParse(type_comboBox.SelectedItem.ToString(), out type))
if (!Enum.IsDefined(typeof(V.CommandList), type)) return;
V.Key_1.Add(key1);
V.Key_2.Add(key2);
V.Type.Add(type);
V.Commands.Add(command_textBox.Text);
if (Pup_data())
{
V.saveConfig();
}
}
private bool Pup_data()
{
V.data_refresh = true;
items_dataGridView.ClearSelection();
items_dataGridView.Rows.Clear();
items_dataGridView.Refresh();
for (int i = 0; i < V.Key_1.Count; i++)
{
items_dataGridView.Rows.Add(V.Key_1[i].ToString(), V.Key_2[i].ToString(), V.Type[i].ToString(), V.Commands[i]);
}
V.data_refresh = false;
return true;
}
private void delete_button_Click(object sender, EventArgs e)
{
var confirmResult = MessageBox.Show("Are you sure to delete this item ??",
"Confirm Delete!!",
MessageBoxButtons.YesNo);
if (confirmResult == DialogResult.No)
{
return;
}
int ndx = items_dataGridView.SelectedCells[0].RowIndex;
string k1 = items_dataGridView.Rows[ndx].Cells[0].Value.ToString();
string k2 = items_dataGridView.Rows[ndx].Cells[1].Value.ToString();
string Type = items_dataGridView.Rows[ndx].Cells[2].Value.ToString();
string command = items_dataGridView.Rows[ndx].Cells[3].Value.ToString();
Key key1;
if (Enum.TryParse(k1, out key1))
if (!Enum.IsDefined(typeof(Key), key1)) return;
Keys key2;
if (Enum.TryParse(k2, out key2))
if (!Enum.IsDefined(typeof(Keys), key2)) return;
V.CommandList type;
if (Enum.TryParse(Type, out type))
if (!Enum.IsDefined(typeof(V.CommandList), type)) return;
int ndx2 = ItemsExist(key1, key2, type, command);
if (ndx2 == -1) return;
V.Key_1.RemoveAt(ndx2);
V.Key_2.RemoveAt(ndx2);
V.Type.RemoveAt(ndx2);
V.Commands.RemoveAt(ndx2);
if (Pup_data())
{
V.saveConfig();
}
}
private int ItemsExist(Key key, Keys keys, V.CommandList type, string command)
{
for (int i = 0; i < V.Key_1.Count; i++)
{
if (V.Key_1[i] == key && V.Key_2[i] == keys && V.Type[i] == type && V.Commands[i] == command)
{
return i;
}
}
return -1;
}
private void Form1_Resize(object sender, EventArgs e)
{
if (starting) return;
if (this.WindowState == FormWindowState.Minimized)
{
Hide();
mini_notifyIcon.Visible = true;
mini_notifyIcon.ShowBalloonTip(100);
}
if (this.WindowState == FormWindowState.Normal)
{
this.ShowInTaskbar = true;
}
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
Show();
this.WindowState = FormWindowState.Normal;
mini_notifyIcon.Visible = false;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (!fully_exit)
{
mini_notifyIcon.Visible = true;
this.Hide();
mini_notifyIcon.ShowBalloonTip(100);
e.Cancel = true;
}
}
private static void SetStartup()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(StartupKey, true);
key.SetValue(StartupValue, Application.ExecutablePath.ToString());
}
private static void UnSetStartup()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(StartupKey, true);
key.DeleteValue(StartupValue);
}
private static bool Startup_Exist()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(StartupKey, true);
return key.GetValueNames().Contains(StartupValue);
}
private void startup_checkBox_CheckStateChanged(object sender, EventArgs e)
{
if (startup_checkBox.Checked)
{
SetStartup();
}
else
{
UnSetStartup();
}
}
private void exit_button_Click(object sender, EventArgs e)
{
fully_exit = true;
Application.Exit();
}
}
}