I'm working on some rotation/movement code where you press an arrow key, the clip rotates to the desired angle, and then moves in the desired direction once it has reached said angle.

I'm having an issue forcing the clip to rotate in the proper direction towards the desired angle.

http://www.nolentabner.com/test/keytest.swf

If you follow the link above, press and hold the arrow keys to move and rotate the clip around the stage. Try rotating the clip all the way until it's facing down then press and hold the LEFT arrow key. Instead of rotating to the left, the clip rotates all the way around (counter-clockwise) until it is facing left.

Code:
var speed:int = 3;
var dir:String = "right";
var targetDir:String = " ";

var keys:Object = new Object();

keys[Keyboard.UP] = {down:false, dirx:0, diry:-1, rot:-90, dir:"up"};
keys[Keyboard.DOWN] = {down:false, dirx:0, diry:1, rot:90, dir:"down"};
keys[Keyboard.LEFT] = {down:false, dirx:-1, diry:0, rot:-180, dir:"left"};
keys[Keyboard.RIGHT] = {down:false, dirx:1, diry:0, rot:0, dir:"right"};


stage.addEventListener(KeyboardEvent.KEY_DOWN, downKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, upKeys);
stage.addEventListener(Event.ENTER_FRAME, onFrame);

function downKeys(e:KeyboardEvent):void
{
    if(keys[e.keyCode] != undefined)
    {
        keys[e.keyCode].down = true;
        targetDir = keys[e.keyCode].dir;

    }
}

function upKeys(e:KeyboardEvent):void
{
    if(keys[e.keyCode] != undefined)
    {
        keys[e.keyCode].down = false;
    }
}

function onFrame(e:Event):void
{
   
    //trace(dir+" "+box.rotation+" "+targetDir);

    for each(var keyOb:Object in keys)
    {
        if(keyOb.down == true)
        {
            // if we are already at our target direction, move ahead

            if(box.rotation == keyOb.rot)
            {
                dir = keyOb.dir;

                box.x += speed*keyOb.dirx;
                box.y += speed*keyOb.diry;
            }
            else
            {
                // if our desired angle is less than our current angle, decrease
                if(keyOb.rot < box.rotation)
                {
					box.rotation -= speed;

                }
                // if our desired angle is greater than our current angle, increase
                else
                {
					box.rotation += speed;
                }
            }
        }
    }
}
I understand that this is happening because technically the angle for the LEFT direction is less than the current angle when facing down (-180 and 90, respectively). What I'm curious about is how to accomplish proper rotation without having specific handlers for each direction. Wouldn't I need to use something like the distance formula and check it against 180, then use that to determine which direction to rotate?

Sorry if this is lengthy, I just want to make sure it's understandable. Check the .swf and you will see what I'm trying to accomplish.