-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from Crequency/dev=main
[Pull Request] More features, more interfaces, some optimizations
- Loading branch information
Showing
18 changed files
with
490 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Common.BasicHelper.Utils.Extensions; | ||
using SharpHook; | ||
using SharpHook.Native; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace KitX_Dashboard.Managers; | ||
|
||
internal class HotKeyManager | ||
{ | ||
private const int keysLimitation = 5; | ||
|
||
private readonly Queue<KeyCode>? keyPressed; | ||
|
||
private readonly Dictionary<string, Action<KeyCode[]>>? hotKeyHandlers; | ||
|
||
public HotKeyManager() | ||
{ | ||
keyPressed = new(); | ||
|
||
hotKeyHandlers = new(); | ||
} | ||
|
||
public HotKeyManager Hook() | ||
{ | ||
var hook = new TaskPoolGlobalHook(); | ||
|
||
hook.KeyPressed += (_, args) => | ||
{ | ||
keyPressed!.Enqueue(args.Data.KeyCode); | ||
|
||
if (keyPressed!.Count > keysLimitation) | ||
_ = keyPressed.Dequeue(); | ||
|
||
VerifyKeys(); | ||
}; | ||
|
||
hook.RunAsync(); | ||
|
||
return this; | ||
} | ||
|
||
private void VerifyKeys() | ||
{ | ||
var index = 0; | ||
|
||
var tmpList = new KeyCode[keysLimitation]; | ||
|
||
keyPressed!.ForEach(x => | ||
{ | ||
tmpList[index] = x; | ||
|
||
++index; | ||
|
||
}, true); | ||
|
||
foreach (var handler in hotKeyHandlers!.Values) | ||
handler.Invoke(tmpList); | ||
} | ||
|
||
public HotKeyManager RegisterHotKeyHandler(string name, Action<KeyCode[]> handler) | ||
{ | ||
hotKeyHandlers!.Add(name, handler); | ||
|
||
return this; | ||
} | ||
|
||
public HotKeyManager UnregisterHotKeyHandler(string name) | ||
{ | ||
if (hotKeyHandlers!.TryGetValue(name, out _)) | ||
{ | ||
hotKeyHandlers.Remove(name); | ||
} | ||
|
||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.