LWJGL 3 Получение состояния блокировки шапки - PullRequest
0 голосов
/ 19 сентября 2018

Пытался воссоздать keyType (KeyEvent e) из java.awt.event.KeyListener в LWJGL с помощью функции вызова GLFWKeyCallback, но когда я пытаюсь получить состояние caps, он вернет только false.Код для получения состояния:

Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);

Теперь вопрос, как мне это реализовать?Также пытался установить логический caps_lock каждый раз в методе обновления с кодом, приведенным выше, та же проблема возвращала false каждый раз.

Весь класс ввода:

public class Input implements
{
private static final char INVALID_CHAR = (char) -1;

// window
private final Window window;
// keytype callback
private final List<KeyTypeCallBack> keytype_callbacks;
// keyboard key
private boolean caps_lock;
private final boolean[] keys;
private final boolean[] last_keys;
// mouse button
private final boolean[] buttons;
private final boolean[] last_buttons;
// mouse pos
private double mousex;
private double mousey;
// mouse scroll
private double scrollx;
private double scrolly;

public Input(Window window)
{
    // window
    this.window = window;
    // keytype callback
    keytype_callbacks = new ArrayList<>();
    // keyboard key
    this.caps_lock = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
    this.keys = new boolean[GLFW.GLFW_KEY_LAST];
    this.last_keys = new boolean[GLFW.GLFW_KEY_LAST];
    // mouse button
    this.buttons = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST];
    this.last_buttons = new boolean[GLFW.GLFW_MOUSE_BUTTON_LAST];
    // mouse pos
    this.mousex = 0;
    this.mousey = 0;
    // mouse scroll
    this.scrollx = 0;
    this.scrolly = 0;
}

protected void createCallBacks()
{
    GLFW.glfwSetKeyCallback(window.getWindow(), new KeyboardKey(this));
    GLFW.glfwSetMouseButtonCallback(window.getWindow(), new MouseButton(this));
    GLFW.glfwSetCursorPosCallback(window.getWindow(), new MousePos(this));
    GLFW.glfwSetScrollCallback(window.getWindow(), new MouseScroll(this));
}

protected void update()
{
    for(int key = 0; key < GLFW.GLFW_KEY_LAST; key++)
    {
        last_keys[key] = keys[key];
    }

    for(int button = 0; button < GLFW.GLFW_MOUSE_BUTTON_LAST; button++)
    {
        last_buttons[button] = buttons[button];
    }
}

public boolean isKeyDown(int key)
{
    return (key > 0 && key <= GLFW.GLFW_KEY_LAST) && keys[key];
}

public boolean isButtonDown(int button)
{
    return (button > 0 && button <= GLFW.GLFW_MOUSE_BUTTON_LAST) && buttons[button];
}

public boolean isKeyUp(int key)
{
    return (key > 0 && key <= GLFW.GLFW_KEY_LAST) && !keys[key];
}

public boolean isButtonUp(int button)
{
    return (button > 0 && button <= GLFW.GLFW_MOUSE_BUTTON_LAST) && !buttons[button];
}

public boolean isKeyPressed(int key)
{
    return (key > 0 && key <= GLFW.GLFW_KEY_LAST) && keys[key] && !last_keys[key];
}

public boolean isButtonPressed(int button)
{
    return (button > 0 && button <= GLFW.GLFW_MOUSE_BUTTON_LAST) && buttons[button] && !last_buttons[button];
}

public boolean isKeyReleased(int key)
{
    return (key > 0 && key <= GLFW.GLFW_KEY_LAST) && !keys[key] && last_keys[key];
}

public boolean isButtonReleased(int button)
{
    return (button > 0 && button <= GLFW.GLFW_MOUSE_BUTTON_LAST) && !buttons[button] && last_buttons[button];
}

public double getMouseX()
{
    return mousex;
}

public double getMouseY()
{
    return mousey;
}

public double getScrollX()
{
    return scrollx;
}

public double getScrollY()
{
    return scrolly;
}

public boolean isCapsLock()
{
    return caps_lock;
}

public void addKeyTypeCallback(KeyTypeCallBack callback)
{
    keytype_callbacks.add(callback);
}

public void removeKeyTypeCallback(KeyTypeCallBack callback)
{
    keytype_callbacks.remove(callback);
}

// TYPE EXTENDER
public interface KeyTypeCallBack
{
    void keyTyped(char key);
}

// CLASSES
private class KeyboardKey extends GLFWKeyCallback
{
    private final Input input;

    private KeyboardKey(Input input)
    {
        this.input = input;
    }


    @Override
    public void invoke(long window, int key, int scancode, int action, int mods)
    {
        input.keys[key] = (action != GLFW.GLFW_RELEASE);

        if(key == GLFW.GLFW_KEY_CAPS_LOCK)
        {
            input.caps_lock = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
        }

        // test for keytype callback
        if(!input.keytype_callbacks.isEmpty() && (action == GLFW.GLFW_PRESS || action == GLFW.GLFW_REPEAT))
        {
            // convert int to char
            char c = GetCharFromInt(input, key);
            // check if typed char is correct
            if(c != Input.INVALID_CHAR)
            {
                for(KeyTypeCallBack callback : input.keytype_callbacks)
                {
                    callback.keyTyped(c);
                }
            }
        }
    }

    private final char GetCharFromInt(Input input, int key)
    {
        boolean upper = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK) || input.isKeyDown(GLFW.GLFW_KEY_LEFT_SHIFT) || input.isKeyDown(GLFW.GLFW_KEY_RIGHT_SHIFT);
        switch(key)
        {
            case GLFW.GLFW_KEY_SPACE:
                return ' ';
            case GLFW.GLFW_KEY_A:
                return upper ? 'A' : 'a';
            case GLFW.GLFW_KEY_B:
                return upper ? 'B' : 'b';
            case GLFW.GLFW_KEY_C:
                return upper ? 'C' : 'c';
            case GLFW.GLFW_KEY_D:
                return upper ? 'D' : 'd';
            case GLFW.GLFW_KEY_E:
                return upper ? 'E' : 'e';
            case GLFW.GLFW_KEY_F:
                return upper ? 'F' : 'f';
            case GLFW.GLFW_KEY_G:
                return upper ? 'G' : 'g';
            case GLFW.GLFW_KEY_H:
                return upper ? 'H' : 'h';
            case GLFW.GLFW_KEY_I:
                return upper ? 'I' : 'i';
            case GLFW.GLFW_KEY_J:
                return upper ? 'J' : 'j';
            case GLFW.GLFW_KEY_K:
                return upper ? 'K' : 'k';
            case GLFW.GLFW_KEY_L:
                return upper ? 'L' : 'l';
            case GLFW.GLFW_KEY_M:
                return upper ? 'M' : 'm';
            case GLFW.GLFW_KEY_N:
                return upper ? 'N' : 'n';
            case GLFW.GLFW_KEY_O:
                return upper ? 'O' : 'o';
            case GLFW.GLFW_KEY_P:
                return upper ? 'P' : 'p';
            case GLFW.GLFW_KEY_Q:
                return upper ? 'Q' : 'q';
            case GLFW.GLFW_KEY_R:
                return upper ? 'R' : 'r';
            case GLFW.GLFW_KEY_S:
                return upper ? 'S' : 's';
            case GLFW.GLFW_KEY_T:
                return upper ? 'T' : 't';
            case GLFW.GLFW_KEY_U:
                return upper ? 'U' : 'u';
            case GLFW.GLFW_KEY_V:
                return upper ? 'V' : 'v';
            case GLFW.GLFW_KEY_W:
                return upper ? 'W' : 'w';
            case GLFW.GLFW_KEY_X:
                return upper ? 'X' : 'x';
            case GLFW.GLFW_KEY_Y:
                return upper ? 'Y' : 'y';
            case GLFW.GLFW_KEY_Z:
                return upper ? 'Z' : 'z';
        }

        return (char) - 1;
    }
}

private class MouseButton extends GLFWMouseButtonCallback
{
    private final Input input;

    private MouseButton(Input input)
    {
        this.input = input;
    }

    @Override
    public void invoke(long window, int button, int action, int mods)
    {
        input.buttons[button] = (action == GLFW.GLFW_PRESS) ? true : (action == GLFW.GLFW_RELEASE) ? false : true;
    }
}

private class MousePos extends GLFWCursorPosCallback
{
    private final Input input;

    private MousePos(Input input)
    {
        this.input = input;
    }


    @Override
    public void invoke(long window, double xpos, double ypos)
    {
        input.mousex = xpos;
        input.mousey = ypos;
    }
}

private class MouseScroll extends GLFWScrollCallback
{
    private final Input input;

    private MouseScroll(Input input)
    {
        this.input = input;
    }


    @Override
    public void invoke(long window, double xoffset, double yoffset)
    {
        input.scrollx = xoffset;
        input.scrolly = yoffset;
    }
}

}

...