Spasi
I ran another test using the window focus call back. I get the initial window focus but when I click off (lost focus triggered) and click back I don't get an event saying that Focus has come back, I only get focus lost events (if i click off again another focus lost event is triggered). I guess the focus has to be true for the cursor to be hidden?
Some output from the below
Window[139768908528128] Focus[true]
Window[139768908528128] Key : 256 Pressed : true
Escape pressed, making cursor hidden
Window[139768908528128] Key : 256 Pressed : false
Window[139768908528128] Key : 256 Pressed : true
Escape pressed, making cursor normal
Window[139768908528128] Key : 256 Pressed : false
Window[139768908528128] Focus[false]
Window[139768908528128] Focus[false]
New test code below
public class TestBed
{
private long window = MemoryUtil.NULL;
// any control interface such as touch screen, keyboard, mouse
private final GLFWKeyCallback keyCallback;
private final GLFWCursorPosCallback cursorPositionCallback;
private final GLFWWindowFocusCallback windowFocusCallback;
private boolean running = true;
private boolean isGrabbed = false;
private GLFWErrorCallback errorCallback;
public TestBed()
{
windowFocusCallback = new GLFWWindowFocusCallback()
{
@Override
public void invoke(long window, int focused)
{
System.out.println("Window[" + window + "] Focus[" + (focused == GL11.GL_TRUE) + "]");
}
};
keyCallback = new GLFWKeyCallback()
{
@Override
public void invoke(long window, int key, int scancode, int action, int mods)
{
boolean isPressed = false;
boolean isRepeated = false;
switch (action)
{
case GLFW.GLFW_RELEASE:
isPressed = false;
break;
case GLFW.GLFW_PRESS:
isPressed = true;
break;
case GLFW.GLFW_REPEAT:
isRepeated = true;
break;
}
System.out.println("Window[" + window + "] Key : " + key + " Pressed : " + isPressed);
if (key == GLFW.GLFW_KEY_ESCAPE && isPressed == true)
{
if (isGrabbed)
{
System.out.println("Escape pressed, making cursor normal");
GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_NORMAL);
isGrabbed = false;
}
else
{
System.out.println("Escape pressed, making cursor hidden");
GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_HIDDEN);
isGrabbed = true;
}
}
if (key == GLFW.GLFW_KEY_F1)
{
System.out.println("Exiting");
running = false;
}
}
};
cursorPositionCallback = new GLFWCursorPosCallback()
{
@Override
public void invoke(long window, double xpos, double ypos)
{
//System.out.println("X : " + (int) xpos + " Y : " + (int) ypos);
}
};
}
public void run()
{
GLFW.glfwSetErrorCallback(errorCallback = errorCallbackPrint(System.err));
if (GLFW.glfwInit() != GL11.GL_TRUE)
{
System.out.println("Exiting because could not initialise GLFW");
System.exit(1);
}
GLFW.glfwDefaultWindowHints();
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GL_FALSE);
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GL_TRUE);
// tell the window what bits per pixel we are interested in
GLFW.glfwWindowHint(GLFW.GLFW_RED_BITS, 8);
GLFW.glfwWindowHint(GLFW.GLFW_GREEN_BITS, 8);
GLFW.glfwWindowHint(GLFW.GLFW_BLUE_BITS, 8);
GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3);
GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2);
// create the actual window
window = GLFW.glfwCreateWindow(800, 600, "", MemoryUtil.NULL, MemoryUtil.NULL);
if (window == MemoryUtil.NULL)
{
System.out.println("Exiting because window is null");
System.exit(2);
}
// if windowed then center
ByteBuffer vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
GLFW.glfwSetWindowPos(window, (GLFWvidmode.width(vidmode) - 800) / 2, (GLFWvidmode.height(vidmode) - 600) / 2);
GLFW.glfwMakeContextCurrent(window);
GLContext.createFromCurrent();
GL11.glViewport(0, 0, 800, 600);
GLFW.glfwSwapInterval(1);
GLFW.glfwShowWindow(window);
GLFW.glfwSetKeyCallback(window, keyCallback);
GLFW.glfwSetCursorPosCallback(window, cursorPositionCallback);
GLFW.glfwSetWindowFocusCallback(window, windowFocusCallback);
while (running)
{
GLFW.glfwSwapBuffers(window);
GLFW.glfwPollEvents();
}
}
public static void main(String[] args)
{
TestBed tb = new TestBed();
tb.run();
}
}