Exemple #1
0
// -- KeyListener notifications
void ListBox::keyPressed(gcn::KeyEvent& keyEvent)
{
    gcn::Key key = keyEvent.getKey();

    if (key.getValue() == Key::ENTER || key.getValue() == Key::SPACE)
    {
        if (keyEvent.isShiftPressed())
        {
            const std::string actionEventId = getActionEventId();
            setActionEventId("default");
            distributeActionEvent();
            setActionEventId(actionEventId);
        }
        else
            distributeActionEvent();
    }
    else if (key.getValue() == Key::UP)
        decrementSelected();
    else if (key.getValue() == Key::DOWN)
        incrementSelected();
    else if (key.getValue() == Key::HOME)
        setSelected(0);
    else if (key.getValue() == Key::END)
        setSelected(getListModel()->getNumberOfElements() - 1);
    else
        return;

    keyEvent.consume();
}
Exemple #2
0
void ItemContainer::keyPressed(gcn::KeyEvent &event)
{
    const int columns = std::max(1, getWidth() / gridWidth);
    const int gridSlot = getVisibleSlot(getSelectedItem());
    int itemX = gridSlot % columns;
    int itemY = gridSlot / columns;

    // Handling direction keys: all of these set selectNewItem, and change
    // itemX or itemY checking only that the selection doesn't go off the top,
    // left or right of the grid.  The block below the switch statement then
    // checks that there's an item in that slot (implictly bounds-checking that
    // the selection didn't go off the bottom of the grid).
    bool selectNewItem = false;
    switch (event.getKey().getValue())
    {
        case Key::LEFT:
            if (itemX != 0)
                itemX--;

            selectNewItem = true;
            event.consume();
            break;
        case Key::RIGHT:
            if (itemX < (columns - 1))
                itemX++;

            selectNewItem = true;
            event.consume();
            break;
        case Key::UP:
            if (itemY != 0)
                itemY--;

            selectNewItem = true;
            event.consume();
            break;
        case Key::DOWN:
            itemY++;
            selectNewItem = true;
            event.consume();
            break;
        case Key::ENTER:
        case Key::SPACE:
            if (event.isShiftPressed())
            {
                const std::string actionEventId = getActionEventId();
                setActionEventId("default");
                distributeActionEvent();
                setActionEventId(actionEventId);
            }
            else
                distributeActionEvent();

            event.consume();
            break;
    }

    if (selectNewItem)
    {
        Item* selection = getItemInVisibleSlot(itemX + columns * itemY);

        if (selection)
            setSelectedItemIndex(selection->getInvIndex());
    }
}