1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-25 15:20:10 +00:00
msfs-popout-panel-manager/WindowsAgent/InputHookManager.cs

97 lines
2.8 KiB
C#
Raw Permalink 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));
}
2024-03-03 05:02:21 +00:00
public static void StartKeyboardHook()
2023-07-12 22:41:31 +00:00
{
2024-03-03 05:02:21 +00:00
if (_keyboardHook == null)
2023-07-12 22:41:31 +00:00
{
Debug.WriteLine("Starting Keyboard Hook...");
_keyboardHook = Hook.GlobalEvents();
_keyboardHook.KeyUp += HandleKeyboardHookKeyUp;
}
}
2024-03-03 05:02:21 +00:00
public static void EndKeyboardHook()
2023-07-12 22:41:31 +00:00
{
2024-03-03 05:02:21 +00:00
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;
2024-02-29 06:11:56 +00:00
if (OnKeyUp != null)
{
foreach (Delegate d in OnKeyUp.GetInvocationList())
OnKeyUp -= (EventHandler<KeyUpEventArgs>)d;
}
}
}
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
}
}