PDA

View Full Version : How do I detect ONLY 1 mouse press with GLFW?



hashbrown
09-30-2016, 12:10 PM
In JavaScript, you got mousedown and mouseup. I'm sure you all know this, but mousedown is true while you have the (left) mouse button pressed, and mouseup is true once you let go of the (left) mouse button and only once.

How can I detect mouseup with GLFW? I've noticed GLFW_PRESS is like mousedown, but GLFW_RELEASE is what I'm looking for but if I'm polling the current mouse state:



int mouseState = glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT);


...in an infinite loop, I can't just do:



else if (mouseState == GLFW_RELEASE) {

m_mouseUp = true;
}



.. m_mouseUp, will always be true (except when I press the left button). I'm either not thinking this right, or I'm not finding the correct API call (http://www.glfw.org/docs/latest/group__input.html).

GClements
09-30-2016, 04:44 PM
How can I detect mouseup with GLFW?

By registering a mouse button callback with glfwSetMouseButtonCallback(). The callback will be invoked whenever a button press or release event occurs for the given window.





int mouseState = glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT);


If you're polling, you need to keep track of the prior state. E.g.


static int oldState = GL_RELEASE;
int newState = glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT);
if (newState == GL_RELEASE && oldState == GL_PRESS) {
// whatever
}
oldState = newState;

Note that glfwGetMouseButton() returns the state from the most recent mouse button event, so the value won't change between calls to glfwPollEvents() (or glfwWaitEvents(), etc).

hashbrown
10-01-2016, 08:26 PM
By registering a mouse button callback with glfwSetMouseButtonCallback(). The callback will be invoked whenever a button press or release event occurs for the given window.


If you're polling, you need to keep track of the prior state. E.g.


static int oldState = GL_RELEASE;
int newState = glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT);
if (newState == GL_RELEASE && oldState == GL_PRESS) {
// whatever
}
oldState = newState;

Note that glfwGetMouseButton() returns the state from the most recent mouse button event, so the value won't change between calls to glfwPollEvents() (or glfwWaitEvents(), etc).

Perfect! That definitely did the trick, very simple solution too. Thanks!

cali9x
10-10-2016, 04:36 AM
By registering a mouse button callback with glfwSetMouseButtonCallback(). The callback will be invoked whenever a button press or release event occurs for the given window.


If you're polling, you need to keep track of the prior state. E.g.


static int oldState = GL_RELEASE;
int newState = glfwGetMouseButton(win, GLFW_MOUSE_BUTTON_LEFT);
if (newState == GL_RELEASE && oldState == GL_PRESS) {
// whatever
}
oldState = newState;

Note that glfwGetMouseButton() returns the state from the most recent mouse button event, so the value won't change between calls to glfwPollEvents() (or glfwWaitEvents(), etc).

Perfect! That certainly did the trick, the solution is simple too