为什么我的方法不适用于字母ENUMS?

时间:2012-08-15 10:22:25

标签: java keyboard

我正在尝试为java游戏编写动态keybind系统。它主要起作用,除了一个大问题:像abc这样的字符不起作用。只有~&$等字符可以使用。

我用KeyFinder lookup = KeyFinder.lookup(keyChar);

调用方法

这是我当前的代码文件,对不起,如果这真的很长。

KeyFinder.java

public enum KeyFinder {
    A(Keyboard.KEY_A, "a", "A"),
    B(Keyboard.KEY_B, "b", "B"),
    C(Keyboard.KEY_C, "c", "C"),
    D(Keyboard.KEY_D, "d", "D"),
    E(Keyboard.KEY_E, "e", "E"),
    F(Keyboard.KEY_F, "f", "F"),
    G(Keyboard.KEY_G, "g", "G"),
    H(Keyboard.KEY_H, "h", "H"),
    I(Keyboard.KEY_I, "i", "I"),
    J(Keyboard.KEY_J, "j", "J"),
    K(Keyboard.KEY_K, "k", "K"),
    L(Keyboard.KEY_L, "l", "L"),
    M(Keyboard.KEY_M, "m", "M"),
    N(Keyboard.KEY_N, "n", "N"),
    O(Keyboard.KEY_O, "o", "O"),
    P(Keyboard.KEY_P, "p", "P"),
    Q(Keyboard.KEY_Q, "q", "Q"),
    R(Keyboard.KEY_R, "r", "R"),
    S(Keyboard.KEY_S, "s", "S"),
    T(Keyboard.KEY_T, "t", "T"),
    U(Keyboard.KEY_U, "u", "U"),
    V(Keyboard.KEY_V, "v", "V"),
    W(Keyboard.KEY_W, "w", "W"),
    X(Keyboard.KEY_X, "x", "X"),
    Y(Keyboard.KEY_Y, "y", "Y"),
    Z(Keyboard.KEY_Z, "z", "Z"),
    TILDE(Keyboard.KEY_GRAVE, "~", "`"),
    TAB(Keyboard.KEY_TAB, "tab"),
    F1(Keyboard.KEY_F1, "F1"),
    F2(Keyboard.KEY_F2, "F2"),
    F3(Keyboard.KEY_F3, "F3"),
    F4(Keyboard.KEY_F4, "F4"),
    F5(Keyboard.KEY_F5, "F5"),
    F6(Keyboard.KEY_F6, "F6"),
    F7(Keyboard.KEY_F7, "F7"),
    F8(Keyboard.KEY_F8, "F8"),
    F9(Keyboard.KEY_F9, "F9"),
    F10(Keyboard.KEY_F10, "F10"),
    F11(Keyboard.KEY_F11, "F11"),
    F12(Keyboard.KEY_F12, "F12"),
    F13(Keyboard.KEY_F13, "F13"),
    F14(Keyboard.KEY_F14, "F14"),
    F15(Keyboard.KEY_F15, "F15"),
    ONE(Keyboard.KEY_1, "1", "!", "one"),
    TWO(Keyboard.KEY_2, "2", "@", "two"),
    THREE(Keyboard.KEY_3, "3", "#", "three"),
    FOUR(Keyboard.KEY_4, "4", "$", "four"),
    FIVE(Keyboard.KEY_5, "5", "%", "five"),
    SIX(Keyboard.KEY_6, "6", "^", "six"),
    SEVEN(Keyboard.KEY_7, "7", "&", "seven"),
    EIGHT(Keyboard.KEY_8, "8", "*", "eight"),
    NINE(Keyboard.KEY_9, "9", "(", "nine"),
    ZERO(Keyboard.KEY_0, "0", ")", "zero"),
    MINUS(Keyboard.KEY_MINUS, "-", "_", "minus"),
    EQUALS_ADD(Keyboard.KEY_EQUALS, "=", "+", "plus", "equals"),
    INSERT(Keyboard.KEY_INSERT, "insert", "ins"),
    DEL(Keyboard.KEY_DELETE, "del", "delete"),
    HOME(Keyboard.KEY_HOME, "home"),
    ADD(Keyboard.KEY_ADD, "add"),
    PAGE_UP(Keyboard.KEY_PRIOR, "prior", "pageup"),
    PAGE_DOWN(Keyboard.KEY_NEXT, "next", "pagedown"),
    NUM_LOCK(Keyboard.KEY_NUMLOCK, "numlock", "numberlock"),
    SQBRACKET_LEFT(Keyboard.KEY_LBRACKET, "[", "{"),
    SQBRACKET_RIGHT(Keyboard.KEY_RBRACKET, "]", "}"),
    SEMICOLON(Keyboard.KEY_SEMICOLON, ";", ":");
    private static final Map<Integer, KeyFinder> keys = new HashMap<Integer, KeyFinder>();
    private static final Map<String, KeyFinder> lookup = new LinkedHashMap<String, KeyFinder>();
    private final int key;
    private final String keyName;
    private final String[] lookupKeys;
    static {
        for (KeyFinder type : EnumSet.allOf(KeyFinder.class)) {
            keys.put(type.key, type);
            for (String key : type.lookupKeys) {
                lookup.put(key, type);
            }
        }
    }

    KeyFinder(int key, String keyName, String lookupKey) {
        this.key = key;
        this.keyName = keyName;
        this.lookupKeys = new String[] { lookupKey };
    }

    KeyFinder(int key, String keyName, String... lookupKeys) {
        this.key = key;
        this.keyName = keyName;
        this.lookupKeys = lookupKeys;
    }

    public static KeyFinder fromKey(int key) {
        return keys.get(key);
    }

    public static String toKeyName(int key) {
        KeyFinder type = keys.get(key);
        if (type != null) {
            return type.getKeyName();
        } else {
            return "#" + key;
        }
    }

    public static KeyFinder lookup(String keyName) {
        return lookup(keyName, true);
    }

    public static KeyFinder lookup(String keyName, boolean fuzzy) {
        KeyFinder i = StringUtil.lookup(lookup, keyName, fuzzy);
        if(i != null)
            return i;
        else
            try {
                return fromKey(Integer.parseInt(keyName));
            } catch (NumberFormatException e) {
                return null;
            }
    }

    public int getKey() {
        return key;
    }

    public String getKeyName() {
        return keyName;
    }
}

3 个答案:

答案 0 :(得分:3)

我剥离了您的KeyFinder,并与StringUtil一起进行了测试,并意识到您从未在keyName地图中使用lookup。所以a不存在。

以下是我所做的一些可能对您有帮助的更改。

lookupKeys更改为列表而不是数组。

private final ArrayList<String> lookupKeys = new ArrayList<String>();

删除你的一个构造函数(它已经过时,因为你有一个varargs

KeyFinder(int key, String keyName, String lookupKey) // Remove

更改其他构造函数,并将keyName包含在lookupKeys中。

KeyFinder(int key, String keyName, String... lookupKeys) {
    this.key = key;
    this.keyName = keyName;
    this.lookupKeys.add(keyName);
    this.lookupKeys.addAll(Arrays.asList(lookupKeys));
}

在一些简单的测试中,它对我有用。

<强> NB

我没有比这更分析你的代码。我不知道你是否真的需要keyName,但我把它留给你。我只是希望查找起作用。

答案 1 :(得分:2)

您的问题是您无法减少问题,因为您的代码过于复杂(=它有太多的依赖关系)。

尝试将代码分成独立的位和片段,并为每个代码编写单元测试。如果单元测试工作,那么您将知道任何错误只能隐藏在连接不同部分的代码中。

那就是说,我的猜测是有些方法会返回null而你编写的代码在这种情况下无声地失败(即不是给出错误消息“这不应该在这里返回null”,你只是给出向上)。像这样的代码是非常不友好的,因为任何不寻常的条件都会使它无声地失败而没有任何线索试图了解正在发生的事情(在这种情况下你)。

答案 2 :(得分:2)

你的代码无法被删除是错的 - 如果你这样做了,你自己就会发现错误。

Here's an ideone snippet说明了失败查找的核心。请注意,您的第一个关键字符未显示在查找地图中 - 因为它是keyName而不是lookupKey

Here's a modified snippet其中名称也显示为查找键。不出所料,查找成功了。

事实上,拥有keyName字段似乎毫无意义,因为它从未使用过(并且toString()更适合获取枚举常量的“名称”)。所以this variant可能是更好的。


你的问题 nothing 与字母和符号有关;只是第一个引用的符号不起作用(因此对大写字母的查找会起作用)。同样,它不需要99%来理解代码如何工作,这只是一个错误地填充静态地图的问题。

请尽量减少盲目性,并在以后更积极主动。