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 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 6:47:21 AM(UTC)
| Reason: Not specified
|
1 user thanked Rob Otter for this useful post.
|
|