We’re back with another episode of #MyoCraft! This time, we’re going to give you a simple script to control your mouse in any application. Now, we’ve covered mouse control in our in-depth Myo Script tutorial series (specifically, Mouse Control Enabled), and that’s still the best path if you really want to understand how to make your own Myo Scripts to do anything you like. But sometimes you just think “Man, I just want to use my Myo like a mouse in this application”. Well, here you go.

  
  


scriptId = 'com.thalmic.mousecontrol'  
    scriptTitle = "Mouse Control"  
    scriptDetailsUrl = ""

    LOCK_THRESHOLD = 800  
    timeSinceLastPose = 0

    description = [[  
    Mouse Control Script

    Control the mouse with your Myo armband! If you want this to be on all the time, make sure you put this at the BOTTOM of your Application Manager list, or nothing else will work. 

    If you would like this to only work for certain applications, follow the directions in onForegroundWindowChange.

    Questions or problems? Talk to us (@thalmicdev)  
    ]]

    link = [[ ]]

    controls = [[  
    Controls:  
     - Move arm to control mouse
     - Fist to left click
     - Fingers spread to right click
     - Wave in middle click
     - Wave out press button 4 (Mac only)
     - Double tap to enable or disable cursor control
     ]]

    knownIssues = [[  
     - button 4 only works on Mac
     ]]

    function notifyUser(edge)  
        if (edge == "down") then
            myo.notifyUserAction()
        end
    end

    function leftClick(edge)  
        notifyUser(edge)
        myo.mouse("left",edge)
    end

    function rightClick(edge)  
        notifyUser(edge)
        myo.mouse("right",edge)
    end

    function middleClick(edge)  
        notifyUser(edge)
        myo.mouse("center",edge)
    end

    function button4Click(edge)  
        notifyUser(edge)
        myo.mouse("button_4",edge)
    end

    function lockMyo(edge)  
        if (myo.getTimeMilliseconds() - timeSinceLastPose > LOCK_THRESHOLD) then
            myo.controlMouse(false)
            myo.lock()
        end
    end

     STANDARD_BINDINGS = {
        fist            = leftClick,
        fingersSpread   = rightClick,
        waveOut         = middleClick,
        wavein          = button4Click,
        doubleTap       = lockMyo
    }
    --STANDARD_BINDINGS = true

    bindings = STANDARD_BINDINGS

    function onForegroundWindowChange(app, title)  
        -- To make this work for a specific application rather than all applications, take these steps:

        -- 1) Delete the following line
        return true

        -- 2) Uncomment (ie, delete -- from) the following line.
        --myo.debug(app)

        -- 3) Load your script into the Application Manager
        -- 4) Turn on Developer Mode in Myo Connect -> Preferences
        -- 5) Open the application you want to control
            -- You should see something pop up in the Myo Debug console along the lines of
            -- something.exe
            -- on Windows, or 
            -- com.something.somethingelse
            -- on Mac. EG: Powerpoint will show you:
            -- POWERPNT.EXE
            -- or
            -- com.microsoft.Powerpoint
        -- 6) Copy and paste the line for your app, and replace YOUR_APP_HERE (leaving the quotes) in the following line
        return app == "YOUR_APP_HERE"

        -- 8) Turn off developer mode, comment out or delete the myo.debug line from step 2, and save and reload your script in the Application Manager. Done!

        -- OPTIONAL
        -- Get stuck or want to do more? Try this tutorial series:
        -- https://www.thalmic.com/blog/myo-script-tutorial-roundup/
    end

    function activeAppName()  
        return scriptTitle
    end

    function onUnlock()  
        myo.unlock("hold")
        myo.controlMouse(true)
    end

    function onPoseEdge(pose, edge)  
        --pose = conditionallySwapWave(pose)
        --myo.debug("onPoseEdge: " .. pose .. ": " .. edge)
        fn = bindings[pose]
        if fn then
            keyEdge = edge == "off" and "up" or "down"
            fn(keyEdge)
        end

        if (pose ~= "rest" and edge == "off") then
            timeSinceLastPose = myo.getTimeMilliseconds()
        end
    end

This script is set up to act as a kind of global mouse control. Similar to the media key script, it will always be active if a connector above it isn’t, and none of the connectors below will ever be active.

That’s pretty useful, but it’s also written in such a way as to make it VERY easy to target to specific applications. Just follow the instructions in onForegroundWindowChange. If the application you are trying to Myo-enable doesn’t use all those buttons (especially button 4, which only works on Mac), feel free to replace some of the mouse buttons with keyboard shortcuts that are more useful. There are some great examples of keyboard input in our adventure game script, if you need a reference.

That’s it for today! Don’t forget to send any of your own ideas for #MyoCraft our way via  MyoCraft@thalmic.com, or write to me on Twitter (@thalmicdev).

See you next week!

Newsletter

Enter your email address and get all latest content delivered to your inbox every now and then.