简体   繁体   English

使用鼠标按钮的WPF全局热键

[英]WPF Global Hotkey using the mouse button

So, I'm using this example to create a global hotkey in a WPF application: 因此,我使用此示例在WPF应用程序中创建全局热键:

https://blog.magnusmontin.net/2015/03/31/implementing-global-hot-keys-in-wpf/ https://blog.magnusmontin.net/2015/03/31/implementing-global-hot-keys-in-wpf/

It's pretty straightforward and works fine... but whenever I try to change the key to the mouse button, the middle one for example, it just does not work... In the list of keys from msdn, there is: 它非常简单并且可以正常工作……但是,每当我尝试将鼠标键更改为键(例如中间键)时,它都无法正常工作……在msdn的键列表中,有:

https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx https://msdn.microsoft.com/zh-CN/library/windows/desktop/dd375731(v=vs.85).aspx

VK_MBUTTON 0x04 Middle mouse button (three-button mouse) VK_MBUTTON 0x04鼠标中键(三键鼠标)

Whenever I try to change the hotkey to any listed key, it works fine, except for mouse buttons.. anyone know why this happens and how to fix it? 每当我尝试将热键更改为任何列出的键时,它都可以正常工作,除了鼠标按钮之外。.有人知道为什么会发生这种情况以及如何解决它?

CapsLock is a key so can be registered as an HotKey . CapsLock是一个密钥,因此可以注册为HotKey The mouse middle button is a button , not a key. 鼠标中键是一个按钮 ,而不是一个键。 You simply can't use the same native method/code. 您根本无法使用相同的本机方法/代码。

I suggest you to adopt the globalmousekeyhook library for your application. 我建议您为应用程序采用globalmousekeyhook库。 In this case the sample that you find on Magnus Montin's blog will become: 在这种情况下,您在Magnus Montin博客上找到的示例将变为:

public partial class MainWindow : Window
{
    private IKeyboardMouseEvents m_GlobalHook;

    public MainWindow()
    {
        InitializeComponent();
    }

    protected override void OnSourceInitialized(EventArgs e)
    {
        m_GlobalHook = Hook.GlobalEvents();
        m_GlobalHook.MouseClick += m_GlobalHook_MouseClick;

        base.OnSourceInitialized(e);
    }

    private void m_GlobalHook_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Middle)
        {
            tblock.Text += "Middle mouse button clicked" + Environment.NewLine;
        }
    }

    protected override void OnClosed(EventArgs e)
    {
        m_GlobalHook.MouseClick -= m_GlobalHook_MouseClick;
        m_GlobalHook.Dispose();

        base.OnClosed(e);
    }
}

The only problem is that globalmousekeyhook library refers to System.Windows.Forms assembly and I do not know if it can be acceptable for you. 唯一的问题是globalmousekeyhook库引用System.Windows.Forms程序集,我不知道它是否可以为您接受。

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

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