2

What I want to do is implement a TextBox which accepts a key combination as the input. For example, the text Win + Ctrl + A appears when these keys are pressed (Windows Forms or WPF).

A nice example is in Winamp settings, under "Global Hotkeys".

Tibi
  • 4,015
  • 8
  • 40
  • 64
  • @DJKRAZE I think he wants to the text box to show him what was pressed. Not send out the presses. – DaveShaw Feb 23 '12 at 17:44
  • he could do that as well by capturing the keys and if there is a 3 key combination of keys pressed he code get at if (e.KeyCode == Keys.Enter && Key.CTRL && key.H) for example do this on the KeyDownEvent.. this should help get you started.. if (e.KeyCode == Win + Crl + A) append Key. for the Win, Ctrl, A – MethodMan Feb 23 '12 at 17:50
  • That is not a pretty way to do it, and you can't get all modifier keys (such as WinKey, which is treated like a normal key, as I read in another source). – Tibi Feb 23 '12 at 17:52
  • This was just an example of what you could do.. you have to have some sort of Key.Code that you want to compare it to.. this you will have to determine what will be the best approach based on using Key.Code – MethodMan Feb 23 '12 at 17:54
  • Here is a Link MSDN you can also use for a list of all Keys http://msdn.microsoft.com/en-us/library/xy8ebtbf%28vs.71%29.aspx – MethodMan Feb 23 '12 at 17:56
  • The message filter looks like a much cleaner and powerful solution, so I will go with that one. Thanks for the suggestion anyway :D – Tibi Feb 23 '12 at 17:59

2 Answers2

3

This is how you can detect almost all keys that are being pressed: source: How to detect the currently pressed key?

private KeyMessageFilter m_filter = new KeyMessageFilter();

private void Form1_Load(object sender, EventArgs e)
{
    Application.AddMessageFilter(m_filter);

}


public class KeyMessageFilter : IMessageFilter
{
    private Dictionary<Keys, bool> m_keyTable = new Dictionary<Keys, bool>();

    public Dictionary<Keys, bool> KeyTable
    {
        get { return m_keyTable; }
        private set { m_keyTable = value; }
    }

    public bool IsKeyPressed()
    {
        return m_keyPressed; 
    }

    public bool IsKeyPressed(Keys k)
    {
        bool pressed = false;

        if (KeyTable.TryGetValue(k, out pressed))
        {
            return pressed;                  
        }

        return false; 
    }

    private const int WM_KEYDOWN = 0x0100;

    private const int WM_KEYUP = 0x0101;

    private bool m_keyPressed = false;


    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == WM_KEYDOWN)
        {
            KeyTable[(Keys)m.WParam] = true;

            m_keyPressed = true;
        }

        if (m.Msg == WM_KEYUP)
        {                
            KeyTable[(Keys)m.WParam] = false;

            m_keyPressed = false;
        }

        return false;
    }
}
Community
  • 1
  • 1
Ben
  • 6,107
  • 6
  • 29
  • 40
0

Check this out :

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    HotKeyCls h = new HotKeyCls() { Ctrl = e.Control, Alt = e.Alt, Shift = e.Shift, Key = e.KeyCode };
    textBox1.Text = h.ToString();
}

HotKeyCls :

//Coded by Amen Ayach's DataClassBuilder @23/02/2012
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class HotKeyCls{

    private bool _Ctrl;
    public bool Ctrl{
        get {
            return _Ctrl;
        }
        set {
            _Ctrl = value;
        }
    }

    private bool _Shift;
    public bool Shift{
        get {
            return _Shift;
        }
        set {
            _Shift = value;
        }
    }

    private bool _Alt;
    public bool Alt{
        get {
            return _Alt;
        }
        set {
            _Alt = value;
        }
    }

    private System.Windows.Forms.Keys _Key;
    public System.Windows.Forms.Keys Key
    {
        get {
            return _Key;
        }
        set {
            _Key = value;
        }
    }

    public override string ToString()
    {
        return (Ctrl ? "Ctrl+" : "") + (Shift ? "Shift+" : "") + (Alt ? "Alt+" : "") + Key.ToString();           
    }
}
Amen Ayach
  • 4,288
  • 1
  • 23
  • 23