[SOLVED] NiftyGUI Registering Space-bar Event

In my current project I’m using the space-bar to zoom the camera to a selected node. While this works I’m noticing that NiftyGUI is also registering the space-bar being pressed and using it to select the previously clicked button so when I click the space-bar it zooms to the selected node as expected, but also clicks whatever button in the GUI was clicked last.

Is there a way to prevent Nifty from using the space-bar in this fashion?

Nevermind I figured it out. All of my window buttons are routed through the same global method and I just inserted the following line near the end of it:

UI.getWinLayer().getFocusHandler().resetFocusElements();

The UI.getWinLayer() method returns the Nifty Layer that holds all of my windows, except popups.

In case anyone is interested:

 public void windowButtonClick(String data) {
        if (data != null) {
            String d[] = data.split(UI.SPLITTER);
            if (d.length > 1) {
                Object w = WindowManager.getWindow(d[0]);
                if (w != null) {
                    Method m = UI.findMethod(w.getClass(), d[1]);
                    if (d.length > 2) {
                        Object[] args = new Object[d.length - 2];
                        for (int i = 2; i < d.length; i++) {
                            args[i - 2] = d[i];
                        }
                        UI.invokeMethodArg(w, m, args);
                    } else {
                        if (m != null) { UI.invokeMethod(w, m); }
                    }

                    UISound.playButtonClick();
                    UI.getWinLayer().getFocusHandler().resetFocusElements();
                }
            }
        }
    }

and:

public static Method findMethod(final Class < ? > c, final String methodName) {
        String mName = parseMethodName(methodName);
        if (mName != null) {
            for (Method m : c.getMethods()) {
                if (mName.equalsIgnoreCase(m.getName())) {
                    return m;
                }
            }
        } else { return null; }
        
        if (c.getSuperclass() != null) {
            return findMethod(c.getSuperclass(), methodName);
        } else { return null; }
    }
    
    public static String parseMethodName(String methodName) {
        String name = null;
        if (!methodName.contains("(")) {
            if (methodName.length() > 0) {
                name = methodName;
            }
        } else {
            if (methodName.length() > 1) { name = methodName.substring(0, methodName.indexOf("(")); }
        }
        
        return name;
    }

Oh yeah:

public static void invokeMethod(Object p, Method m) {
        try {
            m.invoke(p, (Object[]) null);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            StackTraceElement elements[] = e.getStackTrace();
            if (elements != null) {
                for (StackTraceElement se : elements) {
                    System.out.println( se.getClassName() + ":"
                            + se.getFileName() + ":"
                            + se.getMethodName() + ":"
                            + se.getLineNumber());
                }
            }
        }
    }
    
    public static void invokeMethodArg(Object p, Method m, Object[] args) {
        try {
            m.invoke(p, args);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            StackTraceElement elements[] = e.getStackTrace();
            if (elements != null) {
                for (StackTraceElement se : elements) {
                    System.out.println( se.getClassName() + ":"
                            + se.getFileName() + ":"
                            + se.getMethodName() + ":"
                            + se.getLineNumber());
                }
            }
        }
    }
1 Like