4

With Delphi Win32 (VCL) I use:

Application.OnMessage := MyAppMessage;

What is the equivalent in FireMonkey?

I have a routine that needs to catch all the keyboard and mouse events in the application (on all the active form controls) and handle them.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
DelphiFM
  • 133
  • 2
  • 6

3 Answers3

7

I don't know of a way in FireMonkey to capture mouse and keyboard events at the application level in a platform agnostic way. I don't think that has been implemented yet, as of Delphi XE 2 Update 2.

However, by default FireMonkey forms get all of the MouseDown and KeyDown events before the controls do.

If you simply override the MouseDown and KeyDown events on your form, you'll accomplish the same thing.

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
  private
    { Private declarations }
  public
    { Public declarations }
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Single); override;
    procedure KeyDown(var Key: Word; var KeyChar: System.WideChar; Shift: TShiftState); override;
  end;

{ TForm1 }

procedure TForm1.KeyDown(var Key: Word; var KeyChar: System.WideChar;
  Shift: TShiftState);
begin
  // Do what you need to do here
  ShowMessage('Key Down');
  // Let it pass on to the form and control
  inherited;
end;

procedure TForm1.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
  Y: Single);
begin
  // Do what you need to do here
  ShowMessage('Mouse Down');
  // Let it pass on to the form and control
  inherited;
end;

If you want, you can keep going with MouseMove, MouseUp, MouseWheel, MouseLeave, KeyUp, DragEnter, DragOver, DragDrop, and DragLeave.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
  • thank you, your routine can solve my problem. I see that FireMonkey forms, differently from VCL forms, get by default all of the MouseDown and KeyDown events before the controls do. – DelphiFM Nov 09 '11 at 20:29
  • +1 Good for you Marcus. This is now a rather different question which makes my answer look a bit out of place but that's fine, you've helped the asker out. – David Heffernan Nov 09 '11 at 20:42
  • 1
    This does not seem to work when you click on a control that has DragMode=dmAutomatic. Tried it with MouseUp. – Domus Jan 10 '12 at 23:28
6

FireMonkey is cross platform and runs on Windows, Mac OSX, iOS and no doubt many other platforms in due course. Therefore there are no Windows messages exposed by FireMonkey.

Whatever it is you are used to doing with OnMessage in the VCL most likely has an equivalent in FireMonkey. Exactly what that equivalent is depends very much on what your OnMessage handler is trying to achieve.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Nice answer, David. What I tried to say more briefly in my comment. – Marcus Adams Nov 08 '11 at 22:01
  • 1
    @Marcus Yes I know I was just repeating your comment. It was worthy of an answer. If nobody answers then I think that can look bad. – David Heffernan Nov 08 '11 at 22:02
  • OnMessage,huh? Maybe he's using some kind of IPC through messages - and using OnMessage to detect message arrival.... – Fabricio Araujo Nov 09 '11 at 15:56
  • @David, thank you for the reply. In myApp I have a routine that needs to catch all the keyboard and mouse events in the application (on all the active form controls) and handle them – DelphiFM Nov 09 '11 at 16:34
  • I think you need to ask that as a specific question. I don't know the answer to it. Ask a new question. – David Heffernan Nov 09 '11 at 16:37
0

These answers are fine for exposed events, but for other obscure system events it's more tricky. At the time of writing this link isn't answered:

capturing-usb-plug-unplug-events-in-firemonkey

but it will help with solving the general problem.


I would have posted this as a comment not an answer, but the previous answers were not accepting further comments.

Community
  • 1
  • 1
radsdau
  • 495
  • 5
  • 16