转自: baby66 ( http://baby66.bokee.com/6557451.html )

 

package tools;

/*
 * 按键处理
 * @author baby66 引用超级好人:月の蓝瞳
 *
 *在Canvas里这样用:
 protected void keyPressed(int keycode)
 { 
 KEY.doKeyPressed(keycode);
 }

 protected void keyReleased(int keycode)
 {
 KEY.doKeyReleased(keycode);
 }

 *run()的每个循环的第一句用这个:
 KEY.updateKey();

 *
 *在hideNotify里调用这个:(或者其他需要清除按键记录的时候)
 KEY.clearButton();

 *if (KEY.isPressed(KEY.K_OK))
 -----------判断按一下的时候这样用

 if (KEY.isKeyHolding(KEY.K_LEFT | KEY.K_6))
 ---------判断按住的时候这样用

 *
 *if (isPressedAnyKey())
 ----------判断按下任意键的时候这样用

 if (KEY.isPressedAnyKey())这样

 移植的时候只改KEY类里最上面的键值,无视机型正负,变量里一定要写正的。
 *
 *用的是手机上测出的键值的绝对值

 不要用getGameAction,在很多机型上都有错误

 *KEYCODE_OK ---------左软键
 KEYCODE_BACK -------右软键
 *
 */
final class KEY {
    private static final byte KEYCODE_UP = 1;
    private static final byte KEYCODE_DOWN = 2;
    private static final byte KEYCODE_LEFT = 3;
    private static final byte KEYCODE_RIGHT = 4;
    private static final byte KEYCODE_FIRE = 5;
    private static final byte KEYCODE_OK = 6;
    private static final byte KEYCODE_BACK = 7;

    private static int linker = -1;
    static final int K_UP = 1 << (linker++);
    static final int K_DOWN = 1 << (linker++);
    static final int K_LEFT = 1 << (linker++);
    static final int K_RIGHT = 1 << (linker++);
    static final int K_FIRE = 1 << (linker++);
    static final int K_OK = 1 << (linker++);
    static final int K_BACK = 1 << (linker++);
    static final int K_0 = 1 << (linker++);
    static final int K_1 = 1 << (linker++);
    static final int K_2 = 1 << (linker++);
    static final int K_3 = 1 << (linker++);
    static final int K_4 = 1 << (linker++);
    static final int K_5 = 1 << (linker++);
    static final int K_6 = 1 << (linker++);
    static final int K_7 = 1 << (linker++);
    static final int K_8 = 1 << (linker++);
    static final int K_9 = 1 << (linker++);
    static final int K_STAR = 1 << (linker++);
    static final int K_POUND = 1 << (linker++);

    private static final int getKeyMask(int k) {
        k = k > 0 ? k : -k;

        switch (k) {
        case KEYCODE_UP:
            return K_UP;

        case KEYCODE_DOWN:
            return K_DOWN;

        case KEYCODE_LEFT:
            return K_LEFT;

        case KEYCODE_RIGHT:
            return K_RIGHT;

        case KEYCODE_FIRE:
            return K_FIRE;

        case KEYCODE_OK:
            return K_OK;

        case KEYCODE_BACK:
            return K_BACK;

        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            return K_0 << (k - '0');

        case '*':
            return K_STAR;

        case '#':
            return K_POUND;
        }
        return 0;
    }

    private static int keyCurrent;
    private static int keyPressed, keyReleased;
    private static int keyPressedBuffer, keyReleasedBuffer;

    static final void doKeyPressed(int keycode) {
        keyPressedBuffer |= getKeyMask(keycode);
    }

    static final void doKeyReleased(int keycode) {
        keyReleasedBuffer |= getKeyMask(keycode);
    }

    static void updateKey() {
        keyPressed = keyPressedBuffer;
        keyReleased = keyReleasedBuffer;

        keyCurrent |= keyPressed;
        keyCurrent &= ~keyReleased;

        keyPressedBuffer = 0;
        keyReleasedBuffer = 0;
    }

    static final boolean isPressed(int theKey) {
        return ((keyPressed & theKey) != 0);
    }

    static final boolean isKeyHolding(int theKey) {
        return ((keyCurrent & theKey) != 0);
    }

    static final boolean isReleased(int theKey) {
        return ((keyReleased) != 0);
    }

    static final boolean isPressedAnyKey() {
        return (keyPressed != 0);
    }

    static void clearButton() {
        keyCurrent = 0;
    }

    private KEY() {
    }
}