1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-10-16 14:10:45 +00:00
msfs-popout-panel-manager/WindowsAgent/InputEmulationManager.cs
Stanley 43caff1ca9 Squashed commit of the following:
commit 2bbda3c4e4969fdf05566908fde01f1c9e4e23f7
Author: Stanley <hawkeyesk@outlook.com>
Date:   Mon Sep 5 23:54:39 2022 -0400

    Added in-game panel support

commit ec29b0ec2612b10e45ab9a73c10b88a98fcf6eaf
Author: Stanley <hawkeyesk@outlook.com>
Date:   Sun Sep 4 21:21:37 2022 -0400

    Added in-game panel support

commit 97edc184f349e1fde74a15a643fb90fb9bd90395
Author: Stanley <hawkeyesk@outlook.com>
Date:   Thu Sep 1 18:08:44 2022 -0400

    Update touch panel

commit da48ca0a272290466952c5c1bd1ca035d56f930c
Author: Stanley <hawkeyesk@outlook.com>
Date:   Mon Aug 29 22:19:38 2022 -0400

    Added pop out panel temporary display

commit 701346193804f93616b0e6e2222d9d55223f516f
Author: Stanley <hawkeyesk@outlook.com>
Date:   Wed Aug 24 10:33:59 2022 -0400

    Added auto resize window display mode

commit 98cbcd949f1555b44db22267ce5c54858bef47cd
Author: Stanley <hawkeyesk@outlook.com>
Date:   Wed Aug 24 09:39:38 2022 -0400

    Added auto resize window display mode
2022-09-06 00:07:03 -04:00

210 lines
No EOL
7.9 KiB
C#

using System;
using System.Drawing;
using System.Threading;
using WindowsInput;
namespace MSFSPopoutPanelManager.WindowsAgent
{
public class InputEmulationManager
{
const uint MOUSEEVENTF_LEFTDOWN = 0x02;
const uint MOUSEEVENTF_LEFTUP = 0x04;
const uint KEYEVENTF_EXTENDEDKEY = 0x01;
const uint KEYEVENTF_KEYDOWN = 0x0;
const uint KEYEVENTF_KEYUP = 0x2;
const uint VK_RMENU = 0xA5;
const uint VK_LMENU = 0xA4;
const uint VK_LCONTROL = 0xA2;
const uint VK_RCONTROL = 0xA3;
const uint VK_SPACE = 0x20;
const uint VK_ENT = 0x0D;
const uint KEY_0 = 0x30;
private static InputSimulator InputSimulator = new InputSimulator();
public static void LeftClickGameWindow()
{
var simualatorProcess = WindowProcessManager.GetSimulatorProcess();
if (simualatorProcess == null)
return;
Rectangle rectangle;
PInvoke.GetWindowRect(simualatorProcess.Handle, out rectangle);
Rectangle clientRectangle;
PInvoke.GetClientRect(simualatorProcess.Handle, out clientRectangle);
var x = Convert.ToInt32(rectangle.X + (clientRectangle.Width) * 0.5);
var y = Convert.ToInt32(rectangle.Y + (clientRectangle.Height) * 0.5);
LeftClick(x, y);
}
public static void LeftClick(int x, int y)
{
PInvoke.SetCursorPos(x, y); // Need to do this twice to overcome MSFS bug for separating pop out panels
PInvoke.SetCursorPos(x, y);
Thread.Sleep(300);
PInvoke.mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
Thread.Sleep(200);
PInvoke.mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
PInvoke.SetCursorPos(x + 5, y);
}
public static void LeftClickFast(int x, int y)
{
PInvoke.mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
Thread.Sleep(50);
PInvoke.mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}
public static void LeftClickMouseDown(int x, int y)
{
PInvoke.mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
}
public static void LeftClickMouseUp(int x, int y)
{
InputSimulator.Mouse.LeftButtonUp();
}
public static void PopOutPanel(int x, int y, bool useSecondaryKeys)
{
LeftClick(x, y);
Thread.Sleep(300);
PInvoke.SetCursorPos(x, y);
PInvoke.SetCursorPos(x, y);
Thread.Sleep(300);
if (useSecondaryKeys)
{
InputSimulator.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.LCONTROL);
InputSimulator.Keyboard.KeyDown(WindowsInput.Native.VirtualKeyCode.RCONTROL);
PInvoke.mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
Thread.Sleep(200);
PInvoke.mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
InputSimulator.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.RCONTROL);
InputSimulator.Keyboard.KeyUp(WindowsInput.Native.VirtualKeyCode.LCONTROL);
}
else
{
PInvoke.keybd_event(Convert.ToByte(VK_RMENU), 0, KEYEVENTF_EXTENDEDKEY, 0);
PInvoke.mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
Thread.Sleep(200);
PInvoke.mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
PInvoke.keybd_event(Convert.ToByte(VK_RMENU), 0, KEYEVENTF_KEYUP, 0);
}
}
public static void CenterView()
{
var simualatorProcess = WindowProcessManager.GetSimulatorProcess();
if (simualatorProcess == null)
return;
CenterView(simualatorProcess);
}
public static void CenterView(WindowProcess simualatorProcess)
{
Rectangle rectangle;
PInvoke.GetWindowRect(simualatorProcess.Handle, out rectangle);
Rectangle clientRectangle;
PInvoke.GetClientRect(simualatorProcess.Handle, out clientRectangle);
var x = Convert.ToInt32(rectangle.X + (clientRectangle.Width) * 0.5);
var y = Convert.ToInt32(rectangle.Y + (clientRectangle.Height) * 0.5);
PInvoke.SetForegroundWindow(simualatorProcess.Handle);
LeftClick(x, y);
// First center view using Ctrl-Space
PInvoke.keybd_event(Convert.ToByte(VK_RCONTROL), 0, KEYEVENTF_KEYDOWN, 0);
PInvoke.keybd_event(Convert.ToByte(VK_SPACE), 0, KEYEVENTF_KEYDOWN, 0);
Thread.Sleep(200);
PInvoke.keybd_event(Convert.ToByte(VK_SPACE), 0, KEYEVENTF_KEYUP, 0);
PInvoke.keybd_event(Convert.ToByte(VK_RCONTROL), 0, KEYEVENTF_KEYUP, 0);
Thread.Sleep(200);
}
public static void SaveCustomView(string keybinding)
{
var simualatorProcess = WindowProcessManager.GetSimulatorProcess();
if (simualatorProcess == null)
return;
var hwnd = simualatorProcess.Handle;
uint customViewKey = (uint)(Convert.ToInt32(keybinding) + KEY_0);
PInvoke.SetForegroundWindow(hwnd);
Thread.Sleep(500);
PInvoke.SetFocus(hwnd);
Thread.Sleep(300);
// Set view using Ctrl-Alt-0
PInvoke.keybd_event(Convert.ToByte(VK_LCONTROL), 0, KEYEVENTF_KEYDOWN, 0);
PInvoke.keybd_event(Convert.ToByte(VK_LMENU), 0, KEYEVENTF_KEYDOWN, 0);
PInvoke.keybd_event(Convert.ToByte(customViewKey), 0, KEYEVENTF_KEYDOWN, 0);
Thread.Sleep(200);
PInvoke.keybd_event(Convert.ToByte(customViewKey), 0, KEYEVENTF_KEYUP, 0);
PInvoke.keybd_event(Convert.ToByte(VK_LMENU), 0, KEYEVENTF_KEYUP, 0);
PInvoke.keybd_event(Convert.ToByte(VK_LCONTROL), 0, KEYEVENTF_KEYUP, 0);
}
public static void LoadCustomView(string keybinding)
{
var simualatorProcess = WindowProcessManager.GetSimulatorProcess();
if (simualatorProcess == null)
return;
// First center view to make sure recalling custom camera works on the first press
InputEmulationManager.CenterView();
Thread.Sleep(500);
uint customViewKey = (uint)(Convert.ToInt32(keybinding) + KEY_0);
// Then load view using Alt-0
PInvoke.keybd_event(Convert.ToByte(VK_LMENU), 0, KEYEVENTF_KEYDOWN, 0);
PInvoke.keybd_event(Convert.ToByte(customViewKey), 0, KEYEVENTF_KEYDOWN, 0);
Thread.Sleep(200);
PInvoke.keybd_event(Convert.ToByte(customViewKey), 0, KEYEVENTF_KEYUP, 0);
PInvoke.keybd_event(Convert.ToByte(VK_LMENU), 0, KEYEVENTF_KEYUP, 0);
Thread.Sleep(500);
}
public static void ToggleFullScreenPanel(IntPtr hwnd)
{
PInvoke.SetForegroundWindow(hwnd);
Thread.Sleep(500);
PInvoke.SetFocus(hwnd);
Thread.Sleep(300);
PInvoke.keybd_event(Convert.ToByte(VK_RMENU), 0, KEYEVENTF_KEYDOWN, 0);
PInvoke.keybd_event(Convert.ToByte(VK_ENT), 0, KEYEVENTF_KEYDOWN, 0);
Thread.Sleep(200);
PInvoke.keybd_event(Convert.ToByte(VK_ENT), 0, KEYEVENTF_KEYUP, 0);
PInvoke.keybd_event(Convert.ToByte(VK_RMENU), 0, KEYEVENTF_KEYUP, 0);
}
public static void RefocusGameWindow()
{
var simualatorProcess = WindowProcessManager.GetSimulatorProcess();
if (simualatorProcess == null)
return;
var rectangle = WindowActionManager.GetWindowRect(simualatorProcess.Handle);
var clientRectangle = WindowActionManager.GetClientRect(simualatorProcess.Handle);
PInvoke.SetCursorPos(rectangle.X + clientRectangle.Width / 2, rectangle.Y + clientRectangle.Height / 2);
}
}
}