multiple button operations instead of one

Started by loginmen, March 04, 2020, 12:30:56

Previous topic - Next topic

loginmen

I wrote a simple opengl program for input test
import org.lwjgl.opengl.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL46.*;
import static org.lwjgl.system.MemoryUtil.*;

public class TestInput {
	private static long window;
	private static KeyCallBack input = new KeyCallBack();
	private static float color = 0.0f;

	public static void main(String[] args) {
		glfwInit();
		glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
		glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
		glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
		glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
		window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
		if (window == NULL)
		{
			glfwTerminate();
		}
		glfwSwapInterval(1);
		glfwSetKeyCallback(window, input);
		glfwMakeContextCurrent(window);
		GL.createCapabilities();
		while(!glfwWindowShouldClose(window))
		{
			if (input.isKeyPressed(GLFW_KEY_W)) {
				if (color < 1) color += 0.01f;
				System.out.println(color);
			}
			glfwPollEvents();
			glClearColor(color, color, color, 1.0f);
		    glClear(GL_COLOR_BUFFER_BIT);
		    glfwSwapBuffers(window);
		}
		glfwTerminate();
	}
}

import org.lwjgl.glfw.GLFWKeyCallbackI;
import static org.lwjgl.glfw.GLFW.*;

public class KeyCallBack implements GLFWKeyCallbackI {
	private int key;
	private int action;
	
	@Override
	public void invoke(long window, int key, int scancode, int action, int mods) {
		this.key = key;
		this.action = action;
	}
	
	public boolean isKeyPressed(int keyCode) {
		return (key == keyCode) && (action == GLFW_PRESS);
	}
}

I need the buttons to work only once, but the button fires about 4 times if just press and about 30 times if pressed. How fix it? Each output of the number for me is a button fires. I use latest stable build of lwjgl. Using flags is not a good option as there are many buttons