Saturday, April 19, 2008

Alphabot

A friend of mine had put a this link up in Google Talk status message:

http://playfreeonlinegames.eu/playonline/typethealphabet.html

and his record of 2.84 seconds. In an exhausted state, after having spent a day running after some compatibility issue in some code, my first shot earned me 6.89 seconds.

Huh?!


Deciding to take matters a wee bit more seriously I decided to ... umm... cheat. I wrote this Java code:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class alphabot {

public static void main(String[] args) {

try {

Robot robot = new Robot();

robot.delay(5000);

robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_B);
robot.keyPress(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_D);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_F);
robot.keyPress(KeyEvent.VK_G);
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_J);
robot.keyPress(KeyEvent.VK_K);
robot.keyPress(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_M);
robot.keyPress(KeyEvent.VK_N);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_P);
robot.keyPress(KeyEvent.VK_Q);
robot.keyPress(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_U);
robot.keyPress(KeyEvent.VK_V);
robot.keyPress(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_Y);
robot.keyPress(KeyEvent.VK_Z);




} catch (AWTException e) {
e.printStackTrace();
}
}
}



This is an awt robot which can be given the control of your mouse and keyboard. This particular one waits for 5 seconds to let you click or focus your mouse someplace, and then "types" in the letters of the English alphabet.

I compiled it and ran it (as such things go), and before my 5 seconds were up, went to the Webpage to click on 'a' and wait. And , lo! 0.03 secs. Not bad at all ...


This is where I should have ideally finished my post and not written this statement and the few that follow it had I not remembered a puzzle I recently read:

Place 50 balls in 9 boxes so that each box contains an odd number of balls.

At first glance this seems impossible since the sum of an odd number of odd integers cannot be even. Its, however, achievable if you place a box inside another. Talk about a puzzle that forces you to think outside the box to only lead you back to inside one.

Yawn!

3 comments :

Anurag Chugh said...

i would rather do this by placing a microcontroller between the keyboard and computer... (or emulate one on the microcontroller using usb if you are using a laptop..)

maybe i can do 0.02 sec? or maybe it will be more..

complex hardware complex software...

so complex that i dont even know if a simple PS/2 hardware interface will be faster (throughput and response time) than javan virtual machine...

Abhishek Ghose said...

i dont see how a mcu helps at all. Its too heavy duty for this for one- and a mcu has other uses (i mean WHY an MCU - its a modified processor - u need to control a keyboard with THAT?). Anyway if u did want ur keyboard to remain a normal keyboard at all (so no mcu's right?), u might want to reset interrupts (rather re-map the kbrd interrupts). but then again even if you do that u wud need to type in (consider ur keyboard alphabetical by now). So this still depends on ur type-speed --- so unless u control the mouse or the page (javascript), i dont see much benefit here either.

Abhishek Ghose said...

umm...MCUs wud also 'solve' the problem at the wrong level of detail [:)]