We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › Control Mac Spaces with a Tap
Page Index Toggle Pages: 1
Control Mac Spaces with a Tap (Read 1317 times)
Control Mac Spaces with a Tap
Oct 2nd, 2008, 12:02pm
 
Quote:
// Screen Knock Version 1
// October 1, 2008
// by Chris Stones
// http://www.welcometochrisworld.com
//
// One of the few programs I pretty much wrote from skratch.
// Special thanks to the SMS Lib though by Daniel Shiffman and others
// Note: Spaces on Mac OS 10.5 needs to be set to use
//       Command + Arrow Keys for this program to work.
//       * For best performance customize
//         KNOCK and wait_for_shake to your system.
//         as in ... you know, use trail and error values until you get it right.

// I had always acted out like I was knocking my laptop
// from one space to the next. But I finally sat down and made
// it do it by itself.

import java.awt.Robot;
import java.awt.event.KeyEvent;
import sms.*;  // Sudden motion detection

Robot robot;
int[] init_vals;
int[] vals;
int KNOCK;
int wait_for_shake;
boolean wait;
int wait_cnt;

int X,Y,Z; // calculated thresholds

void setup() {
 size(100,100);
 frameRate(20);
 wait = false;
 wait_cnt = 0;
 wait_for_shake = 15; // wait for shaking to stop (number/frames = seconds of waiting)
 KNOCK = 15;          // How big of a jolt before you want to move the screen
 try {
   robot = new Robot();
 }
 catch (Exception e) {
   println("robot Can not be created.");
   exit();
 }

 // Opening values don't start at zero.
 // So we need to collect values at 'rest'
 init_vals = Unimotion.getSMSArray();

 X = init_vals[0];
 Y = init_vals[1];
 Z = init_vals[2];
}


void draw() {
 // Read Sudden Motion Sensor
 int[] vals = Unimotion.getSMSArray();

 // Reset wait count to allow another jolt
 if( wait_cnt == wait_for_shake) {
   wait = false;
   wait_cnt = 0;
 }

 // While we are not waiting for the computer to
 // stop shaking try to make a decision about
 // the motion.
 if(!wait) {
   
   // There's always some shaking going on so we
   // need to check if the shaking was on purpose.
   if( vals[0] > KNOCK+X ) {
     screenLeft();
     wait = true;
   }

   if( vals[0] < -1*KNOCK+X ) {
     screenRight();
     wait = true;
   }

 }
 else {
   wait_cnt++;
 }


}

void screenRight() {
 try {
   robot.keyPress(KeyEvent.VK_META);   // hold meta
   robot.keyPress(KeyEvent.VK_RIGHT ); // then press arrow
   robot.keyRelease(KeyEvent.VK_META);
   robot.keyRelease(KeyEvent.VK_RIGHT);
 }
 catch (Exception e) {
   println("Exception your honor!");
 }
}

void screenLeft() {
 try {
   robot.keyPress(KeyEvent.VK_META);   // hold meta
   robot.keyPress(KeyEvent.VK_LEFT ); // then press arrow
   robot.keyRelease(KeyEvent.VK_META);
   robot.keyRelease(KeyEvent.VK_LEFT);
 }
 catch (Exception e) {
   println("Exception your honor!");
 }
}

void screenUp() {
 try {
   robot.keyPress(KeyEvent.VK_META);   // hold meta
   robot.keyPress(KeyEvent.VK_UP ); // then press arrow
   robot.keyRelease(KeyEvent.VK_META);
   robot.keyRelease(KeyEvent.VK_UP);
 }
 catch (Exception e) {
   println("Exception your honor!");
 }
}


void screenDown() {
 try {
   robot.keyPress(KeyEvent.VK_META);   // hold meta
   robot.keyPress(KeyEvent.VK_DOWN );  // then press arrow
   robot.keyRelease(KeyEvent.VK_META);
   robot.keyRelease(KeyEvent.VK_DOWN);
 }
 catch (Exception e) {
   println("Exception your honor!");
 }
}



Page Index Toggle Pages: 1