forked from kurumpa/dotSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitcher.cs
More file actions
194 lines (170 loc) · 5.77 KB
/
Switcher.cs
File metadata and controls
194 lines (170 loc) · 5.77 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
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace dotSwitcher
{
public class Switcher : IDisposable
{
public event EventHandler<SwitcherErrorArgs> Error;
private List<KeyboardEventArgs> currentWord = new List<KeyboardEventArgs>();
private KeyboardHook kbdHook;
private MouseHook mouseHook;
private ISettings settings;
public Switcher(ISettings settings)
{
this.settings = settings;
kbdHook = new KeyboardHook();
kbdHook.KeyboardEvent += ProcessKeyPress;
mouseHook = new MouseHook();
mouseHook.MouseEvent += ProcessMousePress;
}
public static bool IsPrintable(KeyboardEventArgs evtData)
{
if (evtData.Alt || evtData.Control || evtData.Win) { return false; }
var keyCode = evtData.KeyCode;
if (keyCode >= Keys.D0 && keyCode <= Keys.Z) { return true; }
if (keyCode >= Keys.Oem1 && keyCode <= Keys.OemBackslash) { return true; }
return false;
}
public bool IsStarted()
{
return kbdHook.IsStarted() || mouseHook.IsStarted();
}
public void Start()
{
kbdHook.Start();
mouseHook.Start();
}
public void Stop()
{
kbdHook.Stop();
mouseHook.Stop();
}
// evtData.Handled must be set to true if the key is recognized as hotkey and doesn't need further processing
private void ProcessKeyPress(object sender, KeyboardEventArgs evtData)
{
try
{
OnKeyPress(evtData);
}
catch (Exception ex)
{
OnError(ex);
}
}
private void ProcessMousePress(object sender, EventArgs evtData)
{
try
{
BeginNewWord();
}
catch (Exception ex)
{
OnError(ex);
}
}
private bool HaveModifiers(KeyboardEventArgs evtData)
{
var ctrl = evtData.Control;
var alt = evtData.Alt;
var win = evtData.Win;
return ctrl || alt || win;
}
private bool HaveTrackingKeys(KeyboardEventArgs evtData)
{
var vkCode = evtData.KeyCode;
return evtData.KeyCode == Keys.ControlKey ||
vkCode == Keys.LControlKey ||
vkCode == Keys.RControlKey ||
// yes, don't interrupt the tracking on PrtSc!
vkCode == Keys.PrintScreen ||
vkCode == Keys.ShiftKey ||
vkCode == Keys.RShiftKey ||
vkCode == Keys.LShiftKey ||
vkCode == Keys.NumLock ||
vkCode == Keys.Scroll;
}
private void OnKeyPress(KeyboardEventArgs evtData)
{
var vkCode = evtData.KeyCode;
if (evtData.Equals(settings.SwitchHotkey))
{
ConvertLast();
evtData.Handled = true;
return;
}
//if (evtData.Equals(settings.ConvertSelectionHotkey))
//{
// ConvertSelection();
//}
if (this.HaveTrackingKeys(evtData))
return;
var notModified = !this.HaveModifiers(evtData);
if (vkCode == Keys.Space && notModified) { AddToCurrentWord(evtData); return; }
if (vkCode == Keys.Back && notModified) { RemoveLast(); return; }
if (IsPrintable(evtData))
{
if (GetPreviousVkCode() == Keys.Space) { BeginNewWord(); }
AddToCurrentWord(evtData);
return;
}
// default:
BeginNewWord();
}
private void OnError(Exception ex)
{
if (Error != null)
{
Error(this, new SwitcherErrorArgs(ex));
}
}
#region word manipulation
private Keys GetPreviousVkCode()
{
if (currentWord.Count == 0) { return Keys.None; }
return currentWord[currentWord.Count - 1].KeyCode;
}
private void BeginNewWord() { currentWord.Clear(); }
private void AddToCurrentWord(KeyboardEventArgs data) { currentWord.Add(data); }
private void RemoveLast()
{
if (currentWord.Count == 0) { return; }
currentWord.RemoveAt(currentWord.Count - 1);
}
#endregion
private void ConvertSelection()
{
throw new NotImplementedException();
}
private void ConvertLast()
{
LowLevelAdapter.ReleasePressedFnKeys();
var word = currentWord.ToList();
var backspaces = Enumerable.Repeat<Keys>(Keys.Back, word.Count);
foreach (var vkCode in backspaces) { LowLevelAdapter.SendKeyPress(vkCode, false); }
// Fix for skype
Thread.Sleep(settings.SwitchDelay);
LowLevelAdapter.SetNextKeyboardLayout();
foreach (var data in word)
{
LowLevelAdapter.SendKeyPress(data.KeyCode, data.Shift);
}
}
public void Dispose()
{
Stop();
}
}
public class SwitcherErrorArgs : EventArgs
{
public Exception Error { get; private set; }
public SwitcherErrorArgs(Exception ex)
{
Error = ex;
}
}
}