2017-07-16 193 views
1

使用GLFW相當新穎,我希望在點擊鼠標左鍵時將光標座標輸出到控制檯上。但是,我沒有得到任何輸出。OpenGL在C++中鼠標點擊時獲得光標座標

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; 
    } 
} 
+1

爲什麼在** key **事件的回調中這樣做?如果您想在點擊鼠標按鈕時執行某些操作,則應該在鼠標按鈕回調中檢查該操作。 –

回答

2

您正試圖在鍵盤輸入回調時獲取鼠標輸入。請注意,key對應的值爲GLFW_KEY_*。您應該設置鼠標輸入回調,而不是:

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; 
    } 
}