This tutorial demonstrates forwarding keyboard and mouse input to a Coherent::UI:View
when the view is on the whole screen (e.g. HUD).
The sample solution is already configured and you only have to compile and run it.
It uses the sample application framework described in the previous sections (Base Application Framework, Sample Application Framework).
The output will be in the Coherent/Samples/UI/bin directory.
ApplicationBase::OnKeyEvent
, ApplicationBase::OnMouseEvent
and ApplicationBase::OnTouchEvent
The changes from Sample - Custom File Handler are minimal.
First, the whole custom file handler code is thrown away, since we won't be using it.
The ViewEventListener
and ContextEventListener
classes haven't changed (except that the view created now doesn't use the coui:// scheme).
All the new code in this sample is just 3 methods - Application::OnKeyEvent
, Application::OnMouseEvent
(overridden from ApplicationBase
) and Application::ShouldForwardEventToView
.
Application::ShouldForwardEventToView
just ensures the view is ready to receive events. Application::OnKeyEvent
and Application::OnMouseEvent
check if the event should be forwarded and just send them to the view.
That's all you have to do to get your input in Coherent UI!
It might seem very easy, but there is a lot of work done behind the scenes by the PlatformWindow
class. When it receives a input event (keypress, mouse move, etc.) it has platform specific code that converts native events to Coherent Input events.
Mouse events are usually easy to convert as they are just positions and buttons. Key events, however, usually present trouble since platform specific key codes differ. Coherent UI uses Windows virtual key codes and that's what Coherent::UI::KeyEventData::KeyCode
should represent. This means that you should convert the key codes produced by the input subsystem of your game to Windows virtual keys before sending them to Coherent UI.
There is sample code for converting X11 events in the Base Application Framework. You can check the CoherentInputEventsConvertorX11Linux.cpp file for the implementation.