Skip to content

Commit aaafdaa

Browse files
Fix virtual key to char conversion on Windows (#26)
* Add key char to sample app * Fix virtual key to char conversion on Windows Fixes: #25 * addressing some copilot feedback --------- Co-authored-by: David Ortinau <david.ortinau@microsoft.com>
1 parent 0ee52a5 commit aaafdaa

File tree

6 files changed

+28
-15
lines changed

6 files changed

+28
-15
lines changed

samples/Plugin.Maui.KeyListener.Sample/MainPage.xaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ void AddKeyboardBehavior()
2121

2222
void PrependOutput(string eventName, KeyPressedEventArgs e)
2323
{
24-
OutputLabel.Text = $"{eventName}: {e.Modifiers} {e.Keys}" + Environment.NewLine + OutputLabel.Text;
24+
OutputLabel.Text = $"{eventName}: {e.Modifiers} {e.Keys} {e.KeyChar}" + Environment.NewLine + OutputLabel.Text;
2525
}
2626

27-
private void ClearButton_Clicked(object sender, EventArgs e)
27+
void ClearButton_Clicked(object sender, EventArgs e)
2828
{
2929
OutputLabel.Text = string.Empty;
3030
}

src/Plugin.Maui.KeyListener/KeyboardBehavior.Windows.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,7 @@ void OnWindowKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e
4949

5050
void OnKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
5151
{
52-
char keyChar = ((char)e.Key);
53-
var eventArgs = new KeyPressedEventArgs
54-
{
55-
Keys = e.Key.ToKeyboardKeys(),
56-
KeyChar = keyChar
57-
};
58-
52+
var eventArgs = e.ToKeyPressedEventArgs();
5953
this.RaiseKeyDown(eventArgs);
6054
}
6155
void OnPreviewKeyUp(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
@@ -70,12 +64,7 @@ void OnPreviewKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs
7064

7165
void OnKeyUp(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
7266
{
73-
char keyChar = ((char)e.Key);
74-
var eventArgs = new KeyPressedEventArgs
75-
{
76-
Keys = e.Key.ToKeyboardKeys(),
77-
KeyChar = keyChar
78-
};
67+
var eventArgs = e.ToKeyPressedEventArgs();
7968
this.RaiseKeyUp(eventArgs);
8069
}
8170
}

src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.Windows.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#if WINDOWS
22

3+
using Microsoft.UI.Xaml.Input;
34
using Windows.System;
5+
using Windows.Win32.UI.Input.KeyboardAndMouse;
46

57
namespace Plugin.Maui.KeyListener;
68

@@ -163,5 +165,16 @@ internal static partial class KeyboardKeysExtensions
163165
VirtualKey.NumberPad9 => KeyboardKeys.NumPad9,
164166
_ => 0
165167
};
168+
169+
public static char ToChar(this VirtualKey key) => (char)Windows.Win32.PInvoke.MapVirtualKey((uint)key, MAP_VIRTUAL_KEY_TYPE.MAPVK_VK_TO_CHAR);
170+
171+
internal static KeyPressedEventArgs ToKeyPressedEventArgs(this KeyRoutedEventArgs e)
172+
{
173+
return new KeyPressedEventArgs
174+
{
175+
Keys = e.Key.ToKeyboardKeys(),
176+
KeyChar = e.Key.ToChar(),
177+
};
178+
}
166179
}
167180
#endif
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://aka.ms/CsWin32.schema.json",
3+
"wideCharOnly": true,
4+
"emitSingleFile": true,
5+
"public": false
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MapVirtualKey

src/Plugin.Maui.KeyListener/Plugin.Maui.KeyListener.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@
7272

7373
<ItemGroup>
7474
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" IsImplicitlyDefined="true" />
75+
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.183" Condition="$(TargetFramework.StartsWith('net')) == true AND $(TargetFramework.Contains('-windows')) == true">
76+
<PrivateAssets>all</PrivateAssets>
77+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
78+
</PackageReference>
7579
</ItemGroup>
7680

7781
<!-- Package additions -->

0 commit comments

Comments
 (0)