Skip to content

Commit

Permalink
chore: refactor character input conversion to InputHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
ramezgerges committed Nov 3, 2023
1 parent 5bcba64 commit cf381d0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 64 deletions.
34 changes: 2 additions & 32 deletions src/Uno.UI.Runtime.Skia.Gtk/Input/GtkKeyboardInputSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Windows.UI.Core;
using Uno.Foundation.Logging;
using Windows.Foundation;
using Uno.UI.Helpers;

namespace Uno.UI.Runtime.Skia.Gtk;

Expand Down Expand Up @@ -96,7 +97,7 @@ private void OnKeyReleaseEvent(EventKey evt)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var result = WindowsKeyCodeToUnicode(keyCode);
var result = InputHelper.WindowsKeyCodeToUnicode(keyCode);
return result.Length > 0 ? result[0] : null; // TODO: supplementary code points
}

Expand All @@ -105,37 +106,6 @@ private void OnKeyReleaseEvent(EventKey evt)
return gdkChar == 0 ? null : gdkChar;
}

[DllImport("user32.dll")]
private static extern bool GetKeyboardState(byte[] lpKeyState);

[DllImport("user32.dll")]
private static extern uint MapVirtualKey(uint uCode, uint uMapType);

[DllImport("user32.dll")]
private static extern IntPtr GetKeyboardLayout(uint idThread);

[DllImport("user32.dll")]
private static extern int ToUnicodeEx(uint wVirtKey, uint wScanCode, byte[] lpKeyState, [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pwszBuff, int cchBuff, uint wFlags, IntPtr dwhkl);

private static string WindowsKeyCodeToUnicode(uint keyCode)
{
var keyboardState = new byte[255];
var keyboardStateStatus = GetKeyboardState(keyboardState);

if (!keyboardStateStatus)
{
return "";
}

var scanCode = MapVirtualKey(keyCode, 0);
var inputLocaleIdentifier = GetKeyboardLayout((uint)Environment.CurrentManagedThreadId);

var result = new StringBuilder();
ToUnicodeEx(keyCode, scanCode, keyboardState, result, (int)5, (uint)0, inputLocaleIdentifier);

return result.ToString();
}

private static VirtualKey ConvertKey(Gdk.Key key)
{
// In this function, commented out lines correspond to VirtualKeys not yet
Expand Down
34 changes: 2 additions & 32 deletions src/Uno.UI.Runtime.Skia.Wpf/Input/WpfKeyboardInputSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Windows.Foundation;
using Windows.System;
using Windows.UI.Core;
using Uno.UI.Helpers;
using Uno.UI.Hosting;
using WpfUIElement = System.Windows.UIElement;
using WinUIKeyEventArgs = Windows.UI.Core.KeyEventArgs;
Expand Down Expand Up @@ -88,41 +89,10 @@ private void HostOnKeyUp(object sender, System.Windows.Input.KeyEventArgs args)

private static char? KeyCodeToUnicode(uint keyCode)
{
var result = WindowsKeyCodeToUnicode(keyCode);
var result = InputHelper.WindowsKeyCodeToUnicode(keyCode);
return result.Length > 0 ? result[0] : null; // TODO: supplementary code points
}

[DllImport("user32.dll")]
private static extern bool GetKeyboardState(byte[] lpKeyState);

[DllImport("user32.dll")]
private static extern uint MapVirtualKey(uint uCode, uint uMapType);

[DllImport("user32.dll")]
private static extern IntPtr GetKeyboardLayout(uint idThread);

[DllImport("user32.dll")]
private static extern int ToUnicodeEx(uint wVirtKey, uint wScanCode, byte[] lpKeyState, [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pwszBuff, int cchBuff, uint wFlags, IntPtr dwhkl);

private static string WindowsKeyCodeToUnicode(uint keyCode)
{
var keyboardState = new byte[255];
var keyboardStateStatus = GetKeyboardState(keyboardState);

if (!keyboardStateStatus)
{
return "";
}

var scanCode = MapVirtualKey(keyCode, 0);
var inputLocaleIdentifier = GetKeyboardLayout((uint)Environment.CurrentManagedThreadId);

var result = new StringBuilder();
ToUnicodeEx(keyCode, scanCode, keyboardState, result, (int)5, (uint)0, inputLocaleIdentifier);

return result.ToString();
}

private static VirtualKeyModifiers GetKeyModifiers(ModifierKeys modifierKeys)
{
var modifiers = VirtualKeyModifiers.None;
Expand Down
42 changes: 42 additions & 0 deletions src/Uno.UI/Helpers/InputHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Uno.UI.Helpers;

public class InputHelper
{
[DllImport("user32.dll")]
private static extern bool GetKeyboardState(byte[] lpKeyState);

[DllImport("user32.dll")]
private static extern uint MapVirtualKey(uint uCode, uint uMapType);

[DllImport("user32.dll")]
private static extern IntPtr GetKeyboardLayout(uint idThread);

[DllImport("user32.dll")]
private static extern int ToUnicodeEx(uint wVirtKey, uint wScanCode, byte[] lpKeyState, [Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pwszBuff, int cchBuff, uint wFlags, IntPtr dwhkl);

public static string WindowsKeyCodeToUnicode(uint keyCode)
{
var keyboardState = new byte[255];
var keyboardStateStatus = GetKeyboardState(keyboardState);

if (!keyboardStateStatus)
{
return "";
}

var scanCode = MapVirtualKey(keyCode, 0);
var inputLocaleIdentifier = GetKeyboardLayout((uint)Environment.CurrentManagedThreadId);

var result = new StringBuilder();
var conversionStatus = ToUnicodeEx(keyCode, scanCode, keyboardState, result, (int)5, (uint)0, inputLocaleIdentifier);
if (conversionStatus <= 0 || char.IsControl(result[0]))
{
return "";
}

return result.ToString();
}
}

0 comments on commit cf381d0

Please sign in to comment.