6

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;
    }
nneonneo
  • 171,345
  • 36
  • 312
  • 383
Programer
  • 1,005
  • 3
  • 22
  • 46
  • I assume at the moment you use your card reader as keyboard emulator. Does it have any SDK you can use? Usually such devices come with drivers and controls/APIs you can use in your apps. – StaWho Apr 19 '12 at 07:22
  • it is just a keyboard input. the fact that it is a card reader or just a mumble jumble i hit on the keyboard doesnt matter for this case. – Programer Apr 19 '12 at 08:14
  • Are there any other events you could use? – raznagul Apr 19 '12 at 08:47
  • PreviewKeyDown I guess... KeyPress maybe....any event that the keyboared will trigger is ok – Programer Apr 19 '12 at 08:49

3 Answers3

0

Ok I am making a number of assumptions here:

  1. The value is typed into a text box.
  2. 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;
    }
}
Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60
  • it is not a text box, it is an event handler to all keyDown events in a window, and it does not use the enter key – Programer Apr 19 '12 at 08:04
  • 1
    I think you need to find a different solution then, the WPF KeyEventArgs doesn't provide the keyed value like the winforms version. – Trevor Pilley Apr 19 '12 at 08:17
  • 1
    I know that, therefore my question. There must be a way of acheiveing that in wpf as well – Programer Apr 19 '12 at 08:49
  • 1
    Does this help http://christoph.ruegg.name/blog/2007/5/8/capture-keyboard-input-in-wpf.html? – Trevor Pilley Apr 19 '12 at 08:55
  • it looks nice, but it receives one element to add the handler to. I want the handler in my entire solutin as follows : EventManager.RegisterClassHandler(typeof(Window), Window.KeyDownEvent, new KeyEventHandler(keyPress)); – Programer Apr 19 '12 at 09:40
-1

Try casting it:

char c = (char)e.KeyCode;
Eddie Flores
  • 1,093
  • 13
  • 14
-2
string keylog=""; // do global declaration
//use the bellow line in event handler
keylog= keylog +  e.Key.ToString();
Thiliban
  • 74
  • 4