keyboard introp

This commit is contained in:
TheXamlGuy
2024-01-04 22:27:54 +00:00
parent d45076f2a9
commit df60262349
6 changed files with 102 additions and 2 deletions
@@ -10,6 +10,6 @@
mc:Ignorable="d">
<Grid>
<Button Content="This is a test" />
<Button Content="This is a test" Click="Button_Click"/>
</Grid>
</Page>
@@ -1,8 +1,15 @@
using Hyperbar.Desktop.Win32;
using Microsoft.UI.Xaml.Controls;
using Windows.System;
namespace Hyperbar.Desktop.Contextual;
public sealed partial class ContextualCommandView : Page
{
public ContextualCommandView() => InitializeComponent();
private void Button_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
KeyIntrop.Type((VirtualKey)186, VirtualKey.LeftWindows);
}
}
@@ -18,6 +18,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Hyperbar.Desktop.Win32\Hyperbar.Desktop.Win32.csproj" />
<ProjectReference Include="..\Hyperbar\Hyperbar.csproj" />
</ItemGroup>
+84
View File
@@ -0,0 +1,84 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;
using Windows.System;
using Windows.Win32;
using Windows.Win32.UI.KeyboardAndMouseInput;
namespace Hyperbar.Desktop.Win32;
public class KeyIntrop
{
private static readonly VirtualKey[] ExtendedKeys = [
VirtualKey.Menu,
VirtualKey.Menu,
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));
}
}
+2
View File
@@ -55,3 +55,5 @@ CreateRectRgn
CreateSolidBrush
FillRect
GetDC
SendInput
MapVirtualKey
@@ -1,8 +1,14 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
namespace Hyperbar.Desktop;
public sealed partial class CommandView : Page
{
public CommandView() => InitializeComponent();
protected override void OnKeyDown(KeyRoutedEventArgs e)
{
base.OnKeyDown(e);
}
}