MediatedCommunications / WindowsInput

Capture and Simulate Keyboard and Mouse Input
MIT License
111 stars 17 forks source link

Sending mouse inputs while using global hook to disable mouse usage #12

Closed msalai closed 2 years ago

msalai commented 3 years ago

Is it possible to send mouse inputs while global mouse hook is active in a way that it block mouse usage? I wish to block mouse by user and allow simulation only from within my program.

TonyValenti commented 3 years ago

@msalai - I think what you want to do is ignore certain mouse inputs to keep them inside your app.

Play around with code like this:

using System;

namespace ConsoleApp32 {
    class Program {
        static void Main(string[] args) {
            var H = WindowsInput.Capture.Global.MouseAsync();
            H.MouseMove += H_MouseMove;
            H.MouseEvent += H_MouseEvent;

            Console.ReadLine();
        }

        private static void H_MouseEvent(object sender, WindowsInput.Events.Sources.EventSourceEventArgs<WindowsInput.Events.Sources.MouseEvent> e) {
            e.Next_Hook_Enabled = false;
        }

        private static void H_MouseMove(object sender, WindowsInput.Events.Sources.EventSourceEventArgs<WindowsInput.Events.MouseMove> e) {
            e.Next_Hook_Enabled = false;
        }
    }
}

You'll need to modify it so that it doesn't ignore events that happen inside your app.

msalai commented 3 years ago

I put bool value: static bool MyApp = false;

which I set to true before simulating mouse movement (for example) then I immediately put it to false. I used timer just for testing purpose: private void timer1_Tick(object sender, EventArgs e) { MyApp = true; WindowsInput.Simulate.Events().MoveBy(1, 1).Invoke(); MyApp = false; }

and in event handler I used code like this: private static void H_MouseEvent(object sender, WindowsInput.Events.Sources.EventSourceEventArgs<WindowsInput.Events.Sources.MouseEvent> e) { if (!MyApp) e.Next_Hook_Enabled = false; }

Now it works partially, because I can still disrupt cursor position by my mouse sometimes. Rarely, but still not perfect as I wish it to be. Is there any way to know who sent event?

TonyValenti commented 3 years ago

The WindowsInput library is a low-level library that works by injecting/filtering directly into the device's input buffers. Because of that, you will need to use another method (like the Win32 GetCurrentWindow() api or such) to find the app that was likely the target of the input.

thavious commented 3 years ago

I don't understand the Next_Hook_Enabled / Next_Event_Enabled, how does this relate to the old e.Handled = true scenario where you just want to tell the next hook in line that I've already taken care of this, disregard?

thavious commented 3 years ago

Also to the original comment here, the old way was to use:

(e.Flags & LLKHF_INJECTED) == LLKHF_INJECTED) is injected (e.Flags & LLKHF_INJECTED) == 0) is from the keyboard

to see if the event was an injected event (i.e. put in by your code and not from an actual keyboard) and use that to determine whether you wanted to use/discard it.

btw LLKHF_INJECTED = 0x10;

I wrote a program to handle multiboxing using the old InputSimulator / GlobalMouseKeyHook and was testing this library to see if it worked any better than my old code, but I have been running into several scenarios where your changes are not documented very well enough to get a decent test program together run it through it's paces.

Is there a way to allow the flags to be passed to the event? Is there a reason you hide this from the person using the library?

TonyValenti1 commented 3 years ago

Hi @thavious - Next_Hook_Enabled controlls whether the character that was pressed should be 'consumed' by your app or whether it should be passed down the chain. Next_Event_Enabled is for if you have multiple event handlers attached to the same instance in your app and you want to short-circuit future delegates that would normally be invoked.

Let me take a look at LLKHF_INJECTED and see if I can expose that.

TonyValenti commented 3 years ago

@thavious - I just pushed an update that exposes a RawData property on events that lets you view the data you were mentioning.

thavious commented 3 years ago

Thanks!