MouseX ( )
Parameters
| None. |
Description
|
Returns the horizontal position of the mouse pointer. The value is measured inside your display, so 0 is the left edge and GraphicsWidth()-1 is the right edge, whether you are running windowed or full screen. Together with MouseY it is everything you need to draw a custom pointer with DrawImage, to steer a cursor around a menu, or to test whether the pointer is inside a button rectangle before acting on a MouseHit. It reports an absolute position, which is what you want for anything the player points at: menus, inventory screens, level editors, strategy games. For a first-person camera you want relative movement instead - that is MouseXSpeed, which keeps working even when the pointer has run out of screen to move across. The position comes from the window's own mouse messages, so it stops updating while the pointer is somewhere else on the desktop and simply holds the last place it saw. The value is not offset by Origin - it is a plain screen position, so subtract the origin yourself if you have moved it. Gotcha: losing the window focus resets the tracked position to 0, so MouseX reads 0 after an alt-tab until the player moves the mouse over your window again. MoveMouse updates it immediately, so you can warp the pointer and read the new position in the same frame. See also: MouseY, MouseZ, MouseXSpeed, MoveMouse, MouseDown, HidePointer. |
Example
; MouseX Example ; -------------- Graphics 640,480,0,2 SetBuffer BackBuffer() ; Hide the Windows pointer - the example draws its own crosshair HidePointer While Not KeyDown(1) Cls ; MouseX returns the pointer's x position on screen, ; from 0 to GraphicsWidth()-1 mx=MouseX() my=MouseY() ; Vertical range-marker tracks the MouseX position Color 90,90,90 Line mx,0,mx,479 ; Shooting-range crosshair at the full mouse position Color 255,255,0 Oval mx-10,my-10,20,20,False Line mx-14,my,mx+14,my Line mx,my-14,mx,my+14 Color 255,255,255 Text 0,0,"Move the mouse left and right Esc: exit" Text 0,20,"MouseX() = "+mx+" (0 to "+(GraphicsWidth()-1)+")" Flip Wend End
Index