-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor character input conversion to InputHelper
- Loading branch information
1 parent
5bcba64
commit cf381d0
Showing
3 changed files
with
46 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |