实施快捷方式编辑器TextBox的最佳方法是什么? [英] What is the best way to implement a shortcut editor TextBox?

查看:72
本文介绍了实施快捷方式编辑器TextBox的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是实现一个TextBox,它接受一个键组合作为输入.例如,当按下这些键(Windows窗体或WPF)时,将显示文本Win + Ctrl + A.

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).

一个很好的例子是在Winamp设置的全局热键"下.

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

推荐答案

这是您可以检测到几乎所有被按下的键的方法: 来源:如何检测当前按下的键?

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

这篇关于实施快捷方式编辑器TextBox的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆