From a99af8410d017ad4e053a48ba0b17f919c0ca708 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sat, 7 Feb 2015 21:20:36 +0200 Subject: [PATCH] custom switch hotkey alt+ and win+ are not supported for now --- .gitignore | 1 + LowLevelAdapter.cs | 21 ++- MainForm.cs | 1 - SettingsForm.cs | 5 + Switcher.cs | 347 +++++++++++++++++++++++---------------------- 5 files changed, 189 insertions(+), 186 deletions(-) diff --git a/.gitignore b/.gitignore index f63a74d..5dcdaca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /bin/* /obj/* /*.suo +test.cmd diff --git a/LowLevelAdapter.cs b/LowLevelAdapter.cs index 5a5da25..6773a64 100644 --- a/LowLevelAdapter.cs +++ b/LowLevelAdapter.cs @@ -117,20 +117,17 @@ public static void SendKeyPress(Keys vkCode, bool shift = false) } - public static bool[] ReleasePressedKeys() + public static void ReleasePressedFnKeys() { // temp solution - return new bool[8] - { - ReleasePressedKey(Keys.LMenu, true), - ReleasePressedKey(Keys.RMenu, true), - ReleasePressedKey(Keys.LWin, true), - ReleasePressedKey(Keys.RWin, true), - ReleasePressedKey(Keys.RControlKey, false), - ReleasePressedKey(Keys.LControlKey, false), - ReleasePressedKey(Keys.LShiftKey, false), - ReleasePressedKey(Keys.RShiftKey, false) - }; + //ReleasePressedKey(Keys.LMenu, true), + //ReleasePressedKey(Keys.RMenu, true), + //ReleasePressedKey(Keys.LWin, true), + //ReleasePressedKey(Keys.RWin, true), + ReleasePressedKey(Keys.RControlKey, false); + ReleasePressedKey(Keys.LControlKey, false); + ReleasePressedKey(Keys.LShiftKey, false); + ReleasePressedKey(Keys.RShiftKey, false); } public static bool ReleasePressedKey(Keys keyCode, bool releaseTwice) diff --git a/MainForm.cs b/MainForm.cs index 7213ec4..1cd66e3 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -31,7 +31,6 @@ public SysTrayApp(Switcher engine) trayIcon.ContextMenu = trayMenu; trayIcon.Visible = true; - } private void InitSettings() diff --git a/SettingsForm.cs b/SettingsForm.cs index 4e5d212..3431bfd 100644 --- a/SettingsForm.cs +++ b/SettingsForm.cs @@ -75,6 +75,11 @@ private void shortcutTextBox_KeyUp(object sender, KeyEventArgs e) 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(); } diff --git a/Switcher.cs b/Switcher.cs index 9ce7c36..2ed7881 100644 --- a/Switcher.cs +++ b/Switcher.cs @@ -1,173 +1,174 @@ -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 Error; - private List currentWord = new List(); - private KeyboardHook kbdHook; - private MouseHook mouseHook; - - public KeyboardEventArgs SwitchHotkey { get; set; } - public KeyboardEventArgs ConvertSelectionHotkey { get; set; } - - - public Switcher() - { - 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); - } +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 Error; + private List currentWord = new List(); + private KeyboardHook kbdHook; + private MouseHook mouseHook; + + public KeyboardEventArgs SwitchHotkey { get; set; } + public KeyboardEventArgs ConvertSelectionHotkey { get; set; } + + + public Switcher() + { + 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 ctrl = evtData.Control; + var alt = evtData.Alt; var win = evtData.Win; return ctrl || alt || win; - } - - private bool HaveTrackingKeys(KeyboardEventArgs evtData) - { + } + + 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 || + 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(SwitchHotkey)) - { - ConvertLast(); - evtData.Handled = true; - return; - } - - if (evtData.Equals(ConvertSelectionHotkey)) - { - ConvertSelection(); + } + + private void OnKeyPress(KeyboardEventArgs evtData) + { + var vkCode = evtData.KeyCode; + + if (evtData.Equals(SwitchHotkey)) + { + ConvertLast(); + evtData.Handled = true; + return; + } + + if (evtData.Equals(ConvertSelectionHotkey)) + { + ConvertSelection(); } - if (this.HaveTrackingKeys(evtData)) + 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.Last().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() + 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.Last().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.Back, word.Count); - + var backspaces = Enumerable.Repeat(Keys.Back, word.Count); + foreach (var vkCode in backspaces) { LowLevelAdapter.SendKeyPress(vkCode, false); } // funny fix for my skype Thread.Sleep(20); @@ -175,22 +176,22 @@ private void ConvertLast() 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; - } - } - -} + } + } + + public void Dispose() + { + Stop(); + } + } + + public class SwitcherErrorArgs : EventArgs + { + public Exception Error { get; private set; } + public SwitcherErrorArgs(Exception ex) + { + Error = ex; + } + } + +}