Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(wpf): Replace Reflection with PInovke win32 in WpfKeyboardInputSource #16670

Merged
merged 1 commit into from
May 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 10 additions & 27 deletions src/Uno.UI.Runtime.Skia.Wpf/Input/WpfKeyboardInputSource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Windows.Input;
using System;
using System.Reflection;
using Uno.Foundation.Logging;
using Windows.Foundation;
using Windows.System;
Expand All @@ -10,6 +9,7 @@
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using WpfUIElement = System.Windows.UIElement;
using WinUIKeyEventArgs = Windows.UI.Core.KeyEventArgs;
using System.Runtime.InteropServices;

namespace Uno.UI.Runtime.Skia.Wpf.Input;

Expand All @@ -36,13 +36,12 @@ private void HostOnKeyDown(object sender, KeyEventArgs args)
try
{
var virtualKey = ConvertKey(args.Key);

if (this.Log().IsEnabled(LogLevel.Trace))
{
this.Log().Trace($"OnKeyPressEvent: {args.Key} -> {virtualKey}");
}

var scanCode = GetScanCode(args);
var scanCode = GetScanCode(virtualKey);

KeyDown?.Invoke(this, new(
"keyboard",
Expand Down Expand Up @@ -72,7 +71,7 @@ private void HostOnKeyUp(object sender, System.Windows.Input.KeyEventArgs args)
this.Log().Trace($"OnKeyPressEvent: {args.Key} -> {virtualKey}");
}

var scanCode = GetScanCode(args);
var scanCode = GetScanCode(virtualKey);

KeyUp?.Invoke(this, new(
"keyboard",
Expand All @@ -91,29 +90,10 @@ private void HostOnKeyUp(object sender, System.Windows.Input.KeyEventArgs args)
}
}

// WPF doesn't expose the scancode, but it exists internally
private static uint GetScanCode(KeyEventArgs args)
private static uint GetScanCode(VirtualKey virtualKey)
{
try
{
if (typeof(KeyEventArgs).GetProperty("ScanCode", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) is { } propertyInfo)
{
return (uint)(int)propertyInfo.GetValue(args)!;
}
else
{
throw new PlatformNotSupportedException("Unable to get the ScanCode property from WPF. This likely means this WPF version is not compatible with Uno Platform, contact the developers for more information.");
}
}
catch (Exception e)
{
if (typeof(WpfKeyboardInputSource).Log().IsEnabled(LogLevel.Error))
{
typeof(WpfKeyboardInputSource).Log().LogError("Unable to get ScanCode from WPF KeyEventArgs.", e);
}

throw;
}
var scanCode = MapVirtualKeyW((uint)virtualKey, 0 /*MAPVK_VK_TO_VSC*/);
return scanCode;
}

private static char? KeyCodeToUnicode(uint keyCode, VirtualKey virtualKey)
Expand Down Expand Up @@ -156,8 +136,11 @@ private static VirtualKeyModifiers GetKeyModifiers(ModifierKeys modifierKeys)
return modifiers;
}

private VirtualKey ConvertKey(Key key)
private static VirtualKey ConvertKey(Key key)
{
return (VirtualKey)System.Windows.Input.KeyInterop.VirtualKeyFromKey(key);
}

[DllImport("User32.dll")]
Youssef1313 marked this conversation as resolved.
Show resolved Hide resolved
private static extern uint MapVirtualKeyW(uint code, uint mapType);
}