Search code examples
c#mousemouseeventscreensaver

Defining a MouseEventHandler in C#


I'm trying to define MouseEventHandlers such that the application will exit whenever the mouse is clicked or moved or whenever a key is pressed. This is my first time using C#, but based on what I found online, I've written the code as follows:

     MouseDown += new MouseEventHandler(mouseClickedResponse);
     MouseMove += new MouseEventHandler(mouseMovedResponse);
     KeyDown += new KeyEventHandler(keyResponse);

which connects to:

private void keyResponse(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void mouseClickedResponse(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void mouseMovedResponse(object sender, EventArgs e)
    {
        if (firstCall)     //Keeps the application from exiting immediately
            firstCall = false;
        else Application.Exit();
    }

The problem that I'm finding is that while the KeyEventHandler works perfectly, I can move and click the mouse as much as I want to no avail.

This is the sum total of the code that I've written to allow for user control; am I missing something?


Solution

  • I fixed my problem--my Form was filled with Panels, and by moving the code for mouse input over to the panels, everything worked instantly.