jasonpang / Interceptor

C# wrapper for a Windows keyboard driver. Can simulate keystrokes and mouse clicks in protected areas like the Windows logon screen (and yes, even in games). Wrapping http://oblita.com/Interception
MIT License
286 stars 84 forks source link

Unloading From Event Handler #22

Open j opened 3 years ago

j commented 3 years ago

If I want to listen to a key press and unload on that key press, how do I go about it?

Unload() hangs when executing within the EventHandler.

/cc @jasonpang

j commented 3 years ago

Continue'd Info: I was trying to unload on specific key press to disable / enable keyboard listening.

Jamisco commented 3 years ago

As for why Unload() is hanging, if mousefiltermode is not set to none, Unload will hang because there is a thread that this API uses and for whatever reason the program cannot terminate that thread.

Also, you cannot change keyboard/mousefilter modes after the Load() method has been called.

Now that saying, you can simply use the Keypress event which is offered by this API, and then using a simple if statement, check if the key pressed matches the key want to detect, if it does, just call the unload() method.

j commented 3 years ago

@Jamisco I was using an event handler on the key press event. I wasn't configuring mouse at all, so I'll have to check on that. I ended up making it a UI application with a click-able button to load/unload which worked fine, though I'd like to create a hot key to Load/Unload.

Regarding mouse listeners, are you saying that using them, there will be threads that never end even after application closes? I didn't get a chance to check this.

Jamisco commented 3 years ago

First, off to answer your main question

` Input InputDispenser = new Input(); InputDispenser.KeyboardFilterMode = KeyboardFilterMode.All; InputDispenser.MouseFilterMode = MouseFilterMode.None; InputDispenser.Load();

    private void InputDispenser_OnKeyPressed(object sender, KeyPressedEventArgs e)
    {
        if (e.Key == Keys.F)
        {
                     if(InputDispenser.IsLoaded)
                     {
                               InputDispenser.Unload();
                      }
                      else
                     {
                               InputDispenser.Load();
                     }   
            InputDispender.Unload();
        }
    }`

This should work, I dont see any reason as to why it shouldnt

Regarding the moust listeners, Yes that is my experience. There seems to be an unstoppable thread which cannot be terminated even when you close the program and or visual studio all together. The thread randomly hijacks mouse functions and if you want your mouse back, you have to restart the computer. Atleast that has been my experience thus far