Show / Hide Table of Contents

Introduction

OpenTK provides a number of events that can be used to respond to user actions or system changes. These are exposed through a global event queue.

To register for events all you have to do is register to the Toolkit.Event.EventRaised event. The handler should look like this:

void EventRaised(EventArgs args)
{

}

and is registered as follows:

Toolkit.Event.EventRaised += EventRaised;

With this we are ready to handle some events!

The first event most applications are going to want to handle is the Close event. We use pattern matching to see if args is of the type CloseEventArgs which tells us this is a Close event.

void EventRaised(EventArgs args)
{
    if (args is CloseEventArgs close)
    {
        // Destroy the window that the user wanted to close.
        Toolkit.Window.Destroy(closeArgs.Window);
    }
}

Window Events

Here follows a list of events that extend WindowEventArgs and what they mean.

EventArgs type Description
CloseEventArgs This event is triggered when the user presses the exit button of the window. CloseEventArgs.Window contains the handle to the window the user wanted to close.
FocusEventArgs This event is triggered when a window gains or loses input focus. FocusEventArgs.GotFocus tells if the window got or lost focus.
WindowMoveEventArgs This event is triggered when a window has its position changed on screen.
WindowResizeEventArgs This event is triggered when a window has its size changed on screen.
WindowFramebufferResizeEventArgs This event is triggered when a window has its size changed on screen.
WindowModeChangeEventArgs This event is triggered when the window mode of a window changes.
WindowScaleChangeEventArgs This event is triggered when the scale the window should display at has changed. Typically because the user has moved the window to another monitor with different scaling settings, or because the user changed system dpi settings.
MouseEnterEventArgs This event is triggered when the mouse cursor enters or exits a window.
MouseMoveEventArgs This event is triggered when the mouse moves.
RawMouseMoveEventArgs The event is triggered when the mouse moves and raw mouse input is enabled.
MouseButtonDownEventArgs This event is triggered when a mouse button is pressed.
MouseButtonDownEventArgs This event is triggered when a mouse button is pressed.
ScrollEventArgs This event is triggered when the scrollwheel on a mouse is used.
KeyDownEventArgs This event is triggered when a keyboard key is pressed.

Do not use this event to handle typing, use the TextInputEventArgs instead.
KeyUpEventArgs This event is triggered when a keyboard key is released.

Do not use this event to handle typing, use the TextInputEventArgs instead.
TextInputEventArgs This event is triggered when the user has typed text.
TextEditingEventArgs This event is triggered when the user is composing text using something like IME (e.g. Chinese, Japanese, or Korean).
FileDropEventArgs This event is triggered when a user drags files into a window.
InputLanguageChangedEventArgs This event is triggered when the input language for the window has changed.

Non-window events

Here follows a list of non-window related events.

EventArgs type Description
ThemeChangeEventArgs This event is triggered when a user changes the preferred theme.
DisplayConnectionChangedEventArgs This event is triggered when a display is connected or disconnected from the system.
PowerStateChangeEventArgs This event is triggered when the power state of the system changes. For example if the user put the system to sleep.
ClipboardUpdateEventArgs This event is triggered when the contents of the clipboard has changed.
In this article
Back to top Generated by DocFX