SetMousePosition

Hello,

Is it possible to set the Mouse position, x, y, at a specified position in a newly opened window?

Thanks.

Lennox

with native XOJO commands? no
with platform specific declares? perhaps

With plugin?
MBS ComputerControl plugin has methods for this. See RemoteControlMBS module here in reference:

http://monkeybreadsoftware.net/pluginpart-remotecontrol.shtml

Mac:
https://forum.xojo.com/12285-move-mouse-cursor-and-click-by-code-for-mac/0

Windows:
http://forums.realsoftware.com/viewtopic.php?f=1&t=42784

This is just my opinion:

This is greatly perturbing: you have your mouse at 100, 55 and all of a sudden, you got a brand new window AND the mouse disappears !

Have a look (if you can) to one of El Capitan new features: if you move faster the mouse, the cursor expand by itself (yes, the Mouse Cursor expands in SIZE alone): after that, you know where on the screen is your Mouse Cursor. Great feature ! Especially with nowadays large sized monitors (mine is 3360 x 2100).
And in previous MacBook(s) I also had trouble seeking the MouseCursor (on a 15" monitor ! 1440 x 900 only !)

Now, it is your software and you do not explain why you want that feature (help for disabled persons ?). Out of context…

Happy Christmas everybody!

There are circumstances where placing the cursor on a button when approaching it can be of help to impaired people. I also made some research into positioning the mouse and click to automate software that is not scriptable (the Mac link above was started by me).

But I do concur that moving the mouse cursor is just like taking away the wheel from a car (as in the infamous Jeep hack). IMHO, it is absolutely not ergonomic.

As far as Mac is concerned, this is not allowed in a sandboxed app, so if the OP wants to get in the MAS, it is not a good idea.

On the subject of the growing cursor, I can see it only when I jiggle real fast about 100 pixels around a point. The idea is good, but the gesture is not very evident. Thank you for mentioning it, though. I had not read anything about it before.

You’re welcome Michel.

When I am searching the MouseCursor I do that hoping to reach it (visually)… and so, this is how I discovered that new function that I really love.

Thanks to all.

This does it, combined effort from…
Jason King 25 May 2014 Beta Testers
Axel Schneider is not verified 23 May 2015 Europe (Germany, Erfurt)
Michel Bujardet 25 May 2014 Beta Testers, Xojo Pro #JeSuisParis Edited 2 years ago by Michel Bujardet

#If TargetWin32 // if on Windows
// http://forums.realsoftware.com/viewtopic.php?f=1&t=42784

'paste this in a pushbutton
'
'Declare sub SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer)' As Integer
'setcursorpos val(x.text), val(y.text)
'
'make 2 text boxes named x & y
'click the pushbutton to set it's position.

Declare Sub SetCursorPos Lib "user32" (ByVal x As Integer, ByVal y As Integer) 'as Integer
setcursorpos (Window1.Left + btn1.Left + btn1.Width/2, Window1.Top + btn1.Top + btn1.Height/2)

#elseIf TargetMacOS// if on Mac
// https://forum.xojo.com/12285-move-mouse-cursor-and-click-by-code-for-mac

'Jason King 25 May 2014 Beta Testers
'I needed to move the mouse a while back, so here is my code. This code will center the mouse in the middle of a window.
'
'//Structure CGPoint:
'//////////
'//x as single
'//y as single
'//////////
'
'Declare function CGWarpMouseCursorPosition lib "ApplicationServices" (newPoint as CGPoint) as Ptr
'dim pt as CGPoint
'//center mouse in the window
'pt.x = self.Width/2 + self.Left
'pt.y = self.Height/2 + self.Top
'call CGWarpMouseCursorPosition(pt)
'I'll look into clicking the mouse now.

'https://forum.xojo.com/12285-move-mouse-cursor-and-click-by-code-for-mac
'
'Axel Schneider is not verified 23 May 2015 Europe (Germany, Erfurt)
'@Jason King Here is an example project with the function and the previous function to move the mouse position:
// 'https://www.dropbox.com/s/jajxbkhj0rdtldv/mouse%20manipulation.xojo_binary_project 
'Thanks, very helpful.
'
'an additional method to place mouse in center of a Control
'SetCursorToRectControl (btn1, mainwin)

SetCursorToRectControl (btn1, self)

#Endif

// 'https://forum.xojo.com/12259-move-mouse-position-click-by-code-windows/0

'Michel Bujardet 25 May 2014 Beta Testers, Xojo Pro #JeSuisParis Edited 2 years ago by Michel Bujardet
'Here is the final method.

'To use, call click(x,y,click)

'The click parameter is true for a click, false for a simple mouse cursor move. x and y are relative to the screen. So to make it relative to the window, it is necessary to take into account self.left and self.right. The advantage of this method is that it can click all over the screen, not limited to your app, opening automation possibilities.

'Sub click(x as integer, y as integer, click as boolean)
'#if TargetWin32
'if click = true then
'Declare sub mouse_event Lib “user32” (ByVal dwFlags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As Integer)

'const MOUSEEVENTF_ABSOLUTE = &h8000
'const MOUSEEVENTF_LEFTDOWN = &h2
'const MOUSEEVENTF_LEFTUP = &h4
'const MOUSEEVENTF_MIDDLEDOWN = &h20
'const MOUSEEVENTF_MIDDLEUP = &h40
'const MOUSEEVENTF_MOVE = &h1
'const MOUSEEVENTF_RIGHTDOWN = &h8
'const MOUSEEVENTF_RIGHTUP = &h10
'const MOUSEEVENTF_WHEEL = &h800
'const MOUSEEVENTF_XDOWN = &h80
'const MOUSEEVENTF_XUP = &h100
'const MOUSEEVENTF_HWHEEL = &h1000

'Dim cur_x As integer
'Dim cur_y As integer
'Dim dest_x As integer
'Dim dest_y As integer
'Dim dx As integer
'Dim dy As integer

'dest_x = (65535/screen(0).width)*x
'dest_y = (65535/screen(0).height)*y

'call mouse_event(MOUSEEVENTF_ABSOLUTE+MOUSEEVENTF_MOVE+MOUSEEVENTF_LEFTDOWN+MOUSEEVENTF_LEFTUP,dest_x, dest_y, 0, 0)
'else
'Declare Function SetCursorPos Lib “user32” (ByVal x As Integer, ByVal y As Integer) As Integer
'//==========================================
'// Set cursor position to top left of screen
'//==========================================
'call SetCursorPos(x,y)
'end if
'#Endif

'End Sub