游戏开发/渲染与游戏引擎/OpenGL/GLUT/处理输入指南
外观
此函数处理按键按下时调用的函数。
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_F1 到 GLUT_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;
}