0

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.

Eduardo Elias
  • 1,742
  • 1
  • 22
  • 49

1 Answers1

0

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.

Craig
  • 68
  • 5
  • For key preview it works fine, however mousedown on firemonkey does nothing. So it means that somehow the events are not getting there because should have something like HitTest := true for the TForm, but I am now aware of it. – Eduardo Elias Nov 09 '17 at 10:47
  • @EduardoElias The form does not have a HitTest property, but the controls do. Are you trying to receive the OnMouseDown for other controls within the Form, in which case set the controls OnMouseDown to the forms OnMouseDown event and use Sender(TObject) to determine which control sent it. Also, have you covered the form with a control where HitTest = False? – Craig Nov 09 '17 at 21:16
  • I have not change anything in the controls yet, since I was looking for something "global". I insert frames with hundreds of controls on the main form according user choice. So I was trying to avoid change the behavior of the frames inserted. My goal is to find if the user is still using the application, after sometime with no user interaction I want to log out. – Eduardo Elias Nov 10 '17 at 22:08
  • @eduardoelias You could set a timer and use the OnMouseMove event I guess. There are probably more elegant ways of doing this but the OnMouseMove would be quick and easy. – Craig Nov 12 '17 at 03:51