1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-29 01:00:13 +00:00
msfs-popout-panel-manager/WindowsAgent/InputHookManager.cs

100 lines
2.9 KiB
C#
Raw Normal View History

2022-07-23 19:23:32 +00:00
using System;
using System.Diagnostics;
using System.Drawing;
using WindowsHook;
namespace MSFSPopoutPanelManager.WindowsAgent
{
public class InputHookManager
{
2023-07-12 22:41:31 +00:00
// Mouse hooks
2022-07-23 19:23:32 +00:00
private static IKeyboardMouseEvents _mouseHook;
public static event EventHandler<Point> OnLeftClick;
2023-07-12 22:41:31 +00:00
// Keyboard hooks
private static IKeyboardMouseEvents _keyboardHook;
public static event EventHandler<KeyUpEventArgs> OnKeyUp;
public static void StartMouseHook()
2022-07-23 19:23:32 +00:00
{
if (_mouseHook == null)
{
2023-07-12 22:41:31 +00:00
Debug.WriteLine("Start Mouse Hook...");
2022-07-23 19:23:32 +00:00
_mouseHook = Hook.GlobalEvents();
_mouseHook.MouseDownExt += HandleMouseHookMouseDownExt;
}
}
2023-07-12 22:41:31 +00:00
public static void EndMouseHook()
2022-07-23 19:23:32 +00:00
{
if (_mouseHook != null)
{
2023-07-12 22:41:31 +00:00
Debug.WriteLine("End Mouse Hook...");
2022-07-23 19:23:32 +00:00
_mouseHook.MouseDownExt -= HandleMouseHookMouseDownExt;
_mouseHook.Dispose();
_mouseHook = null;
}
2023-07-12 22:41:31 +00:00
if (OnLeftClick != null)
{
foreach (Delegate d in OnLeftClick.GetInvocationList())
OnLeftClick -= (EventHandler<Point>)d;
}
2022-07-23 19:23:32 +00:00
}
private static void HandleMouseHookMouseDownExt(object sender, MouseEventExtArgs e)
{
if (_mouseHook == null)
return;
if (e.Button == MouseButtons.Left)
2023-07-12 22:41:31 +00:00
OnLeftClick?.Invoke(null, new Point(e.X, e.Y));
}
public static void StartKeyboardHook()
{
if (_keyboardHook != null)
EndKeyboardHook();
if (_keyboardHook == null)
{
Debug.WriteLine("Starting Keyboard Hook...");
_keyboardHook = Hook.GlobalEvents();
_keyboardHook.KeyUp += HandleKeyboardHookKeyUp;
}
}
public static void EndKeyboardHook()
{
if (_keyboardHook != null)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
Debug.WriteLine("Ending Keyboard Hook...");
_keyboardHook.KeyUp -= HandleKeyboardHookKeyUp;
_keyboardHook.Dispose();
_keyboardHook = null;
}
if (OnKeyUp != null)
{
foreach (Delegate d in OnKeyUp.GetInvocationList())
OnKeyUp -= (EventHandler<KeyUpEventArgs>)d;
2022-07-23 19:23:32 +00:00
}
}
2023-07-12 22:41:31 +00:00
private static void HandleKeyboardHookKeyUp(object sender, KeyEventArgs e)
{
OnKeyUp?.Invoke(null, new KeyUpEventArgs() { KeyCode = e.KeyCode.ToString(), IsHoldControl = e.Control, IsHoldShift = e.Shift });
}
}
public class KeyUpEventArgs : EventArgs
{
public string KeyCode { get; set; }
public bool IsHoldControl { get; set; }
public bool IsHoldShift { get; set; }
2022-07-23 19:23:32 +00:00
}
}