简体   繁体   English

Concat KeyDown事件(Keys)到一个C#(wpf)字符串

[英]Concat KeyDown event (Keys) to one C# (wpf) string

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): 我正在使用KeyDown事件获取此对象(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. 我试图连接e.Key.ToString() ,但这不起作用。 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). 我的输入有很多数字和符号(例如;?=等),而e.Key.ToString()的东西不起作用(它给了我一个数字的D3,而SHIFT或CTRL +另一个键用于迹象)。

I just want the string, so when I use for example Console.WriteLine I will get something like 我只想要字符串,所以当我使用例如Console.WriteLine时,我会得到类似的东西

;51895401051=000001341?

and not 并不是

Oem1SHIFTD1CNTRLD2D3D2D1SHIFTD9OemQuestion ....

I tried using KeyConverter but I was unable to figure this out. 我尝试使用KeyConverter,但我无法解决这个问题。

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. TypedText是我想要将键连接到的字符串。 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: 下面的GetCharFromKey函数获取一个Key(你应该发送e.Key)并返回一个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;
    }

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;
    }
}

尝试投射它:

char c = (char)e.KeyCode;
string keylog=""; // do global declaration
//use the bellow line in event handler
keylog= keylog +  e.Key.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM