I am trying to use declares on OSX to move a cursor position to the middle of the screen. The declares work on Windows 10, but I must be missing something with declares on OSX (El Capitan 10.11.6 on Mac Mini with 16 GB Ram). On OSX the cursor enters the OpenGLControl and the cursor disappears (it shouldn’t be disappearing) and the mouse no longer responds to movement. Does anyone have a suggestion for the following code to get the mouse position and set the mouse position? Thanks for your help.
[code]Sub GetCursorPos(ByRef lpPoint as PointAPI) as Boolean
//Get the position of the cursor #If TargetWin32 Then
Soft Declare Function GetCursorPos Lib “user32” (ByRef lpPoint as POINTAPI) As Boolean
Return GetCursorPos(lpPoint) #Endif
#If TargetMacOS Then
Soft Declare Function CGEventCreate Lib “ApplicationServices” (Source as Ptr) as Ptr
Dim Aptr as Ptr
Aptr = CGEventCreate(Nil)
Dim MyPoint as CGPoint
Soft Declare Function CGEventGetLocation Lib “ApplicationServices” (MyTarget as Ptr) as CGPoint
MyPoint = CGEventGetLocation(Aptr)
lpPoint.x = CType(MyPoint.x, integer)
lpPoint.y = CType(MyPoint.y, Integer)
Soft Declare Sub CFRelease Lib “ApplicationServices” (Ref as Ptr)
CFRelease(Aptr)
Return True #Endif
End Sub[/code]
Sub SetCursorPos(x as integer, y as integer) as Integer
//Change the position of the cursor
#If TargetWin32 Then
Soft Declare Function SetCursorPos Lib "user32" (ByVal x As Integer,ByVal y As Integer) As Int32
Return SetCursorPos(x, y)
#Endif
#If TargetMacOS Then
Soft Declare Function CGWarpMouseCursorPosition lib "ApplicationServices" (NewPoint as CGPoint) as Ptr
Dim pt as CGPoint
pt.x = CType(x, Single)
pt.y = CType(y, Single)
Call CGWarpMouseCursorPosition(pt)
Return 0
#Endif
End Sub
I am compiling for 32 bit on both machines. I can’t remember where I read it, and is seems like Mac preferred the use of CGWarpMouseCursorPosition instead of other declares. Mac is not my strong point and I could be wrong on any of this.
Thanks for the suggestions and I’ll give your suggestions a go after a good nights sleep.
I gave almost of the suggestions a try and I wasn’t able to create the CGEventCreateMouseCreate declare because I don’t know how to do this on a Mac.
Changing the library names and the structure type didn’t seem to change the behaviour of the program, as the mouse becomes invisible and doesn’t move. Because there is no mouse cursor, I have to force-quit the application.
I ran this code in a Pushbutton, which is basically your SetCursorPos, and it works correctly.
[code]Sub Action() Handles Action
dim x, y As integer = 500
Soft Declare Function CGWarpMouseCursorPosition lib “ApplicationServices” (NewPoint as CGPoint) as Ptr
Dim pt as CGPoint
pt.x = CType(x, Single)
pt.y = CType(y, Single)
Call CGWarpMouseCursorPosition(pt)
End Sub
CGPoint with Singles
[/code]
Can you reproduce this in a simple project or have you only tested in your full project?
Actually, what you describe, the cursor disappearing and no longer moving, sounds like another CG function that detaches the cursor from the mouse. It’s used for games where you don’t want the cursor moving but can still get deltas. I’ll look for this later, I remember posting about it.
Well, I made a mistake … The SetCursorPos and GetCursorPos is working correctly when I made a small example program to show what was happening. I am going to dig a little deeper into the larger program as there is likely a logical error that I made … DOH!
The code you suggested works well - my apologies for saying it didn’t work.