2017-10-21 04:37:01 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
|
|
namespace ElectronNET.API
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Detect keyboard events when the application does not have keyboard focus.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public sealed class GlobalShortcut
|
|
|
|
|
|
{
|
|
|
|
|
|
private static GlobalShortcut _globalShortcut;
|
2021-09-15 11:14:45 +02:00
|
|
|
|
private static readonly object _syncRoot = new();
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
|
|
|
|
|
internal GlobalShortcut() { }
|
|
|
|
|
|
|
|
|
|
|
|
internal static GlobalShortcut Instance
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_globalShortcut == null)
|
|
|
|
|
|
{
|
2017-11-04 00:16:14 +01:00
|
|
|
|
lock (_syncRoot)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_globalShortcut == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_globalShortcut = new GlobalShortcut();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _globalShortcut;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-09-15 11:14:45 +02:00
|
|
|
|
private readonly Dictionary<string, Action> _shortcuts = new();
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 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.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void Register(string accelerator, Action function)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!_shortcuts.ContainsKey(accelerator))
|
|
|
|
|
|
{
|
|
|
|
|
|
_shortcuts.Add(accelerator, function);
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Off("globalShortcut-pressed");
|
|
|
|
|
|
BridgeConnector.On<string>("globalShortcut-pressed", (shortcut) =>
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
2021-07-12 19:50:39 +02:00
|
|
|
|
if (_shortcuts.ContainsKey(shortcut))
|
2017-10-21 04:37:01 +02:00
|
|
|
|
{
|
|
|
|
|
|
_shortcuts[shortcut.ToString()]();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("globalShortcut-register", accelerator);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 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.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>Whether this application has registered accelerator.</returns>
|
2021-08-23 11:22:37 +02:00
|
|
|
|
public Task<bool> IsRegisteredAsync(string accelerator) => BridgeConnector.OnResult<bool>("globalShortcut-isRegistered", "globalShortcut-isRegisteredCompleted", accelerator);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Unregisters the global shortcut of accelerator.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void Unregister(string accelerator)
|
|
|
|
|
|
{
|
|
|
|
|
|
_shortcuts.Remove(accelerator);
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("globalShortcut-unregister", accelerator);
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Unregisters all of the global shortcuts.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void UnregisterAll()
|
|
|
|
|
|
{
|
|
|
|
|
|
_shortcuts.Clear();
|
2021-07-12 19:50:39 +02:00
|
|
|
|
BridgeConnector.Emit("globalShortcut-unregisterAll");
|
2017-10-21 04:37:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|