Wednesday, February 01, 2017

Foot Powered macOS Monitor Hopping

We interrupt this emacs hack for an OS X hack.

Yesterday, after making progress with the Trinket Pro, I grabbed the foot pedal, wire strippers and athletic tape of all things, and connected everything together:

Yes, this is a pathetic build. No, I don't care. I loaded the Trinket with this small program:

#include <cmdline_defs.h>
#include <ProTrinketKeyboard.h>
#include <ProTrinketKeyboardC.h>
#include <usbconfig.h>


const int PIN_SWITCH = 0;    // Trinket pin to connect to switch 
const int LED        = 13;
int   pressed        = 0;
void setup()
{
  pinMode(PIN_SWITCH, INPUT_PULLUP);
  TrinketKeyboard.begin();  // initialize keyboard library
}

void loop()
{
  TrinketKeyboard.poll();
  if (digitalRead(PIN_SWITCH) == LOW)  // If the foot switch grounds the pin
  {
    if(pressed == 0) {
      TrinketKeyboard.pressKey(0, KEYCODE_F8);
      TrinketKeyboard.pressKey(0, 0);  // release key
      pressed = 1;
    }
  } else {
    pressed = 0;
  }
}

Note the use of pressed variable. It's needed to keep the board from sending a stream of key presses when its held down.

With the program above in place, pressing the foot pedal is equivalent to pressing F8 on the keyboard.

My plan was (and is) to wire this into emacs in an intelligent way. Also, I need to find a way to have the pedal respect the current modifier keys. That is, if I'm holding down shift, then it should send Shift + F8. But all that aside, once I had working pedal, I knew which annoyance I wanted to fix first.

OS X's support for multiple desktops is quite solid. If a screen has focus, it's trivial to use key combinations to navigate among desktops. What doesn't have an obvious solution is jumping the focus between screens. This is the sort of thing I found requests for, but not an obvious solution. Here's one I rigged up.

First off, I grabbed Keyboard Maestro, a piece of software mentioned in my search for a solution. Keyboard Maestro looks quite sophisticated and getting my bearings was non trivial. From poking around I learned two important pieces of information. First, the variable %CurrentMouse% contains the coordinates of where the mouse is. Second, when I'm on the 'left' screen, the x-coordinate is negative. Using these facts, I was able to rig up this rule:

By detecting the presence of the minus sign, I can tell which screen I'm on and warp the mouse to the other monitor. Finally, this is all connected to F8, which is sent by the foot pedal.

I can't tell you how gratifying this solution is. Now, when I'm on the right screen, I no longer need to remove my hands from the home row, but can just tap my foot to warp myself to the left screen. And I'm only a foot press away from being back to the right screen. It's so cool.

No comments:

Post a Comment