⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyhandler.java

📁 Sony Ericsson手机上的Facebook客户端全套代码
💻 JAVA
字号:
// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   KeyHandler.java

package se.southend.drops.screen;

import javax.microedition.lcdui.Canvas;

// Referenced classes of package se.southend.drops.screen:
//            Device

public class KeyHandler
{

    public KeyHandler()
    {
        allowDiagonal = true;
    }

    public KeyHandler(Canvas canvas)
    {
        allowDiagonal = true;
        this.canvas = canvas;
    }

    public static final KeyHandler getInstance()
    {
        if(instance == null)
            instance = new KeyHandler();
        return instance;
    }

    public final void useDirectionalKeys(boolean flag)
    {
        useDirectionalKeys = flag;
    }

    public final void setAllowDiagonal(boolean flag)
    {
        allowDiagonal = flag;
    }

    public final void updateKeys()
    {
        keysHeld &= ~keysReleased;
        lastPressed &= ~keysReleased;
        keysPressed = keysReleased = keysBlocked = 0;
        repeats = repeats(repeatDelay);
    }

    public final boolean wasPressed(int key)
    {
        return ((keysPressed & key) != 0 || (key & lastPressed) != 0 && repeats) && (keysBlocked & key) == 0;
    }

    public final boolean isHeld(int key)
    {
        return (keysHeld & ~keysBlocked & key) != 0;
    }

    public final boolean noneHeld()
    {
        return getHeldKeys() == 0 && getLastPressed() == 0;
    }

    public final void pressKey(int key)
    {
        keysPressed |= key;
        keysHeld |= key;
    }

    public final void releaseKey(int key)
    {
        keysReleased |= key;
    }

    public final void releaseAllKeys()
    {
        keysReleased = -1;
    }

    public final void blockKey(int key)
    {
        keysBlocked |= key;
    }

    public final void unblockKey(int key)
    {
        keysBlocked &= ~key;
    }

    public final void blockAllKeys()
    {
        keysBlocked = -1;
    }

    public final void unblockAllKeys()
    {
        keysBlocked = 0;
    }

    public final int getHeldKeys()
    {
        return keysHeld & ~keysBlocked;
    }

    public final int getPressedKeys()
    {
        return keysPressed & ~keysBlocked;
    }

    public final int getLastPressed()
    {
        return lastPressed & ~keysBlocked;
    }

    public void setCanvas(Canvas canvas)
    {
        this.canvas = canvas;
    }

    public final void setRepetitionDelays(int firstDelay, int repeatDelay)
    {
        this.firstDelay = firstDelay;
        this.repeatDelay = repeatDelay;
    }

    public void keyPressed(int keyCode, boolean convertKeyCode)
    {
        int key;
        if(convertKeyCode)
        {
            key = getKey(keyCode);
            int action = getAction(keyCode, canvas);
            if(key == 0 || useDirectionalKeys && action != 0)
                key = action;
        } else
        {
            key = keyCode;
        }
        if(!allowDiagonal)
        {
            if(isHeld(12) && (key == 1 || key == 2))
                return;
            if(isHeld(3) && (key == 4 || key == 8))
                return;
        }
        lastPressed = key;
        keysPressed |= key;
        keysHeld |= key;
        resetRepeat(firstDelay);
        repeats = false;
    }

    public void keyPressed(int keyCode)
    {
        keyPressed(keyCode, true);
    }

    public void keyReleased(int keyCode, boolean convertKeyCode)
    {
        int key;
        if(convertKeyCode)
        {
            key = getKey(keyCode);
            int action = getAction(keyCode, canvas);
            if(key == 0 || useDirectionalKeys && action != 0)
                key = action;
        } else
        {
            key = keyCode;
        }
        keysReleased |= key;
    }

    public void keyReleased(int keyCode)
    {
        keyReleased(keyCode, true);
    }

    private static int getKey(int keyCode)
    {
        switch(keyCode)
        {
        case 48: // '0'
            return 512;

        case 49: // '1'
            return 1024;

        case 50: // '2'
            return 2048;

        case 51: // '3'
            return 4096;

        case 52: // '4'
            return 8192;

        case 53: // '5'
            return 16384;

        case 54: // '6'
            return 32768;

        case 55: // '7'
            return 0x10000;

        case 56: // '8'
            return 0x20000;

        case 57: // '9'
            return 0x40000;

        case 35: // '#'
            return 0x80000;

        case 42: // '*'
            return 0x100000;
        }
        if(keyCode == KEYCODE_KEY_LEFT_SOFT)
            return 0x200000;
        if(keyCode == KEYCODE_KEY_MIDDLE_SOFT)
            return 0x400000;
        if(keyCode == KEYCODE_KEY_RIGHT_SOFT)
            return 0x800000;
        if(keyCode == KEYCODE_KEY_C)
            return 0x1000000;
        return keyCode != KEYCODE_KEY_BACK ? 0 : 0x2000000;
    }

    private static int getAction(int keyCode, Canvas canvas)
    {
        if(canvas != null)
            switch(canvas.getGameAction(keyCode))
            {
            case 6: // '\006'
                return 8;

            case 2: // '\002'
                return 1;

            case 5: // '\005'
                return 2;

            case 1: // '\001'
                return 4;

            case 8: // '\b'
                return 16;

            case 9: // '\t'
                return 32;

            case 10: // '\n'
                return 64;

            case 11: // '\013'
                return 128;

            case 12: // '\f'
                return 256;
            }
        return 0;
    }

    public static boolean repeats(int repeatDelay)
    {
        if(repeatDelay == 0)
            return false;
        long currentTime = Device.getTime();
        if(currentTime >= nextRepeatTime)
        {
            nextRepeatTime = currentTime + (long)repeatDelay;
            return true;
        } else
        {
            return false;
        }
    }

    public static void resetRepeat(int firstDelay)
    {
        if(firstDelay == 0)
        {
            return;
        } else
        {
            nextRepeatTime = Device.getTime() + (long)firstDelay;
            return;
        }
    }

    private static KeyHandler instance;
    private int keysHeld;
    private int keysPressed;
    private int keysReleased;
    private int keysBlocked;
    private int lastPressed;
    private boolean useDirectionalKeys;
    private boolean allowDiagonal;
    private boolean repeats;
    private Canvas canvas;
    public static int KEYCODE_KEY_LEFT_SOFT = -6;
    public static int KEYCODE_KEY_MIDDLE_SOFT = -20;
    public static int KEYCODE_KEY_RIGHT_SOFT = -7;
    public static int KEYCODE_KEY_C = -8;
    public static int KEYCODE_KEY_BACK = -11;
    public static final int KEY_NONE = 0;
    public static final int LEFT = 1;
    public static final int RIGHT = 2;
    public static final int UP = 4;
    public static final int DOWN = 8;
    public static final int FIRE = 16;
    public static final int GAME_A = 32;
    public static final int GAME_B = 64;
    public static final int GAME_C = 128;
    public static final int GAME_D = 256;
    public static final int KEY_NUM0 = 512;
    public static final int KEY_NUM1 = 1024;
    public static final int KEY_NUM2 = 2048;
    public static final int KEY_NUM3 = 4096;
    public static final int KEY_NUM4 = 8192;
    public static final int KEY_NUM5 = 16384;
    public static final int KEY_NUM6 = 32768;
    public static final int KEY_NUM7 = 0x10000;
    public static final int KEY_NUM8 = 0x20000;
    public static final int KEY_NUM9 = 0x40000;
    public static final int KEY_POUND = 0x80000;
    public static final int KEY_STAR = 0x100000;
    public static final int KEY_LEFT_SOFT = 0x200000;
    public static final int KEY_MIDDLE_SOFT = 0x400000;
    public static final int KEY_RIGHT_SOFT = 0x800000;
    public static final int KEY_C = 0x1000000;
    public static final int KEY_BACK = 0x2000000;
    private int firstDelay;
    private int repeatDelay;
    private static long nextRepeatTime;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -