Is there any way to convert WPF's KeyEventArgs.Key
to Char
?
I tried to use KeyInterop
:
var x = (Char)KeyInterop.VirtualKeyFromKey(e.Key);
For numbers and letters it works fine, but for other characters it doesn't.
E.g. for OemComma
it returns '1/4' instead of ','.
I need it to prevent user of inputting into TextBox
any characters except numbers and separators (i.e. commas or periods).
UPDATE: the comment below found the original http://stackoverflow.com/a/5826175/187650
Found this somewhere that I've forgotten. Still using it, not sure what holes it has, but it's worked as far as I know.
public static class KeyEventUtility
{
// ReSharper disable InconsistentNaming
public enum MapType : uint
{
MAPVK_VK_TO_VSC = 0x0,
MAPVK_VSC_TO_VK = 0x1,
MAPVK_VK_TO_CHAR = 0x2,
MAPVK_VSC_TO_VK_EX = 0x3,
}
// ReSharper restore InconsistentNaming
[DllImport( "user32.dll" )]
public static extern int ToUnicode(
uint wVirtKey,
uint wScanCode,
byte[] lpKeyState,
[Out, MarshalAs( UnmanagedType.LPWStr, SizeParamIndex = 4 )]
StringBuilder pwszBuff,
int cchBuff,
uint wFlags );
[DllImport( "user32.dll" )]
public static extern bool GetKeyboardState( byte[] lpKeyState );
[DllImport( "user32.dll" )]
public static extern uint MapVirtualKey( uint uCode, MapType uMapType );
public static char GetCharFromKey( Key key )
{
char ch = ' ';
int virtualKey = KeyInterop.VirtualKeyFromKey( key );
var keyboardState = new byte[256];
GetKeyboardState( keyboardState );
uint scanCode = MapVirtualKey( (uint)virtualKey, MapType.MAPVK_VK_TO_VSC );
var stringBuilder = new StringBuilder( 2 );
int result = ToUnicode( (uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0 );
switch ( result )
{
case -1:
break;
case 0:
break;
case 1:
{
ch = stringBuilder[0];
break;
}
default:
{
ch = stringBuilder[0];
break;
}
}
return ch;
}
}
My first suggestion would be using PreviewTextInput and using the TextCompositionEventArgs. However this 'eats' the space-characters. For this 'space'-issue see:
another solution would be using the KeyConverter in a KeyUp event:
KeyConverter kc = new KeyConverter();
var str = kc.ConvertToString(e.Key);
For WPF: https://msdn.microsoft.com/en-us/library/system.windows.input.keyconverter(v=vs.110).aspx For Winforms: https://msdn.microsoft.com/en-us/library/system.windows.forms.keysconverter.aspx
Note that KeyConverter will give you the string "Space" instead of " ". (same for "Esc" when pressing the escape key.
Another WPF solution is using this in the KeyUp event:
((char)KeyInterop.VirtualKeyFromKey(e.Key))
Why such approach? There are different ways of preventing users from performing such input. For example, define an event-handler for the TextBox.TextChanged
-event and when the input is not a decimal or a separator, revoke the input.
private void textChangedEventHandler(object sender, TextChangedEventArgs args){
//args contains a property `Changes` where you can see what happened
//check for invalid characters and revoke them
}
See TextChangedEventArgs and the event.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With