Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

Guide to Game Development/Rendering and Game Engines/OpenGL/GLUT/Handling input

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Keyboard Keyboard down This handles the function that gets called when a key is pressed down. 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 } Keyboard up This handles the function that gets called when a key is released. 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 } Some invisible characters Keyboard down and keyboard up can be used to get some 'invisible' character too like backspace and delete, see ascii table, values 0-31 and 127. For ones that don't appear there, see Special keyboard input. Example use: 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 Special keyboard input This is used for keyboard input that don't have an ascii character associated with them (e.g. Arrow up, F1). For every key combination you should look at this reference for the name of the key you need. There are also a reference list for the possible modifiers that you can use. 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; } } Modifiershttps://www.opengl.org/resources/libraries/glut/spec3/node73.html States: GLUT_ACTIVE_SHIFT - Is shift being pressed? GLUT_ACTIVE_CTRL - Is control being pressed? GLUT_ACTIVE_ALT - Is alt being pressed? Use: 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. Special keyshttps://www.opengl.org/resources/libraries/glut/spec3/node54.html States: GLUT_KEY_F1 to 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 Use: void KeyboardSpecial(int key, int x, int y){ if (key == GLUT_KEY_LEFT){} //Left arrow key } Didn't find the key you were looking for? This is probably because it's still an ascii character (see values 0-31 and 127) and so can be accessed through keyboard down and keyboard up. Mouse Mouse Clicked Set-up the function that gets called when the mouse is clicked. 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 } Parameter button: button == GLUT_LEFT_BUTTON - Left mouse click button == GLUT_RIGHT_BUTTON - Right mouse click button == GLUT_MIDDLE_BUTTON - Middle mouse click/mouse wheel clicked button == 3 - Mouse wheel scrolled up (there's another way to detect this) button == 4 - Mouse wheel scrolled down (there's another way to detect this) Parameter state: state == GLUT_DOWN - The button type has been pressed down state == GLUT_UP - The button type has been released Mouse movement This will handle when the mouse is moved. This will contain two functions that do the following: glutMotionFunc Called when the mouse is moving AND the mouse is clicked (dragging). glutPassiveMotionFunc Called when the mouse is moving AND the mouse isn't clicked (generally moving). 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 } Alternative This is an alternative piece of code for if you want to call the same function for all types of mouse movement (clicked and unclicked) 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. } Mouse Wheel Scrolling This is an alternative way to detect mouse wheel movements (other way). 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; } 75%