diff --git a/MouseKeyHook/Chord.cs b/MouseKeyHook/Implementation/Chord.cs similarity index 100% rename from MouseKeyHook/Chord.cs rename to MouseKeyHook/Implementation/Chord.cs diff --git a/MouseKeyHook/KeysExtensions.cs b/MouseKeyHook/Implementation/KeysExtensions.cs similarity index 100% rename from MouseKeyHook/KeysExtensions.cs rename to MouseKeyHook/Implementation/KeysExtensions.cs diff --git a/examples/ConsoleHook/App.config b/examples/ConsoleHook/App.config index 88fa402..51fffc7 100644 --- a/examples/ConsoleHook/App.config +++ b/examples/ConsoleHook/App.config @@ -1,6 +1,7 @@ - + + - - - + + + \ No newline at end of file diff --git a/examples/ConsoleHook/DetectCombinations.cs b/examples/ConsoleHook/DetectCombinations.cs new file mode 100644 index 0000000..7b09e6a --- /dev/null +++ b/examples/ConsoleHook/DetectCombinations.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using System.Windows.Forms; +using Gma.System.MouseKeyHook; + +namespace ConsoleHook +{ + internal class DetectCombinations + { + public static void Do(AutoResetEvent quit) + { + var map = new Dictionary + { + //Specify which key combinations to detect and action - what to do if detected. + //You can create a key combinations directly from string or ... + {Combination.FromString("A+B+C") , () => Console.WriteLine(":-)")}, + //... or alternatively you can use builder methods + {Combination.TriggeredBy(Keys.F).With(Keys.E).With(Keys.D) , () => Console.WriteLine(":-D")}, + {Combination.FromString("Alt+A") , () => Console.WriteLine(":-P")}, + {Combination.FromString("Control+Shift+Z") , () => Console.WriteLine(":-/")}, + {Combination.FromString("Escape") , () => quit.Set()}, + }; + + Console.WriteLine("Detecting following combinations:"); + foreach (var combination in map.Keys) + { + Console.WriteLine("\t{0}",combination); + } + Console.WriteLine("Press ESC to exit."); + + //Actual loop + Hook.GlobalEvents().OnCombination(map); + } + } +} \ No newline at end of file diff --git a/examples/ConsoleHook/DetectSequences.cs b/examples/ConsoleHook/DetectSequences.cs new file mode 100644 index 0000000..3fddc76 --- /dev/null +++ b/examples/ConsoleHook/DetectSequences.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Threading; +using Gma.System.MouseKeyHook; + +namespace ConsoleHook +{ + internal class DetectSequences + { + public static void Do(AutoResetEvent quit) + { + var map = new Dictionary + { + {Sequence.FromString("Control+Z,B") , Console.WriteLine}, + {Sequence.FromString("Control+Z,Z") , Console.WriteLine}, + {Sequence.FromString("Escape,Escape,Escape") , () => quit.Set()}, + }; + + Console.WriteLine("Detecting following combinations:"); + foreach (var sequence in map.Keys) + { + Console.WriteLine("\t{0}", sequence); + } + Console.WriteLine("Press 3 x ESC (three times) to exit."); + + //Actual loop + Hook.GlobalEvents().OnSequence(map); + } + + } +} \ No newline at end of file diff --git a/examples/ConsoleHook/LogKeys.cs b/examples/ConsoleHook/LogKeys.cs new file mode 100644 index 0000000..6ab077e --- /dev/null +++ b/examples/ConsoleHook/LogKeys.cs @@ -0,0 +1,15 @@ +using System; +using System.Threading; +using Gma.System.MouseKeyHook; + +internal class LogKeys +{ + public static void Do(AutoResetEvent quit) + { + Hook.GlobalEvents().KeyPress += (sender, e) => + { + Console.Write(e.KeyChar); + if (e.KeyChar == 'q') quit.Set(); + }; + } +} \ No newline at end of file diff --git a/examples/ConsoleHook/Program.cs b/examples/ConsoleHook/Program.cs index ebb60e5..f3e76cc 100644 --- a/examples/ConsoleHook/Program.cs +++ b/examples/ConsoleHook/Program.cs @@ -4,12 +4,10 @@ using System.Text; using System.Threading; using System.Threading.Tasks; -using System.Windows.Forms; -using Gma.System.MouseKeyHook; namespace ConsoleHook { - class Program + internal class Program { private static void Main(string[] args) { @@ -37,79 +35,12 @@ private static void Main(string[] args) } Console.WriteLine("--------------------------------------------------"); action(quit); - - while (!quit.WaitOne(10)) - Application.DoEvents(); - ; } + private static void Exit(AutoResetEvent quit) { quit.Set(); } } - - internal class DetectSequences - { - public static void Do(AutoResetEvent quit) - { - var map = new Dictionary - { - {Sequence.FromString("Control+Z,B") , Console.WriteLine}, - {Sequence.FromString("Control+Z,Z") , Console.WriteLine}, - {Sequence.FromString("Escape,Escape,Escape") , () => quit.Set()}, - }; - - Console.WriteLine("Detecting following combinations:"); - foreach (var sequence in map.Keys) - { - Console.WriteLine("\t{0}", sequence); - } - Console.WriteLine("Press 3 x ESC (three times) to exit."); - - //Actual loop - Hook.GlobalEvents().OnSequence(map); - } - - } -} - - internal class DetectCombinations - { - public static void Do(AutoResetEvent quit) - { - var map = new Dictionary - { - //Specify which key combinations to detect and action - what to do if detected. - //You can create a key combinations directly from string or ... - {Combination.FromString("A+B+C") , () => Console.WriteLine(":-)")}, - //... or alternatively you can use builder methods - {Combination.TriggeredBy(Keys.F).With(Keys.E).With(Keys.D) , () => Console.WriteLine(":-D")}, - {Combination.FromString("Alt+A") , () => Console.WriteLine(":-P")}, - {Combination.FromString("Control+Shift+Z") , () => Console.WriteLine(":-/")}, - {Combination.FromString("Escape") , () => quit.Set()}, - }; - - Console.WriteLine("Detecting following combinations:"); - foreach (var combination in map.Keys) - { - Console.WriteLine("\t{0}",combination); - } - Console.WriteLine("Press ESC to exit."); - - //Actual loop - Hook.GlobalEvents().OnCombination(map); - } - } - - internal class LogKeys - { - public static void Do(AutoResetEvent quit) - { - Hook.GlobalEvents().KeyPress += (sender, e) => - { - Console.Write(e.KeyChar); - if (e.KeyChar == 'q') quit.Set(); - }; - } - } +} \ No newline at end of file diff --git a/examples/ConsoleHook/Properties/AssemblyInfo.cs b/examples/ConsoleHook/Properties/AssemblyInfo.cs index 93db408..d179696 100644 --- a/examples/ConsoleHook/Properties/AssemblyInfo.cs +++ b/examples/ConsoleHook/Properties/AssemblyInfo.cs @@ -1,5 +1,8 @@ -using System.Reflection; -using System.Runtime.CompilerServices; +// This code is distributed under MIT license. +// Copyright (c) 2010-2018 George Mamaladze +// See license.txt or http://opensource.org/licenses/mit-license.php + +using System.Reflection; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following @@ -33,4 +36,4 @@ // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/examples/ConsoleHook/QuickStart.cs b/examples/ConsoleHook/QuickStart.cs new file mode 100644 index 0000000..9e71333 --- /dev/null +++ b/examples/ConsoleHook/QuickStart.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Gma.System.MouseKeyHook; + +namespace ConsoleHook +{ + class QuickStart + { + public void Do() + { + //1. Define key combinations + var undo = Combination.FromString("Control+Z"); + //Using builder method + var undo2 = Combination.TriggeredBy(Keys.Z).With(Keys.Control); + var fullScreen = Combination.FromString("Shift+Alt+Enter"); + + //2. Define actions + Action actionUndo = DoSomething; + Action actionFullScreen = () => { Console.WriteLine("You Pressed FULL SCREEN"); }; + + void DoSomething() + { + Console.WriteLine("You pressed UNDO"); + } + + //3. Assign actions to key combinations + var assignment = new Dictionary + { + {undo, actionUndo}, + {fullScreen, actionFullScreen} + }; + + //4. Install listener + Hook.GlobalEvents().OnCombination(assignment); + } + + public void DoCompact() + { + void DoSomething() + { + Console.WriteLine("You pressed UNDO"); + } + + Hook.GlobalEvents().OnCombination(new Dictionary + { + {Combination.FromString("Control+Z"), DoSomething}, + {Combination.FromString("Shift+Alt+Enter"), () => { Console.WriteLine("You Pressed FULL SCREEN"); }} + }); + } + + public void DoSequence() + { + var exitVim = Sequence.FromString("Shift+Z,Z"); + var rename = Sequence.FromString("Control+R,R"); + var exitReally = Sequence.FromString("Escape,Escape,Escape"); + + var assignment = new Dictionary + { + {exitVim, ()=>Console.WriteLine("No!")}, + {rename, ()=>Console.WriteLine("rename2")}, + {exitReally, ()=>Console.WriteLine("Ok.")}, + }; + + Hook.GlobalEvents().OnSequence(assignment); + } + + } +} diff --git a/keycomb.md b/keycomb.md new file mode 100644 index 0000000..7cd2f0f --- /dev/null +++ b/keycomb.md @@ -0,0 +1,11 @@ +#Detecting Key Combinations and Seuqnces + +##Quickstart + +Assuming your goal is to detect whenever some key combination, shortcut is pressed and perform some action as a response. Use class `Combination` to describe key combinations. +```csharp +var undo = Combination.FromString("Control+Z"); +var fullScreen = Combination.FromString("Shit+Alt+Enter"); +``` + + diff --git a/keysequence.png b/keysequence.png new file mode 100644 index 0000000..6df7103 Binary files /dev/null and b/keysequence.png differ