Skip to content

Commit

Permalink
Documentation and beautification
Browse files Browse the repository at this point in the history
  • Loading branch information
gmamaladze committed Jan 24, 2018
1 parent 5f97055 commit 547e453
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 79 deletions.
File renamed without changes.
File renamed without changes.
9 changes: 5 additions & 4 deletions examples/ConsoleHook/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>

<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
36 changes: 36 additions & 0 deletions examples/ConsoleHook/DetectCombinations.cs
Original file line number Diff line number Diff line change
@@ -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<Combination, Action>
{
//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);
}
}
}
31 changes: 31 additions & 0 deletions examples/ConsoleHook/DetectSequences.cs
Original file line number Diff line number Diff line change
@@ -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, Action>
{
{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);
}

}
}
15 changes: 15 additions & 0 deletions examples/ConsoleHook/LogKeys.cs
Original file line number Diff line number Diff line change
@@ -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();
};
}
}
75 changes: 3 additions & 72 deletions examples/ConsoleHook/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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, Action>
{
{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<Combination, Action>
{
//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();
};
}
}
}
9 changes: 6 additions & 3 deletions examples/ConsoleHook/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")]
72 changes: 72 additions & 0 deletions examples/ConsoleHook/QuickStart.cs
Original file line number Diff line number Diff line change
@@ -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<Combination, Action>
{
{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, Action>
{
{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<Sequence, Action>
{
{exitVim, ()=>Console.WriteLine("No!")},
{rename, ()=>Console.WriteLine("rename2")},
{exitReally, ()=>Console.WriteLine("Ok.")},
};

Hook.GlobalEvents().OnSequence(assignment);
}

}
}
11 changes: 11 additions & 0 deletions keycomb.md
Original file line number Diff line number Diff line change
@@ -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");
```


Binary file added keysequence.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 547e453

Please sign in to comment.