Skip to content

Commit

Permalink
Code cleanup and license headers.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmamaladze committed Jan 12, 2018
1 parent 90ab329 commit 6bdd2d1
Show file tree
Hide file tree
Showing 36 changed files with 327 additions and 371 deletions.
9 changes: 5 additions & 4 deletions ConsoleHook.Rx/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>
10 changes: 6 additions & 4 deletions ConsoleHook.Rx/DetectCombinations.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
// 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;
using System.Reactive.Linq;
using System.Threading;
using System.Windows.Forms;
Expand Down Expand Up @@ -26,16 +30,14 @@ public static void Do(AutoResetEvent quit)
.Matching(triggers)
.ForEachAsync(trigger =>
{
if (trigger==quitTrigger) quit.Set();
if (trigger == quitTrigger) quit.Set();
Console.WriteLine(trigger);
});

Console.WriteLine("Press Control+Q to quit.");
Console.WriteLine("Monitoring folowing combinations:");
foreach (var name in triggers)
{
Console.WriteLine("\t" + name);
}
}
}
}
21 changes: 12 additions & 9 deletions ConsoleHook.Rx/DetectSequences.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
using System;
// 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;
using System.Reactive.Linq;
using System.Threading;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
using MouseKeyHook.Rx;
using System.Collections.Generic;

namespace ConsoleHook.Rx
{
internal class DetectSequences
{
public static void Do(AutoResetEvent quit)
{
var map = new []
var expected = new HashSet<KeySequence>
{
new Sequence<Keys>(Keys.Control, Keys.Q),
new Sequence<Keys>(Keys.Alt, Keys.Shift),
new KeySequence(Keys.Control, Keys.Q),
new KeySequence(Keys.Alt, Keys.Shift)
};

Hook
.GlobalEvents()
.KeyDownObservable()
.Sequences(2,3)
//.Matching(map)
//.Matching(expected)
.ForEachAsync(sequence =>
{
//if (sequence.Id == "Control+Q") quit.Set();
if (sequence.ToString() == "Control+Q") quit.Set();
Console.WriteLine(sequence);
});

Console.WriteLine("Press Control+Q to quit.");
Console.WriteLine("Monitoring folowing sequences:");
foreach (var name in map)
{
foreach (var name in expected)
Console.WriteLine("\t" + name);
}
}
}
}
27 changes: 11 additions & 16 deletions ConsoleHook.Rx/Program.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
using System;
// 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gma.System.MouseKeyHook;
using MouseKeyHook;
using System.Reactive.Linq;
using System.Threading;
using MouseKeyHook.Rx;
using System.Windows.Forms;

namespace ConsoleHook.Rx
{
class Program
internal class Program
{
static void Main(string[] args)
private static void Main(string[] args)
{
var quit = new AutoResetEvent(false);

var selector = new Dictionary<string, Action<AutoResetEvent>>()
var selector = new Dictionary<string, Action<AutoResetEvent>>
{
{"1. Detect key sequence", DetectSequences.Do},
{"2. Detect key combinationss", DetectCombinations.Do},
Expand All @@ -31,26 +29,23 @@ static void Main(string[] args)
{
Console.WriteLine("Please select one of these:");
foreach (var selectorKey in selector.Keys)
{
Console.WriteLine(selectorKey);
}
var ch = Console.ReadKey(true).KeyChar;
action = selector
.Where(p => p.Key.StartsWith(ch.ToString()))
.Select(p=>p.Value).FirstOrDefault();
.Select(p => p.Value).FirstOrDefault();
}
Console.WriteLine("--------------------------------------------------");
action(quit);

while (!quit.WaitOne(100))
{
Application.DoEvents();
};
;
}

private static void Exit(AutoResetEvent quit)
{
quit.Set();
}
}
}
}
9 changes: 6 additions & 3 deletions ConsoleHook.Rx/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")]
1 change: 1 addition & 0 deletions ConsoleHook.Rx/packages.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>

<packages>
<package id="System.Reactive" version="3.1.1" targetFramework="net452" />
<package id="System.Reactive.Core" version="3.1.1" targetFramework="net452" />
Expand Down
6 changes: 2 additions & 4 deletions Demo/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.ComponentModel;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
using Gma.System.MouseKeyHook.Implementation;

namespace Demo
{
Expand Down Expand Up @@ -64,7 +63,6 @@ private void Subscribe(IKeyboardMouseEvents events)
m_Events.MouseDownExt += HookManager_Supress;
else
m_Events.MouseDown += OnMouseDown;

}

private void Unsubscribe()
Expand Down Expand Up @@ -163,7 +161,7 @@ private void HookManager_MouseWheel(object sender, MouseEventArgs e)
{
labelWheel.Text = string.Format("Wheel={0:000}", e.Delta);
}

private void HookManager_MouseWheelExt(object sender, MouseEventExtArgs e)
{
labelWheel.Text = string.Format("Wheel={0:000}", e.Delta);
Expand Down Expand Up @@ -213,7 +211,7 @@ private void checkBoxSuppressMouse_CheckedChanged(object sender, EventArgs e)
{
if (m_Events == null) return;

if (((CheckBox)sender).Checked)
if (((CheckBox) sender).Checked)
{
m_Events.MouseDown -= OnMouseDown;
m_Events.MouseDownExt += HookManager_Supress;
Expand Down
8 changes: 6 additions & 2 deletions MouseKeyHook.Rx/KeyEvent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// 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.Windows.Forms;
using Gma.System.MouseKeyHook.Implementation;

Expand All @@ -7,8 +11,8 @@ public struct KeyEvent
{
public KeyEvent(Keys keyCode, KeyEventKind kind = KeyEventKind.Down)
{
this.KeyCode = keyCode;
this.Kind = kind;
KeyCode = keyCode;
Kind = kind;
}

public KeyEventKind Kind { get; }
Expand Down
6 changes: 5 additions & 1 deletion MouseKeyHook.Rx/KeyEventKind.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
namespace MouseKeyHook.Rx
// This code is distributed under MIT license.
// Copyright (c) 2010-2018 George Mamaladze
// See license.txt or http://opensource.org/licenses/mit-license.php

namespace MouseKeyHook.Rx
{
public enum KeyEventKind
{
Expand Down
40 changes: 21 additions & 19 deletions MouseKeyHook.Rx/KeyObserverExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
using System;
// 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;
using System.Collections.Generic;
using System.Linq;
using Gma.System.MouseKeyHook;
using srx=System.Reactive.Linq;
using System.Reactive.Linq;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
using Gma.System.MouseKeyHook.Implementation;
using srx = System.Reactive.Linq;

namespace MouseKeyHook.Rx
{
public static class KeyObserverExtensions
{
public static IObservable<Keys> KeyDownObservable(this IKeyboardEvents source)
{
return srx
.Observable
return Observable
.FromEventPattern<KeyEventArgs>(source, "KeyDown")
.Select(ep => ep.EventArgs.KeyCode);
}

public static IObservable<Keys> KeyUpObservable(this IKeyboardEvents source)
{
return srx
.Observable
return Observable
.FromEventPattern<KeyEventArgs>(source, "KeyDown")
.Select(ep => ep.EventArgs.KeyCode);
}
Expand All @@ -31,11 +33,11 @@ public static IObservable<Keys> KeyUpObservable(this IKeyboardEvents source)
public static IObservable<KeyEvent> UpDownEvents(this IKeyboardEvents source)
{
return source
.KeyDownObservable()
.Select(key=>key.Down())
.Merge(source
.KeyUpObservable()
.Select(key=>key.Down()));
.KeyDownObservable()
.Select(key => key.Down())
.Merge(source
.KeyUpObservable()
.Select(key => key.Down()));
}


Expand All @@ -57,31 +59,32 @@ public static IObservable<Trigger> MatchingLongest(this IObservable<Keys> source
var sortedTriggers = triggers
.GroupBy(t => t.TriggerKey)
.Select(group => new KeyValuePair<Keys, IEnumerable<Trigger>>(group.Key, group.OrderBy(t => -t.Length)))
.ToDictionary(pair=>pair.Key, pair=>pair.Value);
.ToDictionary(pair => pair.Key, pair => pair.Value);

return source
.Where(keyCode=> sortedTriggers.ContainsKey(keyCode))
.Where(keyCode => sortedTriggers.ContainsKey(keyCode))
.WithState()
.Select(se => sortedTriggers[se.KeyCode].First(se.Matches));
}

public static IObservable<Sequence<T>> Sequences<T>(this IObservable<T> source, int minLength, int maxLength)
{
return Enumerable
.Range(minLength, maxLength)
.Range(minLength, maxLength-1)
.Select(n => source
.Buffer(n, 1))
.Merge()
.Select(s => new Sequence<T>(s.ToArray()));
}

public static IObservable<IEnumerable<T>> Matching<T>(this IObservable<T> source, IEnumerable<IEnumerable<T>> whitelist)
public static IObservable<IEnumerable<T>> Matching<T>(this IObservable<T> source,
IEnumerable<IEnumerable<T>> whitelist)
{
var min = whitelist.Select(e => e.Count()).Min();
var max = whitelist.Select(e => e.Count()).Max();

return source.Sequences(min, max)
.SelectMany(sequence=>whitelist.Where(w=>w.SequenceEqual(sequence)));
.SelectMany(sequence => whitelist.Where(w => w.SequenceEqual(sequence)));
}

public static IObservable<Sequence<T>> Matching<T>(this IObservable<T> source, ISet<Sequence<T>> whitelist)
Expand All @@ -93,6 +96,5 @@ public static IObservable<Sequence<T>> Matching<T>(this IObservable<T> source, I
.Sequences(min, max)
.Where(whitelist.Contains);
}

}
}
}
9 changes: 6 additions & 3 deletions MouseKeyHook.Rx/KeySequence.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// 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.Linq;
using System.Windows.Forms;

namespace MouseKeyHook.Rx
{
public class KeySequence : Sequence<Keys>
{
public KeySequence(params Keys[] keys)
: base(keys.Select(k=>KeysExtensions.Normalize(k)).ToArray())
public KeySequence(params Keys[] keys)
: base(keys.Select(k => k.Normalize()).ToArray())
{

}
}
}
Loading

0 comments on commit 6bdd2d1

Please sign in to comment.