Upgrade to Pro — share decks privately, control downloads, hide ads and more …

12. Keyboard & Mouse

12. Keyboard & Mouse

94870c02836167043f37f05ae1032690?s=128

Tatsuya Yatagawa

June 04, 2021
Tweet

More Decks by Tatsuya Yatagawa

Other Decks in Technology

Transcript

  1. 12. Keyboard and mouse Tatsuya Yatagawa

  2. Keyboard events in GLFW ◼ Register callback function ◼ glfwSetKeyCallback

    ◼ 1st parameter: Window pointer (GLFWwindow*) ◼ 2nd parameter: Function pointer of callback ◼ Code Computer Graphics Course @Waseda University 2
  3. Callback function for keyboard event ◼ Parameters of callback function

    ◼ 1st: Window pointer ◼ 2nd: Key type (ASCII code is stored) ◼ 3rd: Platform-dependent scan code (not used) ◼ 4th: Key is pressed or released? ◼ 5th: Status of special keys (Shift, Ctrl, Alt) ◼ Code Computer Graphics Course @Waseda University (Simple key press and print its type and action) 3
  4. Identify key types ◼ Key types in GLFW ◼ GLFW_KEY_X

    is defined as an enumeration. ◼ e.g.) “A” → GLFW_KEY_A, “1” → GLFW_KEY_1 ◼ GLFW_KEY_X is defined by ASCII codes(*1) ◼ e.g.) GLFW_KEY_A → 0x41, GLFW_KEY_1 → 0x31 Computer Graphics Course @Waseda University *1) ASCII - Wikipedia https://en.wikipedia.org/wiki/ASCII By converting to char type, it can be printed as a character. 4
  5. Identify event types ◼ Type of keyboard events in GLFW

    ◼ Any key is pressed → GLFW_PRESS ◼ Any key is released → GLFW_RELEASE ◼ When key is kept pressed, key-pressed event is repeatedly detected. Computer Graphics Course @Waseda University 5
  6. Detect special key status ◼ Special keys in GLFW ◼

    Shift key → GLFW_MOD_SHIFT (0x01) ◼ Ctrl key → GLFW_MOD_CONTROL (0x02) ◼ Alt key (Option in MacOS) → GLFW_MOD_ALT (0x04) ◼ Super key → GLFW_MOD_SUPER (0x08) (Super key = Windows key for Windows, Command key for MacOS) Computer Graphics Course @Waseda University Special key is defined as a bit mask. 6
  7. Mouse events in GLFW ◼ Mouse press/release events ◼ Set

    callback with glfwSetMouseButtonCallback ◼ Parameters for callback function ◼ 1st: Window on which event is detected ◼ 2nd: Button type ◼ 3rd: Whether key is pressed or released ◼ 4th: Status of special keys ◼ Mouse move events ◼ Set callback with glfwSetCursorPosCallback ◼ Parameters for callback function ◼ 1st: Window on which event is detected ◼ 2nd: X-coordinate to which mouse moved ◼ 3rd: Y-coordinate to which mouse moved Computer Graphics Course @Waseda University 7
  8. Mouse press/release events ◼ Callback parameters do not receive mouse

    position → glfwGetCursorPos provides the position Computer Graphics Course @Waseda University 8
  9. Mouse move events ◼ Event is detected when mouse is

    moved over window ◼ Callback for move receives the mouse position Computer Graphics Course @Waseda University 9
  10. How to achieve mouse drag? ◼ Basic idea ◼ Mouse

    drag = mouse press + mouse move (before mouse release) ◼ Check mouse is keep pressed and then check it moves. ◼ ToDo ◼ Prepare a bool (global variable) to represent whether mouse button is pressed or not ◼ If button is pressed, set the bool as “true”. If button is released, set the button as “false”. ◼ If cursor position callback is called while the button is pressed, then the event is “drag”. Computer Graphics Course @Waseda University 10
  11. Exercise 12-1 ◼ Implement mouse drag using mouse press/release and

    move callbacks. When mouse is dragged, output position and pressed keys. ◼ Hint: To identify pressed/released mouse buttons, use: ◼ GLFW_MOUSE_BUTTON_LEFT (left button) ◼ GLFW_MOUSE_BUTTON_RIGHT (right button) ◼ GLFW_MOUSE_BUTTON_MIDDLE (middle button) Computer Graphics Course @Waseda University 11