MediatedCommunications / WindowsInput

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

How do i unhook this code? #26

Open anastasiosskg opened 10 months ago

anastasiosskg commented 10 months ago

I took this code from your main page, but when i close my program, it still running.

` public static void Main(){ using (var Keyboard = WindowsInput.Capture.Global.KeyboardAsync()) { //Capture all events from the keyboard Keyboard.KeyEvent += Keyboard_KeyEvent; Console.ReadLine(); } }

private static void Keyboard_KeyEvent(object sender, EventSourceEventArgs e) {

if(e.Data?.KeyDown?.Key == WindowsInput.Events.KeyCode.A || e.Data?.KeyUp?.Key == WindowsInput.Events.KeyCode.A) {
    e.Next_Hook_Enabled = false;
}

}`

TonyValenti commented 10 months ago

Hi @anastasiosskg - If your process exists, the code will not be able to run. Take a look at windows task manage and you'll see that your EXE is still running.

anastasiosskg commented 10 months ago

Thank you for your respond! Am new btw at c#, so i have simple program, i have :

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    { 
        Application.Exit();
    }

so before i use the above code capture key, when i debug and close the application, it closes normally, but when i use the code to capture keys, when i debug and close the application, it still runs at visual studio, i have to press the stop button to stop the program. Is there a way to put in MainForm_FormClosing so when i close th program, it will close normally ?

TonyValenti commented 10 months ago

In your form closing event, make sure to dispose the keyboard class.

anastasiosskg commented 10 months ago

I have form2 as main application, and in class form2 at public form2(){ } i have :

        InitializeComponent();
        //Keyboard and mouse handler
        var Keyboard = WindowsInput.Capture.Global.KeyboardAsync();
        //Capture all events from the keyboard
        Keyboard.KeyEvent += Keyboard_KeyEvent;

and am trying to put Keyboard.Dispose(); in closing_form2 which has Application.Exit(); after dispose, and when i put Keyboard.Dispose(); before exit, i get red underline.

I have try in closing to put :

        var Keyboard = WindowsInput.Capture.Global.KeyboardAsync();
        Keyboard.KeyEvent -= Keyboard_KeyEvent;
        Keyboard.Dispose();

but again the same result, even when i put public form2(){} :

        //Keyboard and mouse handler
        var Keyboard = WindowsInput.Capture.Global.KeyboardAsync();
        //Capture all events from the keyboard
        Keyboard.KeyEvent += Keyboard_KeyEvent;
        Keyboard.Dispose();

i get again the same result. Could you please write me what i have to write and where so the application could exit normally ?

btw, what e.Next_Hook_Enabled = false; means?
And when i use this capture and my program is running, i cannot use physically to use the classic cntrl+c or v, i can use those, when i close the program only.