Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mouse movement
#1
For @Tempodibasic -- Here's a quick work up of how you can get and work with Mousemove X/Y values within Windows.  Going this route keeps our coordinates in sync with the actual _MOUSEX and _MOUSEY coordinates for us, rather than generating the eventual "drift" which occurs with _mousemovementX and _mousemovementy due to rounding glitches adding up over time.

Code: (Select All)
SCREEN _NEWIMAGE(1280, 720, 32)
_MOUSEMOVE 640, 360
WHILE _MOUSEINPUT: WEND
TYPE point_type
    x AS LONG
    y AS LONG
END TYPE
DIM Dmouse AS point_type 'Desktop mouse

DECLARE DYNAMIC LIBRARY "user32"
    FUNCTION GetCursorPos (lpPoint AS point_type)
END DECLARE
DECLARE LIBRARY
    FUNCTION glutGet& (BYVAL what&)
END DECLARE

BorderWidth = glutGet(506)
TitleBarHeight = glutGet(507)

dx = -_SCREENX - BorderWidth: dy = -_SCREENY - BorderWidth - TitleBarHeight 'these are adjusted to match screen coordinates.

DO
    CLS
    WHILE _MOUSEINPUT: WEND
    result = GetCursorPos(Dmouse)
    MMX = _MOUSEX - oldx: MMY = _MOUSEY - oldy
    DMX = Dmouse.x - oldDx: DMY = Dmouse.y - oldDy
    x = x + MMX: y = y + MMY
    dx = dx + DMX: dy = dy + DMY
    PRINT "MousemoveX:"; MMX
    PRINT "MousemoveY:"; MMY
    PRINT "Mousemove New X Coordinate:"; x
    PRINT "Mousemove New Y Coordinate:"; y
    PRINT "Actual X Coordinate:"; _MOUSEX
    PRINT "Actual Y Coordinate:"; _MOUSEY
    PRINT
    PRINT "Desktop MousemoveX:"; DMX
    PRINT "Desktop MousemoveY:"; DMY
    'Remove the remarks below to get TRUE desktop coordinates.
    'As it is, we modified these so that they'll match the current QB64PE screen coordinates.
    PRINT "Desktop Mousemove New X Coordinate:"; dx '+ BorderWidth
    PRINT "Desktop Mousemove New Y Coordinate:"; dy '+ BorderWidth + TitleBarHeight
    PRINT "Desktop Actual X Coordinate:"; Dmouse.x
    PRINT "Desktop Actual Y Coordinate:"; Dmouse.y
    _LIMIT 30
    _DISPLAY
    oldx = _MOUSEX: oldy = _MOUSEY
    oldDx = Dmouse.x: oldDy = Dmouse.y
LOOP UNTIL _MOUSEBUTTON(2)
Reply
#2
(09-26-2023, 11:11 AM)SMcNeill Wrote: For @Tempodibasic -- Here's a quick work up of how you can get and work with Mousemove X/Y values within Windows.  Going this route keeps our coordinates in sync with the actual _MOUSEX and _MOUSEY coordinates for us, rather than generating the eventual "drift" which occurs with _mousemovementX and _mousemovementy due to rounding glitches adding up over time.

Code: (Select All)
SCREEN _NEWIMAGE(1280, 720, 32)
_MOUSEMOVE 640, 360
WHILE _MOUSEINPUT: WEND
TYPE point_type
    x AS LONG
    y AS LONG
END TYPE
DIM Dmouse AS point_type 'Desktop mouse

DECLARE DYNAMIC LIBRARY "user32"
    FUNCTION GetCursorPos (lpPoint AS point_type)
END DECLARE
DECLARE LIBRARY
    FUNCTION glutGet& (BYVAL what&)
END DECLARE

BorderWidth = glutGet(506)
TitleBarHeight = glutGet(507)

dx = -_SCREENX - BorderWidth: dy = -_SCREENY - BorderWidth - TitleBarHeight 'these are adjusted to match screen coordinates.

DO
    CLS
    WHILE _MOUSEINPUT: WEND
    result = GetCursorPos(Dmouse)
    MMX = _MOUSEX - oldx: MMY = _MOUSEY - oldy
    DMX = Dmouse.x - oldDx: DMY = Dmouse.y - oldDy
    x = x + MMX: y = y + MMY
    dx = dx + DMX: dy = dy + DMY
    PRINT "MousemoveX:"; MMX
    PRINT "MousemoveY:"; MMY
    PRINT "Mousemove New X Coordinate:"; x
    PRINT "Mousemove New Y Coordinate:"; y
    PRINT "Actual X Coordinate:"; _MOUSEX
    PRINT "Actual Y Coordinate:"; _MOUSEY
    PRINT
    PRINT "Desktop MousemoveX:"; DMX
    PRINT "Desktop MousemoveY:"; DMY
    'Remove the remarks below to get TRUE desktop coordinates.
    'As it is, we modified these so that they'll match the current QB64PE screen coordinates.
    PRINT "Desktop Mousemove New X Coordinate:"; dx '+ BorderWidth
    PRINT "Desktop Mousemove New Y Coordinate:"; dy '+ BorderWidth + TitleBarHeight
    PRINT "Desktop Actual X Coordinate:"; Dmouse.x
    PRINT "Desktop Actual Y Coordinate:"; Dmouse.y
    _LIMIT 30
    _DISPLAY
    oldx = _MOUSEX: oldy = _MOUSEY
    oldDx = Dmouse.x: oldDy = Dmouse.y
LOOP UNTIL _MOUSEBUTTON(2)
it is a gem and also the good answer to Terry Ritchie issue to detect the mouse out of the window of the application.
Reply




Users browsing this thread: 2 Guest(s)