跳轉到內容

遊戲開發/渲染與遊戲引擎/OpenGL/GLUT/處理輸入指南

75% developed
來自 Wikibooks,開放世界中的開放書籍

鍵盤按下

[編輯 | 編輯原始碼]

此函式處理按鍵按下時呼叫的函式。

void InitScene(){
    //Other functions will be here
    glutKeyboardFunc(KeyboardDown); //Telling glut what function to call when the event occurs
}


void KeyboardDown(unsigned char key, int x, int y){ 
                  //key - The ascii value of the key that's pressed
                  //x - The x coordinate of the mouse when it was pressed
                  //y - The y coordinate of the mouse when it was pressed

    //Handle what will happen when a key is pressed down
}

鍵盤抬起

[編輯 | 編輯原始碼]

此函式處理按鍵釋放時呼叫的函式。

void InitScene(){
    //Other functions will be here
    glutKeyboardUpFunc(KeyboardUp); //Telling glut what function to call when the event occurs
}


void KeyboardUp(unsigned char key, int x, int y){ 
                  //key - The ascii value of the key that's pressed
                  //x - The x coordinate of the mouse when it was pressed
                  //y - The y coordinate of the mouse when it was pressed

    //Handle what will happen when a key is released
}

一些不可見字元

[編輯 | 編輯原始碼]

鍵盤按下鍵盤抬起 可以用來獲取一些'不可見'字元,例如退格鍵和刪除鍵,請檢視 ascii 表,值 0-31 和 127。對於那些沒有出現在那裡的字元,請檢視 特殊鍵盤輸入

示例用法

if (key == 8){} //Backspace has been pressed
if (key == 9){} //TAB has been pressed
if (key == 10){} //Enter has been pressed
if (key == 27){} //Escape has been pressed
if (key == 127){} //Delete has been pressed

特殊鍵盤輸入

[編輯 | 編輯原始碼]

它用於那些沒有與之關聯的 ascii 字元的鍵盤輸入(例如,向上箭頭、F1)。

對於每個鍵組合,你應該檢視 此參考,以瞭解你需要使用的鍵的名稱。

你還可以檢視 參考列表,以瞭解你可以使用的修飾符。

void InitScene(){
    //Other functions will be here
    glutSpecialFunc(KeyboardSpecial); //Telling glut what function to call when the event occurs
}


void KeyboardSpecial(int key, int x, int y){
                  //key - The id code of the keypress
                  //x - The x coordinate of the mouse when it was pressed
                  //y - The y coordinate of the mouse when it was pressed

    //Handle what happens when a key is pressed

    //Example code:
    int modifiers = glutGetModifiers(); //Get if ctrl, alt or shift are pressed can use:if if (modifiers == (GLUT_ACTIVE_CTRL | GLUT_ACTIVE_ALT | GLUT_ACTIVE_SHIFT))
    switch(key){
    case GLUT_KEY_F2 :
        cout << "F2 was pressed." << endl;
        break;
    case GLUT_KEY_UP :
        cout << "The up arrow was pressed." << endl;
        break;
    case GLUT_KEY_INSERT : 
	if (modifiers == (GLUT_ACTIVE_CTRL | GLUT_ACTIVE_ALT)){
	    cout << "Pressed Insert " << "with only Ctrl and Alt" << endl;
	}else if (modifiers & GLUT_ACTIVE_CTRL && modifiers & GLUT_ACTIVE_ALT){
	    cout << "Pressed Insert " << "with Ctrl and Alt" << endl;
        }
	break;
    
    case GLUT_KEY_F1 :
	ignoreRepeats = !ignoreRepeats;
	glutIgnoreKeyRepeat(ignoreRepeats); //This will tell GLUT whether or not to call this function again if the key is pressed twice in a row.
	
	if (ignoreRepeats)
		cout << "Repeates disabled" << endl;
	else
		cout << "Repeats enabled" << endl;
	break;
    }
}

狀態

  • GLUT_ACTIVE_SHIFT - Shift 鍵是否被按下?
  • GLUT_ACTIVE_CTRL - Ctrl 鍵是否被按下?
  • GLUT_ACTIVE_ALT - Alt 鍵是否被按下?

使用方法

int modifiers = glutGetModifiers();

if (modifiers == GLUT_ACTIVE_SHIFT){} //If shift is being pressed.
if (modifiers == GLUT_ACTIVE_CTRL | GLUT_ACTIVE_ALT){} //If ctrl <u>and</u> alt are being pressed.

狀態

  • GLUT_KEY_F1GLUT_KEY_F12
  • GLUT_KEY_LEFT
  • GLUT_KEY_UP
  • GLUT_KEY_RIGHT
  • GLUT_KEY_DOWN
  • GLUT_KEY_PAGE_UP
  • GLUT_KEY_PAGE_DOWN
  • GLUT_KEY_HOME
  • GLUT_KEY_END
  • GLUT_KEY_INSERT

使用方法

void KeyboardSpecial(int key, int x, int y){
    if (key == GLUT_KEY_LEFT){} //Left arrow key
}

沒有找到你正在尋找的鍵?這可能是因為它仍然是 ascii 字元(檢視值 0-31 和 127),因此可以透過 鍵盤按下鍵盤抬起 訪問。

滑鼠點選

[編輯 | 編輯原始碼]

設定滑鼠點選時呼叫的函式。

void InitScene(){
    //Other functions will be here
    glutMouseFunc(MouseClicked); //Telling glut what function to call when the event occurs
}


void MouseClicked(int button, int state, int x, int y){ 
                  //button - The id of the button on the mouse that's clicked, see below for IDs of buttons
                  //state - is it being pressed or released?, see below for possible states
                  //x - The x coordinate of the mouse when it was pressed
                  //y - The y coordinate of the mouse when it was pressed

    //Handle what will happen when a button on the mouse is clicked
}

引數 button

  • button == GLUT_LEFT_BUTTON - 滑鼠左鍵點選
  • button == GLUT_RIGHT_BUTTON - 滑鼠右鍵點選
  • button == GLUT_MIDDLE_BUTTON - 滑鼠中鍵點選/滑鼠滾輪點選
  • button == 3 - 滑鼠滾輪向上滾動 (還有另一種檢測方法)
  • button == 4 - 滑鼠滾輪向下滾動 (還有另一種檢測方法)

引數 state

  • state == GLUT_DOWN - 按鈕型別已被按下
  • state == GLUT_UP - 按鈕型別已被釋放

滑鼠移動

[編輯 | 編輯原始碼]

這將處理滑鼠移動的情況。

這將包含兩個函式,它們執行以下操作

  • glutMotionFunc
在滑鼠移動並且滑鼠被點選(拖動)時呼叫。
  • glutPassiveMotionFunc
在滑鼠移動並且滑鼠沒有被點選(通常是移動)時呼叫。


void InitScene(){
    //Other functions will be here
    glutMotionFunc(MouseMoveAndClicked); //Telling glut what function to call when the event occurs
    glutPassiveMotionFunc(MouseMoveAndUnClicked); //Telling glut what function to call when the event occurs
}



void MouseMoveAndClicked(int x, int y){ 
                  //x - The x coordinate of the mouse when it was pressed
                  //y - The y coordinate of the mouse when it was pressed

    //Handle what will happen when a the mouse is moving and being clicked (dragging).
}

void MouseMoveAndUnClicked(int x, int y){ 
                  //x - The x coordinate of the mouse when it was pressed
                  //y - The y coordinate of the mouse when it was pressed

    //Handle what will happen when a the mouse is moving and NOT being clicked
}

替代方案

[編輯 | 編輯原始碼]

如果你想要對所有型別的滑鼠移動(點選和未點選)呼叫相同的函式,這是一種替代程式碼。

void InitScene(){
    //Other functions will be here
    glutMotionFunc(MouseMoving); //Telling glut what function to call when the event occurs
    glutPassiveMotionFunc(MouseMoving); //Telling glut what function to call when the event occurs
}



void MouseMoving(int x, int y){ 
                  //x - The x coordinate of the mouse when it was pressed
                  //y - The y coordinate of the mouse when it was pressed

    //Handle what will happen when a the mouse is moving.
}

滑鼠滾輪滾動

[編輯 | 編輯原始碼]

這是檢測滑鼠滾輪移動的另一種方法 (另一種方法).

void InitScene(){
    //Other functions will be here
    glutMouseWheelFunc(mouseWheel); //Telling glut what function to call when the event occurs
}

void mouseWheel(int button, int dir, int x, int y)
{
    if (dir > 0){
        // Zoom in
    }else if (dir < 0){
        // Zoom out
    }
    return;
}
  1. https://www.opengl.org/resources/libraries/glut/spec3/node73.html
  2. https://www.opengl.org/resources/libraries/glut/spec3/node54.html
華夏公益教科書