An application can respond to mouse interaction through several event handlers that are called depending on the action being performed.
The existing handlers that respond to mouse interaction are listed in the following table:
Method |
Usage |
---|---|
This is called when the user presses a mouse button | |
This is called when the user releases a mouse button | |
This is called when the user rotates the mouse wheel | |
This is called when the mouse is moved without any button pressed | |
This is called when the mouse is moved with any button pressed |
It is not mandatory to implement all of the preceding methods; you can implement only the ones required by your application.
Implement the necessary event handlers according to the mouse events you need to respond to. For example, to create an application that responds to all available mouse events, you must implement the following code inside your main class declaration:
void mouseDown( MouseEvent event ); void mouseUp( MouseEvent event ); void mouseWheel( MouseEvent event ); void mouseMove( MouseEvent event ); void mouseDrag( MouseEvent event );
The MouseEvent
object passed as a parameter contains information about the mouse event.
We will learn how to work with the
ci::app::MouseEvent
class to respond to mouse events. Perform the following steps to do so:
Vec2i mousePos = event.getPos();
Or we can get the separate x and y coordinates by calling the getX
and getY
methods:
int mouseX = event.getX(); int mouseY = event.getY();
MouseEvent
object also lets us know which mouse button triggered the event by calling the isLeft
, isMiddle
, or isRight
methods. They return a bool
value indicating if it was the left, middle, or right button, respectively.bool leftButton = event.isLeft(); bool rightButton = event.isRight(); bool middleButton = event.isMiddle();
isLeftDown
, isRightDown
, and isMiddleDown
methods that return true
depending on whether the left, right, or middle buttons of the mouse were pressed.bool leftDown = event.isLeftDown(); bool rightDown = event.isRightDown(); bool middleDown = event.isMiddleDown();
getWheelIncrement
method returns a float
value with the movement increment of the mouse wheel.float wheelIncrement = event.getWheelIncrement();
isShiftDown
method returns true
if the Shift key was pressed, the isAltDown
method returns true
if the Alt key was pressed, isControlDown
returns true
if the control key was pressed, and isMetaDown
returns true
if the Windows key was pressed on Windows or the option key was pressed on OS X, isAccelDown
returns true
if the Ctrl key was pressed on Windows or the command key was pressed on OS X.18.116.80.34