问题
I have a magnetic card reader that uses the keyboard input to send data. I am using a KeyDown event where I get this object (C# WPF):
KeyEventArgs e
I want to take the keys that I get and make them one string.
I tried to concat e.Key.ToString()
, but that doesn't work. My input has lots of numbers and signs (such as ; ? = etc.), and the e.Key.ToString() thing doesn't work (it gives me D3 for a number, and SHIFT or CTRL + another key for the signs).
I just want the string, so when I use for example Console.WriteLine I will get something like
;51895401051=000001341?
and not
Oem1SHIFTD1CNTRLD2D3D2D1SHIFTD9OemQuestion ....
I tried using KeyConverter but I was unable to figure this out.
Can someone please help me?
My current event handler (which does not work properly) is:
public static void keyPress(Object sender, KeyEventArgs e)
{
string keyCodeString = e.Key.ToString();
char? key = null;
if (keyCodeString.Length == 1)
{
key = keyCodeString[0];
}
else
{
if (keyCodeString.StartsWith("NumPad"))
{
key = keyCodeString[keyCodeString.Length - 1];
}
else if (keyCodeString.StartsWith("D"))
key = keyCodeString[keyCodeString.Length - 1];
}
TypedText += key;
}
Where TypedText is the string I want to concat the keys to. The output results was explained above.
I solved it myself. Here is the answer. The GetCharFromKey
function below gets a Key (you should send e.Key) and returns a char:
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,
}
[DllImport("user32.dll")]
public static extern bool GetKeyboardState(byte[] lpKeyState);
[DllImport("user32.dll")]
public static extern uint MapVirtualKey(uint uCode, MapType uMapType);
[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);
public static char GetCharFromKey(Key key)
{
char ch = ' ';
int virtualKey = KeyInterop.VirtualKeyFromKey(key);
byte[] keyboardState = new byte[256];
GetKeyboardState(keyboardState);
uint scanCode = MapVirtualKey((uint)virtualKey, MapType.MAPVK_VK_TO_VSC);
StringBuilder 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;
}
回答1:
Ok I am making a number of assumptions here:
- The value is typed into a text box.
- The card reader uses the enter key when it has entered all the values.
If the above is true, you can do this:
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
var valueEntered = cardReaderValue.Text;
}
}
回答2:
Try casting it:
char c = (char)e.KeyCode;
回答3:
string keylog=""; // do global declaration
//use the bellow line in event handler
keylog= keylog + e.Key.ToString();
来源:https://stackoverflow.com/questions/10223297/concat-keydown-event-keys-to-one-c-sharp-wpf-string