SetMousePosition
From Spheriki
Sets the x and y location of the mouse cursor.
Contents |
Usage
- x Number. The x coordinate for the mouse to change position to.
- y Number. The y coordinate for the mouse to change position to.
Examples
SetMousePosition(40, 75);
Moves the mouse to coordinates (40, 75) relative to the top-left of the Sphere video display.
const TOP = 50;
const BOTTOM = 160;
const LEFT = 30;
const RIGHT = 200;
var scr_w = GetScreenWidth();
var scr_h = GetScreenHeight();
var white = CreateColor(255, 255, 255);
var f = GetSystemFont();
while (AreKeysLeft()) GetKey();
while (!AreKeysLeft())
{
f.drawText(0, 0, "Mouse trap! Press any key to quit.");
// Draw bounding box
Line(LEFT, TOP, RIGHT, TOP, white);
Line(RIGHT, TOP, RIGHT, BOTTOM, white);
Line(RIGHT, BOTTOM, LEFT, BOTTOM, white);
Line(LEFT, BOTTOM, LEFT, TOP, white);
// Get mouse location
var x = GetMouseX();
var y = GetMouseY();
// Trap mouse!
if (x < LEFT) x = LEFT;
if (x > RIGHT) x = RIGHT;
if (y < TOP) y = TOP;
if (y > BOTTOM) y = BOTTOM;
SetMousePosition(x, y);
// Draw mouse location
Line(0, y, scr_w, y, white);
Line(x, 0, x, scr_h, white);
// Show everything
FlipScreen();
}
Traps the mouse in a box. The mouse can be moved around, but will not leave the box. Press any key to end the code sample.
Notes
- The coordinates to be supplied are relative to the top-left corner of the Sphere video display. If the Sphere graphics driver has a different output scale (e.g. doubled display resolution), the mouse will still be moved according to the desktop resolution scale. Mouse handling is independent of output scaling.
See also
- AreKeysLeft()
- CreateColor()
- FlipScreen()
- Font.drawText()
- GetKey()
- GetSystemFont()
- GetScreenHeight()
- GetScreenWidth()
- Line()