Speech, Java, Poker, Mac OS X

Last night I got my Mac to understand me when I said basic poker terms like “Fold”, “Call”, and “Raise”. This evening I took the idea a bit further. What if, when I said “Fold”, my Java program would fold my hand for me?

This should be a insanely easy task using AppleScript. Unfortunately none of the poker room clients seem to be scriptable. But if I know where the “Fold” button is, perhaps I could tell my Mac to simulate a mouse click on that spot?

That was the idea. Surprisingly it works. I found a tiny Mac program that simulates mouse clicks, even in non-scriptable applications.

I opened a poker table at Full Tilt and used Pixie to measure the locations of the Fold, Check, Raise, and Call buttons. Then I got my Java program to click at the right spot for each word.

Did it work? You betcha! It was cool saying “Fold” and watching it work.

Here’s the code that does most of the work:


private static void setUpSpeechRecognition() {
SpeechRecognizer speechRecognizer = new SpeechRecognizer();
speechRecognizer.setSpeechRecognizerListener(
new SpeechRecognizerListener() {
public void didRecognizeCommand(String command) {
if (command.equals("Fold")) {
click(627, 704);
} else if (command.equals("Check")) {
click(627, 704);
} else if (command.equals("Raise")) {
click(864, 704);
} else if (command.equals("Call")) {
click(750, 704);
}
}
});
speechRecognizer.setCommands("Fold", "Check", "Raise", "Call");
speechRecognizer.setListensInForegroundOnly(false);
speechRecognizer.setDisplayedCommandsTitle("Poker Copilot");
speechRecognizer.startListening();
}

private static void click(final int x, final int y) {
try {
Robot robot = new Robot();
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);

} catch (AWTException e) {
throw new RuntimeException(e);
}
}

The next task: determining programmatically the location of the command buttons at a poker table.