ExTermbox v0.3.3 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
:
- The
ExTermbox.Bindings.poll_event/1
NIF is called with the event manager's pid. - The NIF creates a new thread for the blocking poll routine and immediately returns with a resource representing a handle for the thread.
- 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.
- 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 pid to future event notifications
Link to this section Functions
child_spec(init_arg) View Source
Returns a specification to start this module under a supervisor.
See Supervisor
.
start_link(opts \\ []) View Source
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.
subscribe(pid \\ __MODULE__, subscriber_pid) View Source
Subscribes the given pid to future event notifications.