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

Notification

Icon
Error

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

Rank: Advanced Member

Reputation:

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

Thanks: 11 times
Was thanked: 1 time(s) in 1 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 0.3.9.7 for it to work (which Rob has just released).

Code:
/*
    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
    Web:     http://www.dirk-schwarzmann.de
    Mail:    rob@dirk-schwarzmann.de

    Minimum SP+ Net version: 0.3.9.7
*/

// 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);
    sp.MouseRestrictClear();
} else {
    // Start restriction
    ShowInfo("Activate mouse restriction", mStartPoint);
    var mMainDir = GetMouseMoveMainDirection(g_timeoutMouseMove_ms, g_minMoveDistance_px, mStartPoint);
    sp.MouseMove(mStartPoint);

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

// ============================================================================
// 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 = Date.now();
    var direction = 0;
    var mPoint, dX, dY;

    do {
        sp.Sleep(50);
        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  && (Date.now() - 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;
    sp.DisplayText(info);
}


Regards,
Rob Otter

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

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

Rank: Administration

Reputation:

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

Thanks: 16 times
Was thanked: 174 time(s) in 151 post(s)
Awesome!

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.

Code:
sp.MessageBox(`Virtual Screen Rectangle:
X:\t${sysinfo.VirtualScreen.X}
Y:\t${sysinfo.VirtualScreen.Y}
Left:\t${sysinfo.VirtualScreen.Left}
Right:\t${sysinfo.VirtualScreen.Right}
Top:\t${sysinfo.VirtualScreen.Top}
Bottom:\t${sysinfo.VirtualScreen.Bottom}
Width:\t${sysinfo.VirtualScreen.Width}
Height:\t${sysinfo.VirtualScreen.Height}`
, "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 12:48:21 AM(UTC)
Rob Otter

Rank: Advanced Member

Reputation:

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

Thanks: 11 times
Was thanked: 1 time(s) in 1 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.