Rename projects to better structure it. The aim is to try and keep it not dependant on the type of UI framework it uses thus allowing us to switch to another UI framework if we need later...

This commit is contained in:
TheXamlGuy
2024-01-06 08:49:12 +00:00
parent b380f06fbf
commit 3e88950669
67 changed files with 211 additions and 196 deletions
+83
View File
@@ -0,0 +1,83 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;
using Windows.System;
using Windows.Win32;
using Windows.Win32.UI.KeyboardAndMouseInput;
namespace Hyperbar.Windows.Win32;
public class KeyInterop
{
private static readonly VirtualKey[] extendedKeys = [
VirtualKey.Menu,
VirtualKey.LeftMenu,
VirtualKey.NumberKeyLock,
VirtualKey.Insert,
VirtualKey.Delete,
VirtualKey.Home,
VirtualKey.End,
VirtualKey.Up,
VirtualKey.Down,
VirtualKey.Left,
VirtualKey.Right,
VirtualKey.Application,
VirtualKey.RightWindows,
VirtualKey.LeftWindows ];
public static void Press(VirtualKey key) => SendKey(key, true);
public static void Release(VirtualKey key) => SendKey(key, false);
public static unsafe void Type(VirtualKey key,
params VirtualKey[] modifierKeys)
{
foreach (VirtualKey modiferKey in modifierKeys)
{
Press(modiferKey);
}
Press(key);
Release(key);
foreach (VirtualKey modifierKey in modifierKeys.Reverse())
{
Release(modifierKey);
}
}
private static unsafe void SendKey(VirtualKey key,
bool pressed)
{
INPUT input = new()
{
type = INPUT_TYPE.INPUT_KEYBOARD
};
input.Anonymous.ki.wVk = (ushort)key;
input.Anonymous.ki.wScan = (ushort)PInvoke.MapVirtualKey(input.Anonymous.ki.wVk, 0);
KEYBD_EVENT_FLAGS flags = 0;
if (input.Anonymous.ki.wScan > 0)
{
flags |= KEYBD_EVENT_FLAGS.KEYEVENTF_SCANCODE;
}
if (!pressed)
{
flags |= KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP;
}
if (extendedKeys.Contains(key))
{
flags |= KEYBD_EVENT_FLAGS.KEYEVENTF_EXTENDEDKEY;
}
input.Anonymous.ki.dwFlags = flags;
input.Anonymous.ki.time = 0;
input.Anonymous.ki.dwExtraInfo = new nuint();
PInvoke.SendInput(new Span<INPUT>(ref input), Marshal.SizeOf(input));
}
}