简体   繁体   中英

Need to send mouse and keyboard events WPF C#

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?

I want to know if exists WPF functions like these win32:

SendMessage();
PostMessage();
SendInput();

If not, how to send WPF Events using these??

Thanks in advance

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 :)

WPF handlers

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..
}

And yes you may use SendMessage, PostMessage and SendInput functions in WPF through interop.

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);

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 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 .

Hope it helps.

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