[Settings/Run] LowLevel Keyboard hooking for Hotkeys (#3825)

* [Launcher/Settings] Low Level Keyboard Hooks

* [Run] LowLevel Keyboard Hook for Hotkeys

* Prevent shortcuts from auto repeating when keeping the keys pressed down
This commit is contained in:
Tomas Agustin Raies
2020-06-11 12:59:36 -07:00
committed by GitHub
parent fa7e4cc817
commit 670033c4da
28 changed files with 584 additions and 546 deletions

View File

@@ -2,9 +2,9 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using System.Text;
using System.Text.Json.Serialization;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
namespace Microsoft.PowerToys.Settings.UI.Lib
{
@@ -30,6 +30,11 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
Code = code;
}
public HotkeySettings Clone()
{
return new HotkeySettings(Win, Ctrl, Alt, Shift, Key, Code);
}
[JsonPropertyName("win")]
public bool Win { get; set; }
@@ -72,8 +77,16 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
output.Append("Shift + ");
}
var localKey = Helper.GetKeyName((uint) Code);
output.Append(localKey);
if (Code > 0)
{
var localKey = Helper.GetKeyName((uint)Code);
output.Append(localKey);
}
else if (output.Length >= 2)
{
output.Remove(output.Length - 2, 2);
}
return output.ToString();
}

View File

@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Text;
using interop;
namespace Microsoft.PowerToys.Settings.UI.Lib
{
public delegate void KeyEvent(int key);
public delegate bool IsActive();
public class HotkeySettingsControlHook
{
const int WM_KEYDOWN = 0x100;
const int WM_KEYUP = 0x101;
const int WM_SYSKEYDOWN = 0x0104;
const int WM_SYSKEYUP = 0x0105;
private KeyboardHook hook;
private KeyEvent keyDown;
private KeyEvent keyUp;
private IsActive isActive;
public HotkeySettingsControlHook(KeyEvent keyDown, KeyEvent keyUp, IsActive isActive)
{
this.keyDown = keyDown;
this.keyUp = keyUp;
this.isActive = isActive;
hook = new KeyboardHook(HotkeySettingsHookCallback, IsActive, null);
hook.Start();
}
private bool IsActive()
{
return isActive();
}
private void HotkeySettingsHookCallback(KeyboardEvent ev)
{
switch (ev.message)
{
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
keyDown(ev.key);
break;
case WM_KEYUP:
case WM_SYSKEYUP:
keyUp(ev.key);
break;
}
}
}
}