QB64 Phoenix Edition
MouseInApp - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Prolific Programmers (https://qb64phoenix.com/forum/forumdisplay.php?fid=26)
+---- Forum: SMcNeill (https://qb64phoenix.com/forum/forumdisplay.php?fid=29)
+---- Thread: MouseInApp (/showthread.php?tid=2041)



MouseInApp - SMcNeill - 09-27-2023

As per posts in a few different topics, here's a simple way to determine if a mouse is inside the program window or not.  (This works just for Windows, but you Linux/Mac guys should be able to substitute for whatever the equivalent of the Windows API call of GetCursorPos is.  I just don't know your OS commands good enough to know what to use for it.)


Code: (Select All)
SCREEN _NEWIMAGE(1280, 720, 32)
_MOUSEMOVE 640, 360

DO
    CLS
    IF MouseInApp THEN PRINT "Mouse is in program area!" ELSE PRINT "The mouse has left the building!"
    _LIMIT 30
    _DISPLAY
LOOP UNTIL _KEYDOWN(27)

FUNCTION MouseInApp
    TYPE MIA_point_type: AS LONG x, y: END TYPE
    DECLARE DYNAMIC LIBRARY "user32": FUNCTION GetCursorPos (lpPoint AS MIA_point_type): END DECLARE
    DECLARE LIBRARY: FUNCTION glutGet& (BYVAL what&): END DECLARE
    DIM AS LONG DX, DY
    DIM Dmouse AS MIA_point_type 'Desktop mouse
    result = GetCursorPos(Dmouse)
    DX = Dmouse.x - _SCREENX - glutGet(506): DY = Dmouse.y - _SCREENY - glutGet(506) - glutGet(507)
    IF DX >= 0 AND DX <= _WIDTH AND DY >= 0 AND DY <= _HEIGHT THEN MouseInApp = -1
END FUNCTION