Search code examples
firemonkeydelphi-10-seattle

How works the OnMouseDown event on my MainForm with Firemokey?


I am using Tmainform.OnKeyDown and it fires always correctly, besides the controls or frames added to the form.

I need the same behavior for OnMouseDown.

My goal is to track activity of the user. After x minutes with no keyboard nor mouse clicks I want to close the application.

Edit: TMainForm.OnMouseDown never gets fired. I don't want to do anything with the event, just know that the user is alive and clicking.


Solution

  • For the Form to see keystrokes prior to the active control you need to set the KeyPreview property within the forms Object Inspector.

    You can also do this via code: Form1.KeyPreview := True;

    There is a substantial explanation in the accepted answer here: How does Delphi's KeyPreview work?

    Regarding your mouse query, how do you know it isn't working if you're not doing anything there?

    Put this code into your forms OnMouseDown event;

    PROCEDURE TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    BEGIN
      CASE Button OF
        mbLeft:   showmessage('Left Mouse Button!');
        mbRight:  showmessage('Right Mouse Button!');
        mbMiddle: showmessage('Middle Mouse Button!');
      END;
    END;
    

    I hope this is helpful and answers your question.