需要发送鼠标和键盘事件WPF C#
我正在研究一个像团队合作者的应用程序(需要移动另一个电脑的光标,并在那里看到我的键盘打字)。可以从我的(客户端)方面捕获事件(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();
如果没有,如何使用这些发送WPF事件?
If not, how to send WPF Events using these??
提前感谢
您可以将自己挂在这些冒泡上路由事件,真正的param在那里抓住所有的事件,即使他们是处理,即使他们在儿童控制内。因此,将它们添加到您的视觉层次结构的顶部:)
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..
}
是的,您可以通过interop使用WPF中的SendMessage,PostMessage和SendInput函数。
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);
然后你只需使用interop注入你的消息。是的,也可以将自己挂钩到WPF中的winproc。 看到这篇文章的详细信息。
Then you just inject your messages using interop. And yes it is also possible to hook your self onto a winproc in WPF. See this post for details.
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.
希望有帮助。