6

In Firemonkey 2 (FMX2) there is the interface

IFMXMouseService = interface(IInterface) ['{2370205F-CF27-4DF6-9B1F-5EBC27271D5A}']

The interface just has a GetMousePos function. But how can I set the mouseposition in a crossplattform way? Any ideas anybody?

The best idea I came up with yet is to do a conditionaly compile until the possibility is existing in FMX - but I do not know how to set the mouseposition via Delphi for MACOSX. I would be thankful for any help.

Roland Kossow
  • 113
  • 1
  • 4
  • I can't for the life of me figure out why you would want to set the position of the mouse. If you are trying to click on some control in another app there are ways of doing so without messing with the user's mouse position. (Like finding/getting the handle for the control and sending it a windows message.) – Marjan Venema Oct 28 '12 at 16:47
  • Hi Marjan. GUI automation is not the reason why I would like to set the mouse position. Hey - even the winapi has a function to do so - so I am obviously not the sole person that wants it. I want to adapt the mouseposition when controls are shutting and opening in an animated way. E.g: If I move the mouse out of control 1 its width is reduced and all other controls are moving to the left. If I cannot set the mouseposition to the left by the widthdelta the user will always have to move the mouse back to the left manually. So I really would like to set the mouseposition on win and mac. – Roland Kossow Oct 30 '12 at 15:01
  • Ah, ok, that scenario never even crossed my mind. Must be because I tend to avoid animations (and sounds). Probably old fashioned, but hey, I like my stuff staying in one place :-) – Marjan Venema Oct 30 '12 at 19:19
  • I understand what you mean. First thing my brother told me was the same. Indeed one of the features that must come as an option. My first impression is also that it might disturb the user in the begining. But especcially on smaller resolution screens it offers big benefit. – Roland Kossow Oct 30 '12 at 23:09

1 Answers1

3

Here is the procedure you need. You will have to add macapi.coregraphics and macapi.cocoatypes to your uses clause.

procedure setmousepos(x,y:single);

var aNSPoint:NSPoint;

begin
  aNSPoint.x:=x;
  aNSPoint.y:=y;
  CGWarpMouseCursorPosition(aNSPoint);
end;

You could of course pass a TPointF in place of X,Y but you still need to set up the NSPoint X and Y separately as NSPoint is different to TPointF.

Regards

Dave Peters
DP Software
www.dpsoftware.com/firemonkey

David Peters
  • 220
  • 1
  • 10
  • Hi - I leave this answer accepted since it solves the problem in the MAC-native-way - but the correct way to implement the functionality in FMX2 would be to use IFMXCursorrService . There is a setcursor function. This was probably done since the cursor must not be set through a mouse device. – Roland Kossow Nov 09 '12 at 11:04
  • @RolandKossow you did all that hard work to quote the interface name and lookup the function names, but completely missed the fact that the type used there is TCursor: it gets and sets the cursor (icon) of the ... mouse cursor. Not the position. – ciuly May 18 '16 at 18:31