-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9fe2a75
commit cef6f6a
Showing
16 changed files
with
643 additions
and
17 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
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,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0-windows10.0.22621.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<Platforms>x64</Platforms> | ||
<LangVersion>12.0</LangVersion> | ||
<UseWindowsForms>True</UseWindowsForms> | ||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Vanara.PInvoke.User32" Version="3.4.17" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,100 @@ | ||
using Vanara.PInvoke; | ||
|
||
namespace Fischless.HotkeyCapture; | ||
|
||
public sealed class Hotkey | ||
{ | ||
public bool Alt { get; set; } | ||
public bool Control { get; set; } | ||
public bool Shift { get; set; } | ||
public bool Windows { get; set; } | ||
|
||
private Keys key; | ||
|
||
public Keys Key | ||
{ | ||
get => key; | ||
set | ||
{ | ||
if (value != Keys.ControlKey && value != Keys.Alt && value != Keys.Menu && value != Keys.ShiftKey) | ||
{ | ||
key = value; | ||
} | ||
else | ||
{ | ||
key = Keys.None; | ||
} | ||
} | ||
} | ||
|
||
public User32.HotKeyModifiers ModifierKey => | ||
(Windows ? User32.HotKeyModifiers.MOD_WIN : User32.HotKeyModifiers.MOD_NONE) | | ||
(Control ? User32.HotKeyModifiers.MOD_CONTROL : User32.HotKeyModifiers.MOD_NONE) | | ||
(Shift ? User32.HotKeyModifiers.MOD_SHIFT : User32.HotKeyModifiers.MOD_NONE) | | ||
(Alt ? User32.HotKeyModifiers.MOD_ALT : User32.HotKeyModifiers.MOD_NONE); | ||
|
||
public Hotkey() | ||
{ | ||
Reset(); | ||
} | ||
|
||
public Hotkey(string hotkeyStr) | ||
{ | ||
try | ||
{ | ||
string[] keyStrs = hotkeyStr.Replace(" ", string.Empty).Split('+'); | ||
|
||
foreach (string keyStr in keyStrs) | ||
{ | ||
if (keyStr.Equals("Win", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
Windows = true; | ||
} | ||
else if (keyStr.Equals("Ctrl", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
Control = true; | ||
} | ||
else if (keyStr.Equals("Shift", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
Shift = true; | ||
} | ||
else if (keyStr.Equals("Alt", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
Alt = true; | ||
} | ||
else | ||
{ | ||
Key = (Keys)Enum.Parse(typeof(Keys), keyStr); | ||
} | ||
} | ||
} | ||
catch | ||
{ | ||
throw new ArgumentException("Invalid Hotkey"); | ||
} | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
string str = string.Empty; | ||
if (Key != Keys.None) | ||
{ | ||
str = string.Format("{0}{1}{2}{3}{4}", | ||
Windows ? "Win + " : string.Empty, | ||
Control ? "Ctrl + " : string.Empty, | ||
Shift ? "Shift + " : string.Empty, | ||
Alt ? "Alt + " : string.Empty, | ||
Key); | ||
} | ||
return str; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
Alt = false; | ||
Control = false; | ||
Shift = false; | ||
Windows = false; | ||
Key = Keys.None; | ||
} | ||
} |
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,41 @@ | ||
namespace Fischless.HotkeyCapture; | ||
|
||
public sealed class HotkeyHolder | ||
{ | ||
private static Hotkey? hotkey; | ||
private static HotkeyHook? hotkeyHook; | ||
private static Action<object?, KeyPressedEventArgs>? keyPressed; | ||
|
||
public static void RegisterHotKey(string hotkeyStr, Action<object?, KeyPressedEventArgs> keyPressed = null!) | ||
{ | ||
if (string.IsNullOrEmpty(hotkeyStr)) | ||
{ | ||
UnregisterHotKey(); | ||
return; | ||
} | ||
|
||
hotkey = new Hotkey(hotkeyStr); | ||
|
||
hotkeyHook?.Dispose(); | ||
hotkeyHook = new HotkeyHook(); | ||
hotkeyHook.KeyPressed -= OnKeyPressed; | ||
hotkeyHook.KeyPressed += OnKeyPressed; | ||
HotkeyHolder.keyPressed = keyPressed; | ||
hotkeyHook.RegisterHotKey(hotkey.ModifierKey, hotkey.Key); | ||
} | ||
|
||
public static void UnregisterHotKey() | ||
{ | ||
if (hotkeyHook != null) | ||
{ | ||
hotkeyHook.KeyPressed -= OnKeyPressed; | ||
hotkeyHook.UnregisterHotKey(); | ||
hotkeyHook.Dispose(); | ||
} | ||
} | ||
|
||
private static void OnKeyPressed(object? sender, KeyPressedEventArgs e) | ||
{ | ||
keyPressed?.Invoke(sender, e); | ||
} | ||
} |
Oops, something went wrong.