ExTermbox v0.3.5 ExTermbox.EventManager View Source

This module implements an event manager that notifies subscribers of the keyboard, mouse and resize events received from the termbox API.

It works by running a poll loop that calls out to the NIFs in ExTermbox.Bindings:

  1. The ExTermbox.Bindings.poll_event/1 NIF is called with the event manager's pid.
  2. The NIF creates a new thread for the blocking poll routine and immediately returns with a resource representing a handle for the thread.
  3. The thread blocks until an event is received (e.g., a keypress), at which point it sends a message to the event manager with the event data and exits.
  4. The event manager notifies its subscribers of the event and returns to step 1.

Example Usage:

def event_loop do
  receive do
    {:event, %Event{ch: ?q} = event} ->
      Bindings.shutdown()
    {:event, %Event{} = event} ->
      # handle the event and wait for another...
      event_loop()
  end
end

{:ok, pid} = EventManager.start_link()
:ok = EventManager.subscribe(self())
event_loop()

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor

Starts an event manager process linked to the current process

Subscribes the given subscriber pid to future event notifications

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Starts an event manager process linked to the current process.

Running multiple instances of the event manager process simultaneously is discouraged, as it could crash the NIF or cause unexpected behavior. By default, the process is registered with a fixed name to prevent this.

Link to this function

subscribe(event_manager_server \\ __MODULE__, subscriber_pid) View Source

Subscribes the given subscriber pid to future event notifications.