Small change to allow a global flag, to enable / disable hotkeys.

This commit is contained in:
Robin 2018-08-08 15:55:45 +02:00
commit a913242b0a

View file

@ -23,8 +23,6 @@
using System; using System;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Threading;
using Autofac.Features.AttributeFilters;
using Caliburn.Micro; using Caliburn.Micro;
using Dapplo.Addons; using Dapplo.Addons;
using Dapplo.Log; using Dapplo.Log;
@ -42,53 +40,67 @@ namespace Greenshot.Components
{ {
private static readonly LogSource Log = new LogSource(); private static readonly LogSource Log = new LogSource();
private readonly ICoreConfiguration _coreConfiguration; private readonly ICoreConfiguration _coreConfiguration;
private readonly SynchronizationContext _synchronizationContext;
private IDisposable _subscriptions; private IDisposable _subscriptions;
public HotkeyService(ICoreConfiguration coreConfiguration, public HotkeyService(ICoreConfiguration coreConfiguration)
[KeyFilter("ui")] SynchronizationContext synchronizationContext)
{ {
_coreConfiguration = coreConfiguration; _coreConfiguration = coreConfiguration;
_synchronizationContext = synchronizationContext; }
/// <summary>
/// If set to false, hotkey presses don't trigger the functionality
/// </summary>
public bool AreHotkeysActive { get; set; } = true;
/// <summary>
/// This can be used to disable or enable the triggering of hotkeys, depending in different logic
/// </summary>
/// <param name="keyboardHookEventArgs">KeyboardHookEventArgs</param>
/// <returns>bool</returns>
private bool HotkeyTrigger(KeyboardHookEventArgs keyboardHookEventArgs)
{
return AreHotkeysActive;
} }
public void Startup() public void Startup()
{ {
Log.Debug().WriteLine("Registering hotkeys"); Log.Debug().WriteLine("Registering hotkeys");
var syncedEvents = KeyboardHook.KeyboardEvents var syncedEvents = KeyboardHook.KeyboardEvents;
.Synchronize()
.ObserveOn(_synchronizationContext)
.SubscribeOn(_synchronizationContext);
// Region hotkey // Region hotkey
var regionHotkeyHandler = new HotKeyHandler(_coreConfiguration, nameof(ICoreConfiguration.RegionHotkey)); var regionHotkeyHandler = new HotKeyHandler(_coreConfiguration, nameof(ICoreConfiguration.RegionHotkey));
_subscriptions = syncedEvents _subscriptions = syncedEvents
.Where(regionHotkeyHandler) .Where(regionHotkeyHandler)
.Where(HotkeyTrigger)
.Subscribe(CaptureRegion, () => regionHotkeyHandler.Dispose()); .Subscribe(CaptureRegion, () => regionHotkeyHandler.Dispose());
// Fullscreen hotkey // Fullscreen hotkey
var fullScreenHotkeyHandler = new HotKeyHandler(_coreConfiguration, nameof(ICoreConfiguration.FullscreenHotkey)); var fullScreenHotkeyHandler = new HotKeyHandler(_coreConfiguration, nameof(ICoreConfiguration.FullscreenHotkey));
_subscriptions = syncedEvents _subscriptions = syncedEvents
.Where(fullScreenHotkeyHandler) .Where(fullScreenHotkeyHandler)
.Where(HotkeyTrigger)
.Subscribe(CaptureFullscreen, () => fullScreenHotkeyHandler.Dispose()); .Subscribe(CaptureFullscreen, () => fullScreenHotkeyHandler.Dispose());
// Last region hotkey // Last region hotkey
var lastRegionHotKeyHandler = new HotKeyHandler(_coreConfiguration, nameof(ICoreConfiguration.LastregionHotkey)); var lastRegionHotKeyHandler = new HotKeyHandler(_coreConfiguration, nameof(ICoreConfiguration.LastregionHotkey));
_subscriptions = syncedEvents _subscriptions = syncedEvents
.Where(lastRegionHotKeyHandler) .Where(lastRegionHotKeyHandler)
.Where(HotkeyTrigger)
.Subscribe(CaptureLast, () => lastRegionHotKeyHandler.Dispose()); .Subscribe(CaptureLast, () => lastRegionHotKeyHandler.Dispose());
// Window hotkey // Window hotkey
var windowHotKeyHandler = new HotKeyHandler(_coreConfiguration, nameof(ICoreConfiguration.WindowHotkey)); var windowHotKeyHandler = new HotKeyHandler(_coreConfiguration, nameof(ICoreConfiguration.WindowHotkey));
_subscriptions = syncedEvents _subscriptions = syncedEvents
.Where(windowHotKeyHandler) .Where(windowHotKeyHandler)
.Where(HotkeyTrigger)
.Subscribe(CaptureWindow, () => windowHotKeyHandler.Dispose()); .Subscribe(CaptureWindow, () => windowHotKeyHandler.Dispose());
// IE hotkey // IE hotkey
var ieHotKeyHandler = new HotKeyHandler(_coreConfiguration, nameof(ICoreConfiguration.IEHotkey)); var ieHotKeyHandler = new HotKeyHandler(_coreConfiguration, nameof(ICoreConfiguration.IEHotkey));
_subscriptions = syncedEvents _subscriptions = syncedEvents
.Where(ieHotKeyHandler) .Where(ieHotKeyHandler)
.Where(HotkeyTrigger)
.Subscribe(CaptureIe, () => ieHotKeyHandler.Dispose()); .Subscribe(CaptureIe, () => ieHotKeyHandler.Dispose());
Log.Debug().WriteLine("Started hotkeys"); Log.Debug().WriteLine("Started hotkeys");