Posts about... All (17) Android (2) Hooking Up Parts (10) Game Controllers (3) Robots (2)

Map PS3 Controller to Mac via Bluetooth

You can easily connect a PS3 controller to your mac via bluetooth then map the buttons to key presses using Processing language. This post details each step while assuming you have already installed Processing.

Parts

WARNING: Unplug all other USB cables from your computer before continuing. I was able connect my PS3 controller with my USB mouse plugged in, but then Processing code would not work. It only worked without anything else plugged in.

Connect the PS3 Controller to the Mac

NOTE: Depending on where your PS3 is located, you may want to unplug it to prevent it from being turned on when you press the PS button in the following steps.

Ensure the Bluetooth icon in your Mac’s menu bar changed to a greyed-out Bluetooth logo with a few dots (see image below). No lights will be lit up on your PS3 controller.

Processing Code

/*
0 = SELECT
1 = LEFT STICK CLICK
2 = RIGHT STICK CLICK
3 = START
4 = UP
5 = RIGHT
6 = DOWN
7 = LEFT
8 = L2
9 = R2
10 = L1
11 = R1
12 = TRIANGLE
13 = CIRCLE
14 = X
15 = SQUARE
16 = PS BUTTON
*/
import procontroll.*;
import java.io.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

Robot robot;
ControllIO controll;
ControllDevice device;
ControllButton upBtn;
ControllButton rightBtn;
ControllButton downBtn;
ControllButton leftBtn;
ControllButton xBtn;
ControllButton startBtn;
ControllButton squareBtn;

boolean upPressed = false;
boolean rightPressed = false;
boolean downPressed = false;
boolean leftPressed = false;
boolean xPressed = false;
boolean startPressed = false;
boolean squarePressed = false;

void setup(){
size(400,400);

try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}

controll = ControllIO.getInstance(this);

device = controll.getDevice(“PLAYSTATION(R)3 Controller”);
device.setTolerance(0.05f);

upBtn = device.getButton(“4″);
rightBtn = device.getButton(“5″);
downBtn = device.getButton(“6″);
leftBtn = device.getButton(“7″);
xBtn = device.getButton(“14″);
startBtn = device.getButton(“3″);
squareBtn = device.getButton(“15″);
}

float totalX = width/2;
float totalY = height/2;

void draw(){
background(255);

// UP
if(upBtn.pressed()){
if(!upPressed){
robot.keyPress(KeyEvent.VK_W);
upPressed = true;
delay();
}
}
else if(upPressed){
robot.keyRelease(KeyEvent.VK_W);
upPressed = false;
}
// RIGHT
if(rightBtn.pressed()){
if(!rightPressed){
robot.keyPress(KeyEvent.VK_D);
rightPressed = true;
delay();
}
}
else if(rightPressed){
robot.keyRelease(KeyEvent.VK_D);
rightPressed = false;
}
// DOWN
if(downBtn.pressed()){
if(!downPressed){
robot.keyPress(KeyEvent.VK_S);
downPressed = true;
delay();
}
}
else if(downPressed){
robot.keyRelease(KeyEvent.VK_S);
downPressed = false;
}
// LEFT
if(leftBtn.pressed()){
if(!leftPressed){
robot.keyPress(KeyEvent.VK_A);
leftPressed = true;
delay();
}
}
else if(leftPressed){
robot.keyRelease(KeyEvent.VK_A);
leftPressed = false;
}
// JUMP
if(xBtn.pressed()){
if(!xPressed){
robot.keyPress(KeyEvent.VK_SPACE);
xPressed = true;
delay();
}
}
else if(xPressed){
robot.keyRelease(KeyEvent.VK_SPACE);
xPressed = false;
}
// MENU
if(startBtn.pressed()){
if(!startPressed){
robot.keyPress(KeyEvent.VK_ESCAPE);
startPressed = true;
delay();
}
}
else if(startPressed){
robot.keyRelease(KeyEvent.VK_ESCAPE);
startPressed = false;
}
// REWIND
if(squareBtn.pressed()){
if(!squarePressed){
robot.keyPress(KeyEvent.VK_SHIFT);
squarePressed = true;
delay();
}
}
else if(squarePressed){
robot.keyRelease(KeyEvent.VK_SHIFT);
squarePressed = false;
}
}

void delay(){
try{
Thread.sleep(100);
}
catch(InterruptedException ie){

}
}

Run the code. If there are no errors in the output area, open TextEdit (or any Notepad-like applications) and start pressing any buttons on your controller that have been mapped to a key in the code. The comments at the top of the code show what the button #’s relate to on the PS3 Controller. The key mappings in this code are written for “Braid” (an awesome game available in the Mac App Store). Change the key mappings as needed for whatever you’re using it for.