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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Setting mouse to ignore outside of _NewImage PhilOfPerth 11 712 12-18-2025, 07:20 PM
Last Post: Pete
  Do Loop, Sleep and Mouse Button Dimster 5 579 09-06-2025, 12:57 PM
Last Post: Dimster
  Stopping repeated mouse-key press PhilOfPerth 8 851 09-02-2025, 11:28 PM
Last Post: Pete
  Enemy movement Proof Of Concept ukdave74 1 473 06-03-2025, 05:51 AM
Last Post: aadityap0901
  Repeating mouse check PhilOfPerth 4 914 07-24-2024, 11:26 PM
Last Post: PhilOfPerth

Forum Jump:


Users browsing this thread: