0

Hello Stack Overflow community!

This is my function that catches every pressed key:

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

To have a little formating, I using this function that replaces me characters with new one:

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

But I want replace D* (D1, for example) with number, if Shift key is pressed. It is possible? And if not, what is better method for key logging than buffer all pressed keys? Thanks a lot!

  • 1
    Is that text from [`System.Windows.Input.KeyEventArgs.Key`](https://msdn.microsoft.com/en-us/library/system.windows.input.keyeventargs.key%28v=vs.110%29.aspx) in [`System.Windows.Input.KeyEventArgs`](https://msdn.microsoft.com/en-us/library/System.Windows.Input.KeyEventArgs%28v=vs.110%29.aspx) (WPF) or from [`System.Windows.Forms.Keys`](https://msdn.microsoft.com/en-us/library/system.windows.forms.keys.aspx) in [`System.Windows.Forms.KeyEventArgs`](https://msdn.microsoft.com/en-us/library/System.Windows.Forms.KeyEventArgs%28v=vs.110%29.aspx) (winforms)? – dbc Aug 29 '15 at 18:31
  • @dbc no, I added my function in man thread, check it out. –  Aug 29 '15 at 18:35
  • You can use [`Keyboard.Modifiers`](https://msdn.microsoft.com/en-us/library/system.windows.input.keyboard.modifiers%28v=vs.110%29.aspx) to see what modifier keys are currently pressed. Is that what you want? – dbc Aug 29 '15 at 18:44
  • Or also [`Control.ModifierKeys`](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.modifierkeys%28v=vs.110%29.aspx). – dbc Aug 29 '15 at 18:49
  • @dbc Thanks for second one! I will try it out! :) –  Aug 29 '15 at 18:52

1 Answers1

0

You can use Control.ModifierKeys (WinForms) or Keyboard.Modifiers (WPF) to see what modifier keys are currently pressed. For instance:

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

Note that Keyboard.Modifiers are a bitmask field. Thus the following code clears a specific modifier when they current keypress event corresponds to that modifier:

    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;
    }
dbc
  • 104,963
  • 20
  • 228
  • 340
  • Thank you, I will now try it out! But I have little question: for key combinations with Shift use Keys.Shift or Keys.ShiftKey ?? –  Aug 29 '15 at 19:48
  • @PepinCZ - the dictionary `allModifierKeys` gives a mapping from modifier key to modifier. – dbc Aug 29 '15 at 19:56
  • Man but... I got his result when typing with shift and altgr...: –  Aug 29 '15 at 20:06
  • `[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] A Z NUMPADU JE TO 13 A NEVIM CO MAM DELAT [] [] [] [] [] [] []. [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []( [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []( [] []( [] []( [] []( [] []( [] []( [] [] []( [] []( [] []( [] []( [] []( [] [] [] [] [] [] [] [] [] [] [] [] [] [] []` –  Aug 29 '15 at 20:06
  • I'm not seeing that. I'll need to see your method `GetAsyncKeyState()` and also how you bind it in to your program to help much further. – dbc Aug 29 '15 at 20:18
  • Thats all: `private static extern short GetAsyncKeyState(int vKey);` –  Aug 29 '15 at 20:24
  • Maybe its bad cause I'm using CZECH keyboard –  Aug 29 '15 at 20:29
  • @PepinCZ - you must have a DllImport declaration for it somewhere. – dbc Aug 29 '15 at 20:35
  • @PepinCZ - OK, so can you update your question to include it please? – dbc Aug 29 '15 at 20:37
  • @PepinCZ - there may be a problem with your `ReplaceChars()` method. For the key `RControlKey` it returns `"R"` which is probably not what you want. What does your code do if you remove this method? – dbc Aug 29 '15 at 20:43
  • @PepinCZ - if you are printing your output to the console, you may need to set its encoding to Utf8. See [c# unicode string output](http://stackoverflow.com/questions/5055659/c-sharp-unicode-string-output). – dbc Aug 29 '15 at 21:15