![](/img/trans.png)
[英]Is it possible to catch Win7 multitouch events, convert them to mouse and keyboard events and send them to another application?
[英]Is it possible to handle Keyboard/Mouse events application wide in WPF?
我意識到這個問題之前可能已被問過,但我找不到這個十年的答案所以我希望答案已經改變並且是“是”。
我正在開發一個沒有GUI可以說的應用程序,因此沒有“主窗口”來“捕獲”鼠標或鍵盤事件。
我看過Gma.HookManager項目,但它都是為WinForms編寫的,試圖讓它適應WPF讓我想把酗酒作為一種愛好......
是否可以將鼠標/事件處理程序編寫到實際的應用程序類中,即使應用程序中沒有窗口或其他GUI,這些事件也會在按鍵和/或鼠標事件中被捕獲?
我寫了一個快速的utillity類,用gp在wpf中做你需要的東西。
確保在項目中添加對gma庫和Windows窗體的引用。 我很快就對它進行了測試,效果很好,如果您有任何疑問,請告訴我。 堆棧溢出的新功能btw不確定我是否收到通知。
編輯:我的解決方案涉及創建自己的包裝器,如果您將圍繞gma事件,使用系統KeyInterop類將關鍵數據從Windows窗體鍵事件轉換為wpf關鍵數據。
這是班級
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;
}
}}
這是使用它的一個例子。
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());
}
}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.