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 EventQueue.EventRaised
event. The handler should look like this:
void EventRaised(PalHandle? handle, PlatformEventType type, EventArgs args)
{
}
and is registered as follows:
EventQueue.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(PalHandle? handle, PlatformEventType type, 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.
Event Enum | EventArgs type | Description |
---|---|---|
Close |
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. |
Focus |
FocusEventArgs |
This event is triggered when a window gains or loses input focus. FocusEventArgs.GotFocus tells if the window got or lost focus. |
WindowMove |
WindowMoveEventArgs |
This event is triggered when a window has its position changed on screen. |
WindowResize |
WindowResizeEventArgs |
This event is triggered when a window has its size changed on screen. |
WindowFramebufferResize |
WindowFramebufferResizeEventArgs |
This event is triggered when a window has its size changed on screen. |
WindowModeChange |
WindowModeChangeEventArgs |
This event is triggered when the window mode of a window changes. |
WindowScaleChange |
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. |
MouseEnter |
MouseEnterEventArgs |
This event is triggered when the mouse cursor enters or exits a window. |
MouseMove |
MouseMoveEventArgs |
This event is triggered when the mouse moves. |
RawMouseMove |
RawMouseMoveEventArgs |
The event is triggered when the mouse moves and raw mouse input is enabled. |
MouseDown |
MouseButtonDownEventArgs |
This event is triggered when a mouse button is pressed. |
MouseUp |
MouseButtonDownEventArgs |
This event is triggered when a mouse button is pressed. |
Scroll |
ScrollEventArgs |
This event is triggered when the scrollwheel on a mouse is used. |
KeyDown |
KeyDownEventArgs |
This event is triggered when a keyboard key is pressed. Do not use this event to handle typing, use the TextInput event instead. |
KeyUp |
KeyUpEventArgs |
This event is triggered when a keyboard key is released. Do not use this event to handle typing, use the TextInput event instead. |
TextInput |
TextInputEventArgs |
This event is triggered when the user has typed text. |
TextEditing |
TextEditingEventArgs |
This event is triggered when the user is composing text using something like IME (e.g. Chinese, Japanese, or Korean). |
FileDrop |
FileDropEventArgs |
This event is triggered when a user drags files into a window. |
InputLanguageChanged |
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.
Event Enum | EventArgs type | Description |
---|---|---|
ThemeChange |
ThemeChangeEventArgs |
This event is triggered when a user changes the preferred theme. |
DisplayConnectionChanged |
DisplayConnectionChangedEventArgs |
This event is triggered when a display is connected or disconnected from the system. |
PowerStateChange |
PowerStateChangeEventArgs |
This event is triggered when the power state of the system changes. For example if the user put the system to sleep. |
ClipboardUpdate |
ClipboardUpdateEventArgs |
This event is triggered when the contents of the clipboard has changed. |