forked from kurumpa/dotSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsForm.cs
More file actions
87 lines (77 loc) · 2.58 KB
/
Copy pathSettingsForm.cs
File metadata and controls
87 lines (77 loc) · 2.58 KB
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
using System;
using System.Diagnostics;
using System.Windows.Forms;
namespace dotSwitcher
{
public partial class SettingsForm : Form
{
bool keyIsSet;
KeyboardHook kbdHook;
TextBox currentTextBox;
Settings settings;
KeyboardEventArgs currentHotkey;
public SettingsForm(Settings settings)
{
this.settings = settings;
currentTextBox = null;
kbdHook = new KeyboardHook();
kbdHook.KeyboardEvent += kbdHook_KeyboardEvent;
InitializeComponent();
InitializeValues();
}
void kbdHook_KeyboardEvent(object sender, KeyboardEventArgs e)
{
if (currentTextBox != null)
{
var vk = e.KeyCode;
if (vk != Keys.LMenu && vk != Keys.RMenu
&& vk != Keys.LWin && vk != Keys.RWin
&& vk != Keys.LShiftKey && vk != Keys.RShiftKey
&& vk != Keys.LControlKey && vk != Keys.RControlKey)
{
e.Handled = true;
}
Invoke((MethodInvoker)delegate { currentTextBox.Text = e.ToString(); });
currentHotkey = e;
}
}
private void InitializeValues()
{
shortcutTextBox.LostFocus += unsetCurrentInput;
shortcutTextBox.Leave += unsetCurrentInput;
shortcutTextBox.GotFocus += setCurrentInput;
shortcutTextBox.Enter += setCurrentInput;
shortcutTextBox.Text = settings.SwitchHotkey.ToString();
}
private void SettingsForm_Load(object sender, EventArgs e)
{
kbdHook.Start();
}
private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
{
kbdHook.Stop();
}
private void setCurrentInput(object sender, EventArgs e)
{
currentTextBox = (TextBox)sender;
}
private void unsetCurrentInput(object sender, EventArgs e)
{
currentTextBox = null;
}
private void shortcutTextBox_KeyUp(object sender, KeyEventArgs e)
{
settings.SwitchHotkey = currentHotkey;
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (settings.SwitchHotkey.Alt || settings.SwitchHotkey.Win)
{
MessageBox.Show("Sorry, win+ and alt+ hotkeys are not supported yet");
return;
}
DialogResult = DialogResult.OK;
Close();
}
}
}