Skip to content

Instantly share code, notes, and snippets.

@adamjs
Last active October 16, 2023 23:52
Show Gist options
  • Save adamjs/25d01503178d8d26f7692a2d3641a159 to your computer and use it in GitHub Desktop.
Save adamjs/25d01503178d8d26f7692a2d3641a159 to your computer and use it in GitHub Desktop.
Mouse Input in Ultralight (1.3 API)

Sending Mouse Input to a View

To send mouse input to a View, you should first create a MouseEvent and then pass it to the function View::FireMouseEvent.

Mouse Move Event

You should send a Mouse Move event whenever the mouse changes position.

Here is an example of moving the mouse to (100, 100) coordinate with no buttons pressed:

MouseEvent evt;
evt.type = MouseEvent::kType_MouseMoved;
evt.x = 100;
evt.y = 100;
evt.button = MouseEvent::kButton_None;

view->FireMouseEvent(evt);

Mouse Down Event

You should send a mouse down event whenever a mouse button is pressed.

Here is an example of pressing the left mouse-button at the (200, 200) coordinate:

MouseEvent evt;
evt.type = MouseEvent::kType_MouseDown;
evt.x = 200;
evt.y = 200;
evt.button = MouseEvent::kButton_Left;

view->FireMouseEvent(evt);

Mouse Up Event

You should send a mouse down event whenever a mouse button is released.

Here is an example of releasing the left mouse-button at the (200, 200) coordinate:

MouseEvent evt;
evt.type = MouseEvent::kType_MouseUp;
evt.x = 200;
evt.y = 200;
evt.button = MouseEvent::kButton_Left;

view->FireMouseEvent(evt);

Handling Mouse Cursor Events

The View may sometimes change the displayed mouse cursor in response to user interactions (for example, the "hand" cursor should be displayed when a user hovers over a link).

To receive notification of cursor events, you should inherit from the ViewListener class and handle the ViewListener::OnChangeCursor callback:

class MyViewListener : public ViewListener {
public:
  MyViewListener() {}
  virtual ~MyViewListener() {}
  
  // Inherited from ViewListener.
  // The View will call this when the mouse cursor changes.
  virtual void OnChangeCursor(ultralight::View* caller, Cursor cursor)
  {
     // Handle the event here.
  }
};

You can bind MyViewListener to a View by calling View::set_view_listener:

MyViewListener* listener = new MyViewListener();
view->set_view_listener(listener);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment