2020-07-23 07:55:04 +08:00
|
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
2020-06-12 03:59:36 +08:00
|
|
|
|
using interop;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerToys.Settings.UI.Lib
|
|
|
|
|
{
|
|
|
|
|
public delegate void KeyEvent(int key);
|
2020-06-24 08:19:36 +08:00
|
|
|
|
|
2020-06-12 03:59:36 +08:00
|
|
|
|
public delegate bool IsActive();
|
|
|
|
|
|
|
|
|
|
public class HotkeySettingsControlHook
|
|
|
|
|
{
|
2020-07-24 04:08:20 +08:00
|
|
|
|
private const int WmKeyDown = 0x100;
|
|
|
|
|
private const int WmKeyUp = 0x101;
|
|
|
|
|
private const int WmSysKeyDown = 0x0104;
|
|
|
|
|
private const int WmSysKeyUp = 0x0105;
|
2020-06-12 03:59:36 +08:00
|
|
|
|
|
2020-07-24 04:08:20 +08:00
|
|
|
|
private KeyboardHook _hook;
|
|
|
|
|
private KeyEvent _keyDown;
|
|
|
|
|
private KeyEvent _keyUp;
|
|
|
|
|
private IsActive _isActive;
|
2020-06-12 03:59:36 +08:00
|
|
|
|
|
|
|
|
|
public HotkeySettingsControlHook(KeyEvent keyDown, KeyEvent keyUp, IsActive isActive)
|
|
|
|
|
{
|
2020-07-24 04:08:20 +08:00
|
|
|
|
_keyDown = keyDown;
|
|
|
|
|
_keyUp = keyUp;
|
|
|
|
|
_isActive = isActive;
|
|
|
|
|
_hook = new KeyboardHook(HotkeySettingsHookCallback, IsActive, null);
|
|
|
|
|
_hook.Start();
|
2020-06-12 03:59:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsActive()
|
|
|
|
|
{
|
2020-07-24 04:08:20 +08:00
|
|
|
|
return _isActive();
|
2020-06-12 03:59:36 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HotkeySettingsHookCallback(KeyboardEvent ev)
|
|
|
|
|
{
|
|
|
|
|
switch (ev.message)
|
|
|
|
|
{
|
2020-07-24 04:08:20 +08:00
|
|
|
|
case WmKeyDown:
|
|
|
|
|
case WmSysKeyDown:
|
|
|
|
|
_keyDown(ev.key);
|
2020-06-12 03:59:36 +08:00
|
|
|
|
break;
|
2020-07-24 04:08:20 +08:00
|
|
|
|
case WmKeyUp:
|
|
|
|
|
case WmSysKeyUp:
|
|
|
|
|
_keyUp(ev.key);
|
2020-06-12 03:59:36 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-24 08:19:36 +08:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
// Dispose the KeyboardHook object to terminate the hook threads
|
2020-07-24 04:08:20 +08:00
|
|
|
|
_hook.Dispose();
|
2020-06-24 08:19:36 +08:00
|
|
|
|
}
|
2020-06-12 03:59:36 +08:00
|
|
|
|
}
|
|
|
|
|
}
|