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  
#1 Posted : Saturday, January 8, 2022 5:42:05 PM(UTC)
Rob

Rank: Administration

Reputation:

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

Thanks: 28 times
Was thanked: 416 time(s) in 354 post(s)

Step 1:


SAVE ANY OPEN DOCUMENTS - Start testing these scripts under the assumption that things could get mucked up and you might need to reboot the computer to get unstuck!

I've tried to build in some resiliency/risk mitigation, but things like this are complicated and when you're hooking into the mouse events, things can go sideways and leave you in an unpredictable state.


Step 2:


Go to Options > Advanced and check the box Enable Mouse Hook Event Subscription.



Step 3:


Go to Global Actions > Load/Unload > Load tab and paste this script:

Code:
// We only want to create this event binding in one (the last) script engine
if(__spEngineWrapper.Engine.Name == sp.EngineList().Last().Engine.Name) {

    //This toggles on and off a synchronous mouse button hook event subscription
    //If event already bound, disconnect
    var mouseButtonEventObj = sp.GetStoredObject("mouseButtonEvent");
    if(mouseButtonEventObj.GetType().FullName.includes('EventConnection')) 
    {
        mouseButtonEventObj.disconnect();
        sp.DeleteStoredObject("mouseButtonEvent");
    } 
    else 
    {
        // Otherwise, create the synchronous event binding and define the callback
        // function which is called on each mouse button down and up event
        var mouseButtonEvent = MouseHook.OnMouseHookButtonEvent.connect(
            function (sender, mouseHookEvent) 
            {
                //Wrap all code in try/catch, exceptions will crash S+
                try 
                {
                    // Get stored PieMode variable to determine how to handle the mouse event
                    var pieMode = StrokesPlus.StoredValues.Strings.Get("PieMode");
        
                    // If there's something in the string value, meaning the action has indicated we're in pie mode
                    // Otherwise, we ignore the mouse click and pass it along
                    if(pieMode)
                    {            
                        // Initialize pieButton 
                        var pieButton = "MouseButtons.None";

                        // Set the appropriate pieButton value so we know which one is
                        // being held for the pie menu
                        switch(pieMode)
                        {
                            case "LMB":
                                pieButton = "MouseButtons.Left";
                                break;
                            case "MMB":
                                pieButton = "MouseButtons.Middle";
                                break;
                            case "RMB":
                                pieButton = "MouseButtons.Right";
                                break;
                        }

                        // Only perform the actions on mouse button released (after full click)
                        if(mouseHookEvent.ButtonState == ButtonState.Up)
                        {
                            // If the LMB is released, then select the pie item
                            // else if right click up, then send Escape
                            if(mouseHookEvent.Button == MouseButtons.Left)
                            {
                                // Release the pieButton - we put this in a timer so it occurs in a separate
                                // thread, so we don't get hung within this event

                                StrokesPlus.Timer.New("pieButtonRelease", 10, -1, `StrokesPlus.Input.Mouse.Button.Release(${pieButton});`);

                                // Clear the PieMode variable
                                StrokesPlus.StoredValues.Strings.Dispose("PieMode");
                            }
                            else if(mouseHookEvent.Button == MouseButtons.Right)
                            {
                                // Press the Escape key
                                StrokesPlus.Input.Keyboard.Keys.Press(vk.ESCAPE);

                                // Release the pieButton - we put this in a timer so it occurs in a separate
                                // thread, so we don't get hung within this event
                                StrokesPlus.Timer.New("pieButtonRelease", 10, -1, `StrokesPlus.Input.Mouse.Button.Release(${pieButton});`);

                                // Clear the PieMode variable
                                StrokesPlus.StoredValues.Strings.Dispose("PieMode");                            
                            }
                        }

                        // If we're in Pie Mode, instruct the S+ mouse hook to consume the event 
                        // so apps/Windows don't see the click as happening
                        mouseHookEvent.Consume = true;
                    }
                }
                catch{}                    
            }
        );
        //Note that on S+ reload, events in Stored Object list have disconnect called on them
        sp.StoreObject("mouseButtonEvent", mouseButtonEvent);
    }
}



Step 4:


For your current pie menu actions, use this script - changing MouseButtons.Left and "LMB" to either "MMB" or "RMB" accordingly:
Code:
// Press down the Control key
StrokesPlus.Input.Keyboard.Keys.Hold(vk.LCONTROL);

// Wait 10 milliseconds
StrokesPlus.Delay.Sleep(10);

// Press down the Shift key
StrokesPlus.Input.Keyboard.Keys.Hold(vk.LSHIFT);

// Wait 10 milliseconds
StrokesPlus.Delay.Sleep(10);

// Press and hold Left Mouse Button down
StrokesPlus.Input.Mouse.Button.Hold(MouseButtons.Left);

// Wait 10 milliseconds
StrokesPlus.Delay.Sleep(10);

// Release the Control key
StrokesPlus.Input.Keyboard.Keys.Release(vk.LCONTROL);

// Release the Shift key
StrokesPlus.Input.Keyboard.Keys.Release(vk.LSHIFT);

// Store a variable so the mouse event script knows we're in pie mode
// and we're handling the LMB pie (to know which button to release)
StrokesPlus.StoredValues.Strings.Set("PieMode", "LMB");

// Create a timer that will clear the PieMode variable after 10000
// milliseconds (10 seconds) - this is a fail safe to ensure you don't end up 
// with mouse clicks being locked up (consumed) if there is an unexpected issue
StrokesPlus.Timer.New("PieModeClear", 10000, -1, `StrokesPlus.StoredValues.Strings.Dispose("PieMode");`);  

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.