Part Four: Pressing Any Key
Did you get
onPoseEdge working? Here’s my implementation:
function onPoseEdge(pose, edge) myo.debug("onPoseEdge: " .. pose .. ": " .. edge) if (edge == "on") then if (pose == "waveOut") then onWaveOut() elseif (pose == "waveIn") then onWaveIn() elseif (pose == "fist") then onFist() elseif (pose == "fingersSpread") then onFingersSpread() end end end
We only care about when a
pose starts, so
edge must be
on every time. There is no
else condition in the block checking for poses either, since there are other poses we are not handling at this time.
If you load this up you will notice an error message in the log each time you try one of those gestures. Don’t worry, that just means we haven’t implemented any of those functions. Let’s do that now.
Myo scripts are really all about using the Myo armband to trigger certain key presses. Once you’ve detected that the user is using the right app and they’ve done the required gesture with the Myo, you will trigger a key or mouse button press.
To press a key you will use the
myo.keyboard(keyname, edge, modifiers..) function. This takes the name of any key, an
edge (one of
press), and any number of modifiers (
up correspond to whether the key is being press down or released.
press is down then up right away. You’ll have to figure it out within the app you are scripting for whether down, up, or press is more appropriate.
Let’s build something really simple and generic. You can get pretty far in a lot of applications with a few keyboard commands. “Tab” usually takes you to the next thing you can focus on. “Shift-tab” will take you backwards. “Enter” will select something. “Esc” will back up or take you to a menu.
waveOut to tab,
waveIn to tab with shift pressed,
fist to enter, and
fingersSpread to escape. Try implementing each of those functions we defined to trigger those key presses, and print some debug output so you can verify that’s the command you are inputting. You can find out more information on what keys are called in the scripting API reference.
This is what I did:
function onWaveOut() myo.debug("Next") myo.keyboard("tab", "press") end function onWaveIn() myo.debug("Previous") myo.keyboard("tab","press","shift") end function onFist() myo.debug("Enter") myo.keyboard("return","press") end function onFingersSpread() myo.debug("Escape") myo.keyboard("escape", "press") end
Pretty straightforward. But also pretty powerful. You could modify things a bit like using the
edge value for the
edge parameter to let users hold down buttons if they wanted, but we’re going to leave it as is for now. There is another interesting thing here that you may or may not have noticed: Try switching your Myo to your other arm and (after performing the setup gesture) tab around.
Notice that? Wave out is still away from your body. If you started on your left arm, the wave out to go forward might feel better now. The Myo UX guidelines recommend waving to the “right” to be forward, and “left” to be back. We’ll need to switch wave out and wave in ourselves to support left handed use.
For you to do that, all you need to know is how to determine which arm the Myo is on. Simply enough, it’s
myo.getArm(), which returns
unknown. Write a helper function that switches
waveOut if the user is wearing their Myo on their left arm, and call it before your logic handling which pose the user just did.
Good luck! I’ll give you my solution tomorrow.