Inputs in widget not detected

Hello.

I'm developing a widget.

It should show this behavior:

- Show main view.

- On UP/DOWN press: increment/decrement a variable, recalculate stuff and redraw main view.

- On MENU press: show confirmation dialog to reset data.

- On BACK press: show Glance (if available) or exit.

I've used the Input sample application included in the SDK. Added the InputDelegate.mc class to my project and added an instance of the InputDelegate to the function calling the main view, in he main App class.

In the InputDelegate.onKey function (for the moment) I check for the key pressed, print it using System.println and continue. The MENU press is already calling the confirmation in this code:

    public function onKey(evt as KeyEvent) as Boolean {
        System.println("InputDelegate.onKey");
        var key = evt.getKey();

        var button = getButton(key);

        _parentView.setButton(button);

        var keyAction = getKeyAction(key);

        if (keyAction != null) {
            _parentView.setAction(keyAction);
        }

        if (key == KEY_ENTER) {
            System.println("MOB");
            var message = Application.loadResource( Rez.Strings.ResetData );
            var dialog = new WatchUi.Confirmation(message);
            WatchUi.pushView (
                dialog,
                new MyConfirmationDelegate(),
                WatchUi.SLIDE_IMMEDIATE
            );

        } else if (key == KEY_DOWN) {
            System.println("DOWN");
        } else if (key == KEY_UP) {
            System.println("UP");
        } else if (key == KEY_ESC) {
            System.println("ESC");
        }

        if (key == KEY_ESC) {
            if (_lastKey == KEY_ESC) {
                System.exit();
            }
        }

        _lastKey = key;

        return true;
    }

Issue is: MENU works as expected. UP/DOWN/BACK are never intercepted. If I press any of those the widget calls view.onHide and app.onStop and exits cleanly.

How can I intercept the UP/DOWN/BACK buttons? Aren't they available to widgets?

Thanks in advance!

  • for widgets (not started from a glance), up/down exit the widget and move through the widget loop.

    What you can do is push a second view and handle up/down in that view's delegate.  Instead of using onKey(), you may consider using onNextPage(), onPreviousPage() as that will also handle swipes on touch devices.  Make sure you always return true or false in all cases in your delegate.

  • Thanks Jim!

    I got it (I guess). Let me rephrase it to confirm my understanding.

    I have a widget with a glance view and an initial view, both called from the main app class. The getInitialView includes the call to the view and to the input delegate:

        // Return the initial view of your application here
        function getInitialView() as Array<Views or InputDelegates>? {
            System.println("App.getInitialView()");
            var view = new $.MainView();
            var delegate = new $.MainViewDelegate(view);
            return [view, delegate] as Array<Views or InputDelegates>;
        }
    
        // Return the initial view of your application here
        function getGlanceView() as Array<Views or InputDelegates>? {
            System.println("App.getGlanceView()");
            return [ new GlanceView() ] as Array<Views or InputDelegates>;
        }

    That works.

    When I run the app it shows the Glance (if glance view is enabled) and then on 'SELECT' shows the MainView. If glance view is not enabled it goes directly to the MainView.

    Then, once in the MainView the only button I can capture is the SELECT. Any other (UP/DOWN/BACK) cleanly exit the widget.

    That is the expected behavior and capturing UP/DOWN/BACK from the Main View of a widget is not possible.

    I should call from the MainView a second view when the SELECT button is pressed.

    That second view will be able to capture the UP/DOWN/BACK buttons.

    I tried like that and it works: I can capture the inputs in the second view.

    Thanks again Jim!