Fairly new to using GLFW, I want to output the cursor coordinate onto the console whenever the left mouse button in clicked. However, I am not getting any output at all.
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
//ESC to quit
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, GL_TRUE);
return;
}
if (key == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
double xpos, ypos;
//getting cursor position
glfwGetCursorPos(window, &xpos, &ypos);
cout << "Cursor Position at (" << xpos << " : " << ypos << endl;
}
}
You are trying to get mouse input at keyboard input callback. Notice that key
corresponds to GLFW_KEY_*
values. You should set mouse input callback instead:
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
double xpos, ypos;
//getting cursor position
glfwGetCursorPos(window, &xpos, &ypos);
cout << "Cursor Position at (" << xpos << " : " << ypos << endl;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With