83 lines
2.1 KiB
C#
83 lines
2.1 KiB
C#
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));
|
|
}
|
|
} |