using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace ElectronNET.API
{
///
/// Detect keyboard events when the application does not have keyboard focus.
///
public sealed class GlobalShortcut
{
private static GlobalShortcut _globalShortcut;
private static readonly object _syncRoot = new();
internal GlobalShortcut() { }
internal static GlobalShortcut Instance
{
get
{
if (_globalShortcut == null)
{
lock (_syncRoot)
{
if (_globalShortcut == null)
{
_globalShortcut = new GlobalShortcut();
}
}
}
return _globalShortcut;
}
}
private readonly Dictionary _shortcuts = new();
///
/// Registers a global shortcut of accelerator.
/// The callback is called when the registered shortcut is pressed by the user.
///
/// When the accelerator is already taken by other applications, this call will
/// silently fail.This behavior is intended by operating systems, since they don’t
/// want applications to fight for global shortcuts.
///
public void Register(string accelerator, Action function)
{
if (!_shortcuts.ContainsKey(accelerator))
{
_shortcuts.Add(accelerator, function);
BridgeConnector.Off("globalShortcut-pressed");
BridgeConnector.On("globalShortcut-pressed", (shortcut) =>
{
if (_shortcuts.ContainsKey(shortcut))
{
_shortcuts[shortcut.ToString()]();
}
});
BridgeConnector.Emit("globalShortcut-register", accelerator);
}
}
///
/// When the accelerator is already taken by other applications,
/// this call will still return false. This behavior is intended by operating systems,
/// since they don’t want applications to fight for global shortcuts.
///
/// Whether this application has registered accelerator.
public Task IsRegisteredAsync(string accelerator) => BridgeConnector.OnResult("globalShortcut-isRegistered", "globalShortcut-isRegisteredCompleted", accelerator);
///
/// Unregisters the global shortcut of accelerator.
///
public void Unregister(string accelerator)
{
_shortcuts.Remove(accelerator);
BridgeConnector.Emit("globalShortcut-unregister", accelerator);
}
///
/// Unregisters all of the global shortcuts.
///
public void UnregisterAll()
{
_shortcuts.Clear();
BridgeConnector.Emit("globalShortcut-unregisterAll");
}
}
}