Viewport.keyPressed


#1

Can you change the keyPressed method in the Viewport class so that it sends key presses up the hierarchy if it doesn’t need them like this?

void Viewport::keyPressed (const KeyPress& key) { if (key.isKeyCode (KeyPress::upKey) || key.isKeyCode (KeyPress::downKey) || key.isKeyCode (KeyPress::pageUpKey) || key.isKeyCode (KeyPress::pageDownKey) || key.isKeyCode (KeyPress::homeKey) || key.isKeyCode (KeyPress::endKey)) { verticalScrollBar->keyPressed (key); } else if (key.isKeyCode (KeyPress::leftKey) || key.isKeyCode (KeyPress::rightKey)) { horizontalScrollBar->keyPressed (key); } else if (getParentComponent() != 0) { getParentComponent()->keyPressed(key); } }
This way KeyPressMappingSet still works with components in a Viewport.

Cheers,
Caleb


#2

Yes - well spotted, that’s how it should be done! Try this version:

void Viewport::keyPressed (const KeyPress& key)
{
    const bool isUpDownKey = key.isKeyCode (KeyPress::upKey)
                                || key.isKeyCode (KeyPress::downKey)
                                || key.isKeyCode (KeyPress::pageUpKey)
                                || key.isKeyCode (KeyPress::pageDownKey)
                                || key.isKeyCode (KeyPress::homeKey)
                                || key.isKeyCode (KeyPress::endKey);

    const bool isLeftRightKey = key.isKeyCode (KeyPress::leftKey)
                                 || key.isKeyCode (KeyPress::rightKey);

    if (verticalScrollBar->isVisible() && isUpDownKey)
    {
        verticalScrollBar->keyPressed (key);
    }
    else if (horizontalScrollBar->isVisible() && (isLeftRightKey || isUpDownKey))
    {
        horizontalScrollBar->keyPressed (key);
    }
    else
    {
        Component::keyPressed (key);
    }
}

this will work better if there’s only a horizontal scrollbar visible, and will pass on all keypresses if no scrollbars are visible


#3