需要发送鼠标和键盘事件WPF C#
我工作的一个teamviewer型应用程序(需要移动另一台PC的光标,看到我的键盘打字那里)。是否有可能从我的(客户端)侧(的MouseMove,MOUSEBUTTONDOWN等)捕获事件,并直接把他们注入到另一个(服务器)的一面?
I'm working on a teamviewer-like application (need to move the cursor of an another pc and see the typing of my keyboard there). Is it possible to capture events from my (client) side ( MouseMove, MouseButtonDown, etc) and inject them directly to the other (server) side?
我想知道如果存在WPF的功能,如这些Win32的:
I want to know if exists WPF functions like these win32:
SendMessage();
PostMessage();
SendInput();
如果没有,如何使用这些??
If not, how to send WPF Events using these??
在此先感谢
您可能会钩住你的自我了这些冒泡的路由事件,真正的参数没有抓住,即使它们被处理的所有事件的,即使它们是子控件内。因此,在你的视觉层次结构的顶部添加它们的地方:)
You may hook your self up on these bubbling routed events, the true param there grabs all events even if they are handled, and even if they are inside child controls. So add them at the top of your visual hierarchy somewhere :)
AddHandler(KeyUpEvent, new KeyEventHandler(OnKeyUp), true);
AddHandler(MouseUpEvent, new MouseButtonEventHandler(OnMouseUp), true);
private void OnMouseUp(Object sender, MouseButtonEventArgs e)
{
// Post/Send message, SendInput or whatever here..
}
private void OnKeyUp(Object sender, KeyEventArgs e)
{
// Post/Send message, SendInput or whatever here..
}
和是的,你可以通过互操作使用SendMessage函数,PostMessage的和SendInput功能WPF。
And yes you may use SendMessage, PostMessage and SendInput functions in WPF through interop.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool PostMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
internal static extern UINT SendInput(UINT nInputs, [MarshalAs(UnmanagedType.LPArray), In] INPUT[] pInputs, int cbSize);
然后你只需使用互操作注入您的邮件。是的,也可以勾你的自我到WPF中Winproc传。 详情请参阅此帖子。
pinvoke.net 是为W32 C#互操作的良好来源,但东西告诉我你已经意识到了这一点:)你有这里发送输入的例子
pinvoke.net is a good source for w32 C# interop, but something tells me you are already aware of this :) You have an example of sending inputs here.
希望它帮助。