Is it possible to use Keyboard to select instead of Touch in ListView?

Hi there,
I want to ask [the title said that] :smile:
As I know, ListView using touch to handle ( or determine ) what Item in ListView being selected.
How to about keyboard ?
Example using Key Arrows ( like Up, Down, Left, Right ) to select, press Enter to chose, or Esc to unselect. Is there any method in Layout or Widget support this feature ?
If no please help me how to do like above
Thank you

Add this to you scene, for example in you scene’s init() method:

    _eventDispatcher->removeEventListener(_keyboardListener);
    _keyboardListener = EventListenerKeyboard::create();
    _keyboardListener->onKeyReleased = [this] (EventKeyboard::KeyCode keyCode, Event *event) {
        switch (keyCode) {
            case EventKeyboard::KeyCode::KEY_RETURN:
            case EventKeyboard::KeyCode::KEY_ENTER:
            case EventKeyboard::KeyCode::KEY_KP_ENTER:
                log("Enter");
                break;
            case EventKeyboard::KeyCode::KEY_ESCAPE:
                log("Escape");
                break;
            case EventKeyboard::KeyCode::KEY_KP_UP:
            case EventKeyboard::KeyCode::KEY_UP_ARROW:
                log("Up");
                break;
            case EventKeyboard::KeyCode::KEY_KP_DOWN:
            case EventKeyboard::KeyCode::KEY_DOWN_ARROW:
                log("Down");
                break;
            case EventKeyboard::KeyCode::KEY_KP_LEFT:
            case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
                log("Left");
                break;
            case EventKeyboard::KeyCode::KEY_KP_RIGHT:
            case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
                log("Right");
                break;

            default: ;
        }
    };
    _eventDispatcher->addEventListenerWithSceneGraphPriority(_keyboardListener, this);

And instead of logging do whatever you like to your ListView :wink:

1 Like