keyboard introp
This commit is contained in:
@@ -10,6 +10,6 @@
|
|||||||
mc:Ignorable="d">
|
mc:Ignorable="d">
|
||||||
|
|
||||||
<Grid>
|
<Grid>
|
||||||
<Button Content="This is a test" />
|
<Button Content="This is a test" Click="Button_Click"/>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Page>
|
</Page>
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
|
using Hyperbar.Desktop.Win32;
|
||||||
using Microsoft.UI.Xaml.Controls;
|
using Microsoft.UI.Xaml.Controls;
|
||||||
|
using Windows.System;
|
||||||
|
|
||||||
namespace Hyperbar.Desktop.Contextual;
|
namespace Hyperbar.Desktop.Contextual;
|
||||||
|
|
||||||
public sealed partial class ContextualCommandView : Page
|
public sealed partial class ContextualCommandView : Page
|
||||||
{
|
{
|
||||||
public ContextualCommandView() => InitializeComponent();
|
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>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Hyperbar.Desktop.Win32\Hyperbar.Desktop.Win32.csproj" />
|
||||||
<ProjectReference Include="..\Hyperbar\Hyperbar.csproj" />
|
<ProjectReference Include="..\Hyperbar\Hyperbar.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -55,3 +55,5 @@ CreateRectRgn
|
|||||||
CreateSolidBrush
|
CreateSolidBrush
|
||||||
FillRect
|
FillRect
|
||||||
GetDC
|
GetDC
|
||||||
|
SendInput
|
||||||
|
MapVirtualKey
|
||||||
@@ -1,8 +1,14 @@
|
|||||||
using Microsoft.UI.Xaml.Controls;
|
using Microsoft.UI.Xaml.Controls;
|
||||||
|
using Microsoft.UI.Xaml.Input;
|
||||||
|
|
||||||
namespace Hyperbar.Desktop;
|
namespace Hyperbar.Desktop;
|
||||||
|
|
||||||
public sealed partial class CommandView : Page
|
public sealed partial class CommandView : Page
|
||||||
{
|
{
|
||||||
public CommandView() => InitializeComponent();
|
public CommandView() => InitializeComponent();
|
||||||
|
|
||||||
|
protected override void OnKeyDown(KeyRoutedEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnKeyDown(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user