In standard .NET there existed the ToAscii/ToUnicode and MapVirtualKey functions to take care of this functionality, though it seems an equivalent function or library has not been brought into Metro/WinRT. If there is actually no equivalent function or library exposed in Metro/WinRT then that would make a custom text input box VERY difficult to bring to market in non-US countries. Specific example: in my custom control, if a french keyboard user presses the è,ù,é, or à keys, they are unable to be translated to the correct character. For example, è uses the scan code for VirtualKey.Number7, and as far as I can tell there is no way to know the keyboard layout or no easy way to translate that scancode based on the current keyboard layout. Does anyone have some information about this?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
I suppose it depends on what you are looking for. If you are looking for a simple English letter or number, you can simply do this:
Best of luck!
You can use the Win32 API function MapVirtualKey that maps a virtual key to a bunch of values one of which is a character value. MapVirtualKey seems to use the currently set keyboard layout (this is not documented). To use a specified layout you could use MapVirtualKeyEx.
MapVirtualKey does not take into account whether Shift is pressed or not. To easily get an information if Shift is pressed you could use GetKeyState (unfortunately the WinRT team didn't make it easy to get the state of the modifier keys).
Here is an example of how to translate a virtual key into a character:
Update
Unfortunately this solution is not applicable for Windows Store Apps that must be certified. The certification fails since with MapVirtualKey and GetKeyState unsupported APIs are used. This means also that this solution will much likely not run under WinRT.
For WinRT (example with a TextBox and only select Letters):
For cancel de char entry:
optional:
and modify CharacterReceived:
Warning! if your TextBox has a MaxLength you must modify your code: