1

At the moment I am using a example code from the internet, but I want to change it so I can get the value of the mousebutton that is pressed down as an string value to use further in the Form1.cs under a button.

The problem is I can't really get the return value from the mousebutton that is pressed down. It keeps giving me null values. Is this because its a anonymous async method? Im new to async and anonoymous methods, but read some information about this on the internet. Here is the code in the class I used. Also I used the MouseKeyHook NuGet package from George Mamaladze. I want to get the result to return or "save" somewhere so it stays as an constant. Now the result changes directly after the input of the mousebutton, because its probably a async method that runs on the background.

public class ClickDetector
{
    public ClickDetector() { }

     public static void ListenForMouseEvents()
    {

        
        Console.WriteLine("Listening to mouse clicks.");

        //When a mouse button is pressed 
         Hook.GlobalEvents().MouseDown += async (sender, e) =>
        {


            Form1.label1.Text = $"Mouse {e.Button} Down"; //This should modify label1.Text.

            var result = e.Button.ToString();



        };
        //When a double click is made
        Hook.GlobalEvents().MouseDoubleClick += async (sender, e) =>
        {
            Form1.label1.Text = $"Mouse {e.Button} button double clicked."; //This should modify label1.Text.



        };


    }
}
Giorgio R
  • 11
  • 4
  • Yes. I guess the right usage of async await is the key. Take a look at: https://stackoverflow.com/questions/12451609/how-to-await-raising-an-eventhandler-event – George Mamaladze Apr 05 '22 at 07:52

0 Answers0