From ef270a1aa4cdc1ae8e3df8ebda91847891125103 Mon Sep 17 00:00:00 2001 From: SpoinkyNL Date: Wed, 17 Feb 2016 17:32:19 +0100 Subject: [PATCH] Updated keyboard hook and TypeWave, fixes #19 --- Artemis/Artemis/Artemis.csproj | 9 +-- Artemis/Artemis/Models/MainModel.cs | 4 ++ .../Modules/Effects/TypeWave/TypeWaveModel.cs | 57 ++++++++++--------- .../VolumeDisplay/VolumeDisplayModel.cs | 16 ++---- .../Utilities/Keyboard/KeyboardHook.cs | 31 ++++++++++ Artemis/Artemis/packages.config | 2 +- 6 files changed, 76 insertions(+), 43 deletions(-) create mode 100644 Artemis/Artemis/Utilities/Keyboard/KeyboardHook.cs diff --git a/Artemis/Artemis/Artemis.csproj b/Artemis/Artemis/Artemis.csproj index 8bd373750..12aa1c42e 100644 --- a/Artemis/Artemis/Artemis.csproj +++ b/Artemis/Artemis/Artemis.csproj @@ -101,6 +101,10 @@ ..\packages\CUE.NET.1.0.0\lib\net45\CUE.NET.dll True + + ..\packages\MouseKeyHook.5.4.0\lib\net40\Gma.System.MouseKeyHook.dll + True + ..\packages\Hardcodet.NotifyIcon.Wpf.1.0.5\lib\net451\Hardcodet.Wpf.TaskbarNotification.dll True @@ -127,10 +131,6 @@ ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll True - - ..\packages\Open.WinKeyboardHook.1.0.10.0\lib\net45\Open.WinKeyboardHook.dll - True - @@ -270,6 +270,7 @@ + diff --git a/Artemis/Artemis/Models/MainModel.cs b/Artemis/Artemis/Models/MainModel.cs index 56503a858..82a2b0417 100644 --- a/Artemis/Artemis/Models/MainModel.cs +++ b/Artemis/Artemis/Models/MainModel.cs @@ -8,6 +8,7 @@ using Artemis.Events; using Artemis.KeyboardProviders; using Artemis.Settings; using Artemis.Utilities.GameState; +using Artemis.Utilities.Keyboard; using Artemis.Utilities.Memory; using Caliburn.Micro; @@ -23,6 +24,7 @@ namespace Artemis.Models EffectModels = new List(); KeyboardProviders = ProviderHelper.GetKeyboardProviders(); GameStateWebServer = new GameStateWebServer(); + KeyboardHook = new KeyboardHook(); Events = events; Fps = 25; @@ -33,6 +35,8 @@ namespace Artemis.Models _processWorker.DoWork += ProcessWorker_DoWork; } + public KeyboardHook KeyboardHook { get; set; } + public EffectModel ActiveEffect { get; set; } public KeyboardProvider ActiveKeyboard { get; set; } public List EffectModels { get; set; } diff --git a/Artemis/Artemis/Modules/Effects/TypeWave/TypeWaveModel.cs b/Artemis/Artemis/Modules/Effects/TypeWave/TypeWaveModel.cs index 0c4d68303..35e737d4e 100644 --- a/Artemis/Artemis/Modules/Effects/TypeWave/TypeWaveModel.cs +++ b/Artemis/Artemis/Modules/Effects/TypeWave/TypeWaveModel.cs @@ -7,65 +7,66 @@ using System.Windows.Forms; using Artemis.KeyboardProviders.Logitech.Utilities; using Artemis.Models; using Artemis.Utilities; -using Open.WinKeyboardHook; +using Gma.System.MouseKeyHook; namespace Artemis.Modules.Effects.TypeWave { public class TypeWaveModel : EffectModel { + private readonly List _waves; + private Color _randomColor; + public TypeWaveModel(MainModel mainModel, TypeWaveSettings settings) : base(mainModel) { Name = "TypeWave"; - Waves = new List(); + _waves = new List(); + _randomColor = Color.Red; Settings = settings; - - // KeyboardIntercepter won't start untill the effect is active - KeyboardInterceptor = new KeyboardInterceptor(); } public TypeWaveSettings Settings { get; set; } - public List Waves { get; set; } - public KeyboardInterceptor KeyboardInterceptor { get; set; } public override void Dispose() { - KeyboardInterceptor.KeyUp -= HandleKeypress; - KeyboardInterceptor.StopCapturing(); + MainModel.KeyboardHook.Unsubscribe(HandleKeypress); } public override void Enable() { - KeyboardInterceptor.StartCapturing(); - KeyboardInterceptor.KeyUp += HandleKeypress; + // Listener won't start unless the effect is active + MainModel.KeyboardHook.Subscribe(HandleKeypress); } public override void Update() { - for (var i = 0; i < Waves.Count; i++) + if (Settings.IsRandomColors) + _randomColor = ColorHelpers.ShiftColor(_randomColor, 25); + + for (var i = 0; i < _waves.Count; i++) { // TODO: Get from settings var fps = 25; - Waves[i].Size += Settings.SpreadSpeed; + _waves[i].Size += Settings.SpreadSpeed; if (Settings.IsShiftColors) - Waves[i].Color = ColorHelpers.ShiftColor(Waves[i].Color, Settings.ShiftColorSpeed); + _waves[i].Color = ColorHelpers.ShiftColor(_waves[i].Color, Settings.ShiftColorSpeed); var decreaseAmount = 255/(Settings.TimeToLive/fps); - Waves[i].Color = Color.FromArgb(Waves[i].Color.A - decreaseAmount, Waves[i].Color.R, Waves[i].Color.G, - Waves[i].Color.B); + _waves[i].Color = Color.FromArgb(_waves[i].Color.A - decreaseAmount, _waves[i].Color.R, _waves[i].Color.G, + _waves[i].Color.B); - if (Waves[i].Color.A >= decreaseAmount) + if (_waves[i].Color.A >= decreaseAmount) continue; - Waves.RemoveAt(i); + _waves.RemoveAt(i); i--; } } public override Bitmap GenerateBitmap() { - if (Waves.Count == 0) + if (_waves.Count == 0) return null; var bitmap = new Bitmap(21, 6); @@ -78,17 +79,17 @@ namespace Artemis.Modules.Effects.TypeWave // Don't want a foreach, collection is changed in different thread // ReSharper disable once ForCanBeConvertedToForeach - for (var i = 0; i < Waves.Count; i++) + for (var i = 0; i < _waves.Count; i++) { - if (Waves[i].Size == 0) + if (_waves[i].Size == 0) continue; var path = new GraphicsPath(); - path.AddEllipse(Waves[i].Point.X - Waves[i].Size/2, Waves[i].Point.Y - Waves[i].Size/2, - Waves[i].Size, Waves[i].Size); + path.AddEllipse(_waves[i].Point.X - _waves[i].Size/2, _waves[i].Point.Y - _waves[i].Size/2, + _waves[i].Size, _waves[i].Size); var pthGrBrush = new PathGradientBrush(path) { - SurroundColors = new[] {Waves[i].Color}, + SurroundColors = new[] {_waves[i].Color}, CenterColor = Color.Transparent }; @@ -96,8 +97,8 @@ namespace Artemis.Modules.Effects.TypeWave pthGrBrush.FocusScales = new PointF(0.3f, 0.8f); g.FillPath(pthGrBrush, path); - g.DrawEllipse(new Pen(pthGrBrush, 1), Waves[i].Point.X - Waves[i].Size/2, - Waves[i].Point.Y - Waves[i].Size/2, Waves[i].Size, Waves[i].Size); + g.DrawEllipse(new Pen(pthGrBrush, 1), _waves[i].Point.X - _waves[i].Size/2, + _waves[i].Point.Y - _waves[i].Size/2, _waves[i].Size, _waves[i].Size); } } return bitmap; @@ -114,8 +115,8 @@ namespace Artemis.Modules.Effects.TypeWave if (keyMatch == null) return; - Waves.Add(Settings.IsRandomColors - ? new Wave(new Point(keyMatch.PosX, keyMatch.PosY), 0, ColorHelpers.GetRandomRainbowColor()) + _waves.Add(Settings.IsRandomColors + ? new Wave(new Point(keyMatch.PosX, keyMatch.PosY), 0, _randomColor) : new Wave(new Point(keyMatch.PosX, keyMatch.PosY), 0, ColorHelpers.ToDrawingColor(Settings.WaveColor))); } diff --git a/Artemis/Artemis/Modules/Overlays/VolumeDisplay/VolumeDisplayModel.cs b/Artemis/Artemis/Modules/Overlays/VolumeDisplay/VolumeDisplayModel.cs index b41ce66f1..b5b0925e2 100644 --- a/Artemis/Artemis/Modules/Overlays/VolumeDisplay/VolumeDisplayModel.cs +++ b/Artemis/Artemis/Modules/Overlays/VolumeDisplay/VolumeDisplayModel.cs @@ -3,21 +3,19 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; using System.Windows.Forms; using Artemis.Models; +using Gma.System.MouseKeyHook; using NAudio.CoreAudioApi; -using Open.WinKeyboardHook; namespace Artemis.Modules.Overlays.VolumeDisplay { public class VolumeDisplayModel : OverlayModel { + private IKeyboardMouseEvents _mGlobalHook; + public VolumeDisplayModel(MainModel mainModel, VolumeDisplaySettings settings) : base(mainModel) { Settings = settings; Name = "VolumeDisplay"; - - KeyboardInterceptor = new KeyboardInterceptor(); - KeyboardInterceptor.StartCapturing(); - Enabled = Settings.Enabled; VolumeDisplay = new VolumeDisplay(mainModel, settings); @@ -26,18 +24,16 @@ namespace Artemis.Modules.Overlays.VolumeDisplay public VolumeDisplay VolumeDisplay { get; set; } public VolumeDisplaySettings Settings { get; set; } - public KeyboardInterceptor KeyboardInterceptor { get; set; } public override void Dispose() { - KeyboardInterceptor.KeyUp -= HandleKeypress; - KeyboardInterceptor.StopCapturing(); + MainModel.KeyboardHook.Unsubscribe(HandleKeypress); } public override void Enable() { - KeyboardInterceptor.StartCapturing(); - KeyboardInterceptor.KeyUp += HandleKeypress; + // Listener won't start unless the effect is active + MainModel.KeyboardHook.Subscribe(HandleKeypress); } public override void Update() diff --git a/Artemis/Artemis/Utilities/Keyboard/KeyboardHook.cs b/Artemis/Artemis/Utilities/Keyboard/KeyboardHook.cs new file mode 100644 index 000000000..3cea93995 --- /dev/null +++ b/Artemis/Artemis/Utilities/Keyboard/KeyboardHook.cs @@ -0,0 +1,31 @@ +using System.Windows.Forms; +using Gma.System.MouseKeyHook; + +namespace Artemis.Utilities.Keyboard +{ + public class KeyboardHook + { + private IKeyboardMouseEvents _mGlobalHook; + public int Subscriptions { get; set; } + + public void Subscribe(KeyEventHandler handleKeypress) + { + if (Subscriptions < 1) + _mGlobalHook = Hook.GlobalEvents(); + + _mGlobalHook.KeyDown += handleKeypress; + Subscriptions++; + } + + public void Unsubscribe(KeyEventHandler handleKeypress) + { + _mGlobalHook.KeyDown -= handleKeypress; + Subscriptions--; + + if (Subscriptions >= 1) + return; + _mGlobalHook.Dispose(); + _mGlobalHook = null; + } + } +} \ No newline at end of file diff --git a/Artemis/Artemis/packages.config b/Artemis/Artemis/packages.config index fa7dbeeb8..0e7520c5b 100644 --- a/Artemis/Artemis/packages.config +++ b/Artemis/Artemis/packages.config @@ -11,8 +11,8 @@ + - \ No newline at end of file