简体   繁体   中英

Is it possible to handle Keyboard/Mouse events application wide in WPF?

I realize that this question has likely been asked before but I can't find an answer from this decade so I'm hoping that the answer has changed and is "Yes".

I'm working on an application that has no GUI to speak of, so there exists no "main window" to "capture" mouse or keyboard events.

I've looked at the Gma.HookManager project but it's all written for WinForms, and trying to adapt it to WPF is making me want to take up alcoholism as a hobby...

Is it possible to write mouse/event handlers into the actual application class such that, even with no windows or other GUI present within the application, those events will pick up on key presses and/or mouse events?

I wrote up a quick utillity class to do what you need in wpf with gma.

Make sure you add references to the gma library and windows forms in your project. I quickly tested it and it works well, let me know if you have any questions. New to stack overflow btw not sure if I get notified.

EDIT: My solution involves creating my own wrapper if you will around the gma events, converting the key data from the windows form key event to wpf key data using the system KeyInterop class.

Here is the class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Gma.UserActivityMonitor;
using System.Windows.Input;
using System.Windows.Forms;

namespace WpfApplicationTest
{
public class HookManager
{

    private GlobalEventProvider _provider;

    public event EventHandler<HookKeyArgs> KeyDown;

    public event EventHandler<HookKeyArgs> KeyUp;

    public HookManager()
    {
        _provider = new Gma.UserActivityMonitor.GlobalEventProvider();

        _provider.KeyDown += _provider_KeyDown;
        _provider.KeyUp += _provider_KeyUp;
    }

    void _provider_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (KeyUp != null)
        {
            KeyUp(this, new HookKeyArgs(convertWinFormsKey(e.KeyData), false, true));
        }
    }

    void _provider_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (KeyDown != null)
        {
            KeyDown(this, new HookKeyArgs(convertWinFormsKey(e.KeyData), true, false));
        }
    }

    System.Windows.Input.Key convertWinFormsKey(System.Windows.Forms.Keys keyMeta)
    {
        Keys formsKey = keyMeta;
        return KeyInterop.KeyFromVirtualKey((int)formsKey);
    }
}

public class HookKeyArgs : EventArgs
{
    public System.Windows.Input.Key KeyPressed {get; private set;}
    public bool IsDown { get; private set; }

    public bool IsUp { get; private set; }

    public HookKeyArgs(System.Windows.Input.Key keyPressed, bool isDown, bool isUp)
    {
        this.KeyPressed = keyPressed;
        this.IsDown = isDown;
        this.IsUp = isUp;
    }


}}

Here is an example of using it.

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var hookManager = new HookManager();

        hookManager.KeyUp += hookManager_KeyUp;
    }

    void hookManager_KeyUp(object sender, HookKeyArgs e)
    {
        MessageBox.Show("key pressed: " + e.KeyPressed.ToString());
    }
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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