Skip to content

Commit

Permalink
Merge pull request #16670 from lindexi/t/lindexi/GetScanCode
Browse files Browse the repository at this point in the history
perf(wpf): Replace Reflection with PInovke win32 in WpfKeyboardInputSource
  • Loading branch information
Youssef1313 authored May 10, 2024
2 parents c26af1c + a851109 commit 3acfcf6
Showing 1 changed file with 10 additions and 27 deletions.
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")]
private static extern uint MapVirtualKeyW(uint code, uint mapType);
}

0 comments on commit 3acfcf6

Please sign in to comment.