3
votes

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

1
I would go this way: Set up an application which can switch between server and client mode (who ever gets monitored and who is controlling). Next part would be streaming the desktop. After that, there are 2 ways: Search for how to set up a mouse and keyboard hook and send the data to the server or work with hover-events. You could probably look for an easy chat-code with already coded server and client to start with. Hard to deal with this because there is no information about how far you are.C4d
WPF is a user interface technology. You can pinvoke Win32 API calls from a C# / .Net application.Lee Willis

1 Answers

2
votes

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.