FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Electron.NET/src/ElectronNET.API/API/GlobalShortcut.cs at develop · ElectronNET/Electron.NET · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
ElectronNET
/
Electron.NET
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
743
Star
7.6k
Code
Issues
21
Pull requests
1
Discussions
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Electron.NET
/
src
/
ElectronNET.API
/
API
/
GlobalShortcut.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
102 lines (89 loc) · 3.38 KB
Breadcrumbs
Electron.NET
/
src
/
ElectronNET.API
/
API
/
GlobalShortcut.cs
Copy path
File metadata and controls
102 lines (89 loc) · 3.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Text
.
Json
;
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
;
private
static
object
_syncRoot
=
new
object
(
)
;
internal
GlobalShortcut
(
)
{
}
internal
static
GlobalShortcut
Instance
{
get
{
if
(
_globalShortcut
==
null
)
{
lock
(
_syncRoot
)
{
if
(
_globalShortcut
==
null
)
{
_globalShortcut
=
new
GlobalShortcut
(
)
;
}
}
}
return
_globalShortcut
;
}
}
private
Dictionary
<
string
,
Action
>
_shortcuts
=
new
Dictionary
<
string
,
Action
>
(
)
;
/// <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
)
;
BridgeConnector
.
Socket
.
Off
(
"globalShortcut-pressed"
)
;
BridgeConnector
.
Socket
.
On
<
string
>
(
"globalShortcut-pressed"
,
(
shortcut
)
=>
{
if
(
_shortcuts
.
TryGetValue
(
shortcut
,
out
var
action
)
)
{
action
(
)
;
}
}
)
;
BridgeConnector
.
Socket
.
Emit
(
"globalShortcut-register"
,
accelerator
)
;
}
}
/// <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>
public
Task
<
bool
>
IsRegisteredAsync
(
string
accelerator
)
{
var
tcs
=
new
TaskCompletionSource
<
bool
>
(
)
;
BridgeConnector
.
Socket
.
Once
<
bool
>
(
"globalShortcut-isRegisteredCompleted"
,
tcs
.
SetResult
)
;
BridgeConnector
.
Socket
.
Emit
(
"globalShortcut-isRegistered"
,
accelerator
)
;
return
tcs
.
Task
;
}
/// <summary>
/// Unregisters the global shortcut of accelerator.
/// </summary>
public
void
Unregister
(
string
accelerator
)
{
_shortcuts
.
Remove
(
accelerator
)
;
BridgeConnector
.
Socket
.
Emit
(
"globalShortcut-unregister"
,
accelerator
)
;
}
/// <summary>
/// Unregisters all of the global shortcuts.
/// </summary>
public
void
UnregisterAll
(
)
{
_shortcuts
.
Clear
(
)
;
BridgeConnector
.
Socket
.
Emit
(
"globalShortcut-unregisterAll"
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL