Handling Input

Input such as mouse and keyboard actions are injected into the Visualize event thread in the same way that other events are. Thus, handling input is a similar process to handling other types of events. Before reading any further, you should be aware that it is possible to handle input events using operators, which are covered at this link.

For this example, we’ll demonstrate how to handle a mouse event. Visualize intercepts and injects all mouse events from the operating system into the event thread, including mouse motion and dragging. As previously discussed, the first step to handling any event is to create an event handler class. This is accomplished by subclassing EventHandler and overriding the Handle method so that it performs your custom logic. In this example, we’ll simply get the location of the mouse event. Visualize abstracts mouse events into LocatorEvent objects. A locator event could represent an event from a traditional mouse or a touch-screen input event:

    class MyEventHandler: public HPS::EventHandler {
        ~MyEventHandler() { Shutdown(); }

        HandleResult Handle(HPS::Event const* in_event)
            MouseEvent const* mouse_event = (MouseEvent const*)in_event;
            WindowPoint location = mouse_event->Location;

            // ... do some logic

            return HandleResult::Handled; // or return NotHandled, as the case may be

Lastly, you need to ensure your event handler is subscribed to the appropriate EventDispatcher. In Visualize, there are two event dispatchers - one from the Database which dispatches events such as errors and warnings, and one from the window which dispatches input events. In this case, we’ll use the window event dispatcher.

    MyEventHandler* myEventHandler = new MyEventHandler();
    HPS::EventDispatcher dispatcher = myWindowKey.GetEventDispatcher();
    dispatcher.Subscribe(*myEventHandler, HPS::Object::ClassID<MouseEvent>());

NOTE: Make sure you retain a reference to your event handler object! If it goes out of scope, it won’t work.

The code in the above example assumes there was only one locator in the event, which is usually fine for an application using a conventional mouse. In a touch-screen application, there could by more than one locator returned if the user is touching the screen at more than one location. In that case, you should process each locator separately by examining the locatorEvent.Locators structure. In C++, that structure is an STL vector, and in C# it is a simple array of Locator objects.