Polycode provides keyboard, mouse, joystick and touch handling on platforms where they are supported. You can handle input by listening to events dispatched by Polycode (see Event System) in C++ or Lua, in Lua by implementing global functions that are called automatically by the Lua binding system or by checking the input state via the CoreInput class.

Contents

Input overview.

The center of the input system in Polycode is the CoreInput class, which is accessed via CoreServices or from the Core directly. It dispatches events (see Event System) for all input that happens in the application. For each input event, it dispatches an InputEvent class, which contains information about the input event, such as which button was pressed or what the new mouse position is. In Lua, Polycode also provides a way to handle input without dealing with events by automatically calling specific global Lua functions.

CoreInput also provides a way to check input state directly at any time, such as seeing if a key or a mouse button is currently being pressed.

Keyboard input.

Polycode dispatches keyboard key press and release events and allows for checking the pressed state of any keyboard key. Keyboard input events contain information about which key was pressed or released and their corresponding character codes for text input.

To listen to keyboard input via events, you must add an event listener to CoreInput:

InputEvent::EVENT_KEYDOWN - for key presses.

InputEvent::EVENT_KEYUP - for key releases.

C++
Services()->getInput()->addEventListener(this, InputEvent::EVENT_KEYDOWN);
Services()->getInput()->addEventListener(this, InputEvent::EVENT_KEYUP);
Lua
Services.Input:addEventListener(nil, onKeyDownEvent, InputEvent.EVENT_KEYDOWN)
Services.Input:addEventListener(nil, onKeyUpEvent, InputEvent.EVENT_KEYUP)

When handling the event, the keyboard key that was pressed or released will be passed as part of CoreInput's InputEvent, which you have to cast Event to when you are handling events dispatched from CoreInput. You can get the event key using the keyCode() method of InputEvent. Keys are defined as a PolyKEY enum for each key on the keyboard and all start with KEY_ followed by the name of the key, such as KEY_SPACE or KEY_a. Refer to PolyInputKeys.h for the full list of keys.

C++
void ExampleApp::handleEvent(Event *event) {
    if(event->getDispatcher() == Services()->getInput()) {
        InputEvent *inputEvent = (InputEvent*) event;
        switch(event->getEventCode()) {
            case InputEvent::EVENT_KEYDOWN:
                switch(inputEvent->keyCode()) {
                    case KEY_LEFT:
                        cout << "LEFT WAS PRESSED!\n";
                    break;
                }
            break;
        }
    }
}
Lua
function onKeyDownEvent(t, event)
	local inputEvent = safe_cast(event, InputEvent)
	if inputEvent:keyCode() == KEY_LEFT then
		print("LEFT WAS PRESSED")
	end
end

If what you are interested in is the character code being typed on the keyboard (for example if you are making a text input field) and not the key itself, you can use the getCharCode() method of InputEvent to see what character was typed.

You can also check if a key is currently being pressed by calling the getKeyState method of CoreInput for a specific key you want to check.

C++
if(Services()->getInput()->getKeyState(KEY_LEFT)) {
    // left is down
}
Lua
if Services.Input:getKeyState(KEY_LEFT) then
	-- left is down
end

Alternatively, in Lua, you can also implement a global function called onKeyDown for presses and onKeyUp for releases, which will be called automatically on keyboard input.

Lua
function onKeyDown(key)
	if key == KEY_LEFT then
		print("LEFT WAS PRESSED")
	end
end

Mouse input.

Polycode dispatches events for mouse movement, button presses and mouse wheel movement (mouse wheel presses come in as a mouse button press).

To listen to mouse input via events, you must add an event listener to CoreInput:

InputEvent::EVENT_MOUSEMOVE - for mouse movement

InputEvent::EVENT_MOUSEDOWN - for mouse button presses

InputEvent::EVENT_MOUSEUP - for mouse button releases

InputEvent::EVENT_MOUSEWHEEL_UP - for mousewheel up movement

InputEvent::EVENT_MOUSEWHEEL_DOWN - for mousewheel down movement.

C++
Services()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
Services()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
Lua
Services.Input:addEventListener(nil, onMouseMoveEvent, InputEvent.EVENT_MOUSEMOVE)
Services.Input:addEventListener(nil, onMouseDownEvent, InputEvent.EVENT_MOUSEDOWN)

When handling mouse events, the input event will contain the mouse position (in pixels) and which mouse button was pressed or released. The mouse button is represented by an index with 0 being the left mouse button, 1 being right mouse button, 2 being the middle (or mouse wheel) and higher indexes for mice that support more than 3 buttons.

C++
void ExampleApp::handleEvent(Event *event) {
    if(event->getDispatcher() == core->getInput()) {
        InputEvent *inputEvent = (InputEvent*) event;
        switch(event->getEventCode()) {
            case InputEvent::EVENT_MOUSEMOVE:
                cout << "MOUSE X:" << inputEvent->mousePosition.x << endl;
                cout << "MOUSE Y:" << inputEvent->mousePosition.y << endl;
            break;
            case InputEvent::EVENT_MOUSEDOWN:
                cout << "Mouse button:" << inputEvent->mouseButton << endl;
            break;
        }
    }
}
Lua
function onMouseMoveEvent(t, event)
	local inputEvent = safe_cast(event, InputEvent)
	print("MOUSE X:"..inputEvent.mousePosition.x)
	print("MOUSE Y:"..inputEvent.mousePosition.y)
end

function onMouseDownEvent(t, event)
	local inputEvent = safe_cast(event, InputEvent)
	print("Mouse button:"..inputEvent.mouseButton)
end

If you just want the delta of the mouse movement, for exampe if you are bulding a mouse-controlled first person game, you can use the getMouseDelta() method of CoreInput to get the delta of the changed mouse position after a mouse move event.

You can also check if a mouse button is currently being pressed by calling the getMouseButtonState method of CoreInput for a specific mouse button you want to check.

C++
if(Services()->getInput()->getMouseButtonState(0)) {
    // left mouse button is down
}
Lua
if Services.Input:getMouseButtonState(0) then
	-- left mouse button is down
end

Alternatively, in Lua, you can also implement a global function called onMouseDown for mouse button presses, onMouseUp for releases and onMouseMove for mouse movement, which will be called automatically on mouse input.

Lua
function onMouseDown(button, x, y)
	if button == 0 then
		print("LEFT MOUSE CLICK AT "..x..","..y)
	end
end

Joystick input.

TBD

Multi-touch input.

Polycode supports multi-touch input on supported platforms. On desktops, multi-touch is supported natively in Windows versions 7 and above and on all platforms via the TUIO module.

Polycode dispatches input events when touches begin, move or end. To listen to touch events, you must add an event listener to CoreInput:

InputEvent::EVENT_TOUCHES_BEGAN - for new touches (when a finger first touches the screen).

InputEvent::EVENT_TOUCHES_MOVED - when touches move (when a finger moves without being lifted).

InputEvent::EVENT_TOUCHES_ENDED - when touches end (a finger is lifted from the screen).

When handling touch input, the InputEvent will contain information about the touch that the event pertains to, as well as all the other touches that are happening at the moment. Information for each touch is stored in the TouchInfo class, which contains a unique numeric id of the touch and its position. TouchInfo for the touch that the even is being generated for is stored in the touch member of InputEvent and all the other concurrent touches are stored in the touches vector of TouchInfo classes in InputEvent.

C++
void ExampleApp::handleEvent(Event *event) {
    if(event->getDispatcher() == core->getInput()) {
        InputEvent *inputEvent = (InputEvent*) event;
        switch(event->getEventCode()) {
            case InputEvent::EVENT_TOUCHES_BEGAN:
                cout << "TOUCH BEGAN (" << inputEvent->touch.id << ")\n";
            break;
            case InputEvent::EVENT_TOUCHES_MOVED:
                cout << "CURRENT TOUCHES:\n";
                for(int i=0; i < inputEvent->touches.size(); i++) {
                    cout << "TOUCH ID:" << inputEvent->touches[i].id;
                    cout << " X:" << inputEvent->touches[i].position.x;
                    cout << " Y:" << inputEvent->touches[i].position.y << endl;
                }
            break;
        }
    }
}

You can simulate touch events on a system that doesn't support touch input by setting the simulateTouchWithMouse boolean in CoreInput to true. You can also enable mouse input on a system that only has touch input by setting the simulateMouseWithTouch boolean in CoreInput to true.