Up-Down buttons in a widget menu navigates away from widget

Former Member
Former Member
I'm using this custom menu inside my widget. When pressing the up/down buttons, I'm returning true from the InputDelegate.onKey, but it seems to be sending the event to the system, navigating between widgets/main screen. This works fine when using the simulator.


using Toybox.WatchUi as Ui;
using Toybox.System as Sys;
using Toybox.Graphics as Gfx;
using Toybox.Application as App;

class WidgetMenuApp extends App.AppBase {
function initialize() {
AppBase.initialize();
}
function getInitialView() {
return [ new WidgetMenuView(), new MainViewInputDelegate() ];
}
}

class WidgetMenuView extends Ui.View {
function onUpdate(dc) {
dc.setColor( Gfx.COLOR_BLACK, Gfx.COLOR_BLACK );
dc.clear();
dc.setColor( Gfx.COLOR_DK_BLUE, Gfx.COLOR_TRANSPARENT );
dc.drawText( dc.getWidth() / 2, dc.getHeight() / 2, Gfx.FONT_LARGE, "Main screen", Gfx.TEXT_JUSTIFY_CENTER );
}
}

class MainViewInputDelegate extends Ui.InputDelegate
{
function onKey(key) {
Sys.println("key pressed :" +key.getKey() );
if(key.getKey() == Ui.KEY_ENTER || key.getKey() == Ui.KEY_MENU) {
var menuView = new CustomMenuView();
Ui.switchToView( menuView, new CustomMenuViewInputDelegate(), Ui.SLIDE_IMMEDIATE );
}
}
}

class CustomMenuView extends Ui.View {
function onUpdate(dc) {
dc.setColor( Gfx.COLOR_BLACK, Gfx.COLOR_BLACK );
dc.clear();

dc.setColor( Gfx.COLOR_DK_BLUE, Gfx.COLOR_TRANSPARENT );
dc.drawText( dc.getWidth() / 2, dc.getHeight() / 2, Gfx.FONT_LARGE, "Menu text", Gfx.TEXT_JUSTIFY_CENTER );
}
}


class CustomMenuViewInputDelegate extends Ui.InputDelegate
{
function onKey(key) {
Sys.println("key pressed :" +key.getKey() );
if(key.getKey() == Ui.KEY_ENTER || key.getKey() == Ui.KEY_MENU) {
Sys.println("Pressed enter or menu");
}
if(key.getKey() == Ui.KEY_UP ) {
Sys.println("Pressed up");
}
if(key.getKey() == Ui.KEY_DOWN ) {
Sys.println("Pressed down");
}
return true;
}
}

  • You are not returning true from MainViewInputDelegate.onKey(). I'm not sure if it is the source of the problem, but it seems suspect. Although the language doesn't require it, you should be sure to explicitly return from functions that are expected to provide return values.

    Travis
  • On second look, I see the problem.

    You are using switchToView() in MainViewInputDelegate. If you are implementing a widget, you must use pushView() to 'enter' your widget. Once the user has entered your widget, the key up/down (and a few others) will be sent to the most recently pushed input delegate. Otherwise those events are sent to the system.

    Travis
  • Former Member
    Former Member over 9 years ago
    Thanks. The pushView() method worked. It did not work initially as I was conditionally doing a pushView() from the main view on startup. It seems that switching to a custom view without user interaction does not work. This is allowed in the simulator. I had to do a workaround by waiting for the user to navigate.