-
Notifications
You must be signed in to change notification settings - Fork 9
Fix virtual key to char conversion on Windows #26
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
Fix virtual key to char conversion on Windows #26
Conversation
|
@davidortinau Maybe you'd like to use CsWin32 to provide the bindings, or know some other way of handling it without hitting the Win32 API directly? Either way, I'm happy to clean it up before merge. |
83e58bb to
54b30aa
Compare
|
I've added CsWin32 as a build dependency on Windows. It's maybe a bit overkill for one function, but it's something others will be reasonably familiar with if more bindings are needed and it saves debate about how to manage p/invokes. I'd understand if it's not wanted though. |
|
Didn't take long to find that we do in fact need more bindings 😆 This is essentially a prerequisite for #27 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds proper Win32 interop to convert VirtualKey codes into characters and refactors Windows-specific key event handling to use these new extensions.
- Introduce CsWin32 package and config to generate the
MapVirtualKeyP/Invoke binding - Add
ToCharandToKeyPressedEventArgsextension methods to centralize key-to-char conversion - Update
KeyboardBehavior.Windowsand sample to consume the new helpers
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Plugin.Maui.KeyListener/Plugin.Maui.KeyListener.csproj | Add Microsoft.Windows.CsWin32 package reference for Win32 APIs |
| src/Plugin.Maui.KeyListener/NativeMethods.txt | List of native method names (MapVirtualKey) for CsWin32 generation |
| src/Plugin.Maui.KeyListener/NativeMethods.json | CsWin32 configuration (wideCharOnly, emitSingleFile, public=false) |
| src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.Windows.cs | Implement ToChar and ToKeyPressedEventArgs extensions |
| src/Plugin.Maui.KeyListener/KeyboardBehavior.Windows.cs | Refactor key event handlers to use ToKeyPressedEventArgs |
| samples/Plugin.Maui.KeyListener.Sample/MainPage.xaml.cs | Update sample to show KeyChar in output |
Comments suppressed due to low confidence (2)
src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.Windows.cs:172
- The new
ToKeyPressedEventArgshelper wraps key conversion logic. Consider adding unit tests to verify thatKeysandKeyCharare set correctly across a variety ofVirtualKeyvalues and modifier states.
internal static KeyPressedEventArgs ToKeyPressedEventArgs(this KeyRoutedEventArgs e)
src/Plugin.Maui.KeyListener/NativeMethods.txt:1
- [nitpick] It's not clear how
NativeMethods.txtis consumed. Consider adding a header or comment explaining its purpose or remove it if it's no longer needed.
MapVirtualKey
| _ => 0 | ||
| }; | ||
|
|
||
| public static char ToChar(this VirtualKey key) => (char)PInvoke.MapVirtualKey((uint)key, MAP_VIRTUAL_KEY_TYPE.MAPVK_VK_TO_CHAR); |
Copilot
AI
May 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using MapVirtualKey with MAPVK_VK_TO_CHAR ignores modifier keys (Shift, Ctrl) and current keyboard layout/layout-specific dead keys. Consider using ToUnicodeEx to accurately map key codes + modifiers to characters.
|
|
||
| using Microsoft.UI.Xaml.Input; | ||
| using Windows.System; | ||
| using Windows.Win32; |
Copilot
AI
May 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Importing the entire Windows.Win32 namespace is broad. You can narrow this to Windows.Win32.UI.Input.KeyboardAndMouse to limit scope and improve readability.
| using Windows.Win32; |
| _ => 0 | ||
| }; | ||
|
|
||
| public static char ToChar(this VirtualKey key) => (char)PInvoke.MapVirtualKey((uint)key, MAP_VIRTUAL_KEY_TYPE.MAPVK_VK_TO_CHAR); |
Copilot
AI
May 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The newly added ToChar extension lacks XML documentation. Adding a <summary> explaining expected behavior, limitations (e.g., no modifiers), and return value would help maintainers and API consumers.
Use MapVirtualKey to convert
VirtualKeytocharFixes: #25