Welcome Guest! To enable all features please Login or Register.



Go to last post Go to first unread
Rob Otter  
#1 Posted : Thursday, October 29, 2020 6:00:56 AM(UTC)
Rob Otter

Rank: Advanced Member


Groups: Approved
Joined: 10/26/2020(UTC)
Posts: 50
Location: Darmstadt

Thanks: 15 times
Was thanked: 2 time(s) in 2 post(s)
Hi folks,

after discovering this great tool and that it offers endless possibilities, I decided to port some of my small tools to sp+n, reducing the number of standalone single-service simple-tools I had developed in AHK and AutoIt over the last years.

This is the first one, as I need it a lot and found it very easy to rewrite. On hotkey press, it will limit your mouse movement to either horizontal or vertical, whatever direction you move your mouse first.
Restriction is removed with the next hotkey press. This is a very basic version, stripped down to what is really needed. I hope you might find it useful and it could serve as an inspiration for further extending it. Please let me know what you think about it!

Please be aware that you will need at least sp+n version for it to work (which Rob has just released).

    Restrict mouse movement to one dimension - horizontal or vertical.
    Mouse may move over the whole screen estate even on multi-monitor setup.
    Version: 1.0
    Date:    2020-10-28
    Author:  Dirk Schwarzmann

    Minimum SP+ Net version:

// You may configure these values to your likings:

// Measure mouse movement for x milliseconds to compute main direction or standstill.
// After this time, functionality will be cancelled or mouse will be pinned
// to its current position (see next setting).
var g_timeoutMouseMove_ms = 2000;

// If true, pin the mouse cursor if it was not moved within the timeframe set above,
// else terminate script without doing anything.
var g_pinMouse = true;

// Minimum mouse move distance (in Pixel) in any direction to judge movement as not standstill.
// Must be greater than 0.
var g_minMoveDistance_px = 5;

// ============================================================================
// Main:
// ============================================================================

// Rectangle holding the movement area on restriction
var restrictRect;
// The root point to work on
var mStartPoint = sp.GetCurrentMousePoint();

if (sp.MouseRestrictActive()) {
    // Restriction active, so remove it
    ShowInfo("Deactivate mouse restriction", mStartPoint);
} else {
    // Start restriction
    ShowInfo("Activate mouse restriction", mStartPoint);
    var mMainDir = GetMouseMoveMainDirection(g_timeoutMouseMove_ms, g_minMoveDistance_px, mStartPoint);

    switch (mMainDir) {
        case 0:
            if (g_pinMouse) {
                // Pin mouse cursor to its current position
                restrictRect = new Rectangle(mStartPoint.X, mStartPoint.Y, 0, 0);
                ShowInfo("Standstill", mStartPoint);
            } else {
                ShowInfo("Doing nothing, exiting", mStartPoint);
        case 1:
            // Restrict to horizontal movement
            restrictRect = new Rectangle(sysinfo.VirtualScreen.X, mStartPoint.Y, sysinfo.VirtualScreen.Width, 0);
            ShowInfo("Horizontal", mStartPoint);
        case 2:
            // Restrict to vertical movement
            restrictRect = new Rectangle(mStartPoint.X, sysinfo.VirtualScreen.Y, 0, sysinfo.VirtualScreen.Height);
            ShowInfo("Vertical", mStartPoint);

// ============================================================================
// Helper functions:
// ============================================================================

    Returns the main direction of the mouse movement within a given timeframe.
    0 = standstill
    1 = horizontal (X direction)
    2 = vertical (Y direction)

    Return type: int
function GetMouseMoveMainDirection(mTimeout, mMinMove, mStartPoint) {
    var startTime =;
    var direction = 0;
    var mPoint, dX, dY;

    do {
        mPoint = sp.GetCurrentMousePoint();
        dX = Math.abs(mStartPoint.X - mPoint.X);
        dY = Math.abs(mStartPoint.Y - mPoint.Y);

        if (dX > mMinMove || dY > mMinMove) {
            direction = dX > dY ? 1 : 2;
    } while (direction < 1  && ( - startTime) < mTimeout);

    return direction;

function ShowInfo(msg, screenPoint) {
    var info = new DisplayTextInfo();
    info.Message = msg;
    info.Duration = 1000;
    info.Location = (screenPoint.X + 10) + "," + (screenPoint.Y + 10); 
    info.MessageFont = new Font("Segoe UI Semibold", 12);
    info.BackColor = "gray";
    info.Opacity = 0.7;
    info.Padding = 10;

Rob Otter

Edited by user Thursday, October 29, 2020 6:47:21 AM(UTC)  | Reason: Not specified

thanks 1 user thanked Rob Otter for this useful post.
Rob on 10/29/2020(UTC)
#2 Posted : Thursday, October 29, 2020 6:16:53 AM(UTC)

Rank: Administration


Groups: Translators, Members, Administrators
Joined: 1/11/2018(UTC)
Posts: 952
United States
Location: Tampa, FL

Thanks: 22 times
Was thanked: 274 time(s) in 239 post(s)

FYI, you can use SystemInformation.VirtualScreen which is the rectangle for the entire virtual desktop instead of the unions. SystemInformation is exposed as sysinfo to scripts, so sysinfo.VirtualScreen.

sp.MessageBox(`Virtual Screen Rectangle:
, "Virtual Desktop");

var allScreensRect = sysinfo.VirtualScreen;
thanks 1 user thanked Rob for this useful post.
Rob Otter on 10/29/2020(UTC)
Rob Otter  
#3 Posted : Thursday, October 29, 2020 6:48:21 AM(UTC)
Rob Otter

Rank: Advanced Member


Groups: Approved
Joined: 10/26/2020(UTC)
Posts: 50
Location: Darmstadt

Thanks: 15 times
Was thanked: 2 time(s) in 2 post(s)
Thanks Rob, that comes in handy and I didn´t know about that yet. I have updated the code accordingly, simplifying it even more.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.