Setting up the onTap feature for an app

I'm trying to set up a feature on an app I'm working on where something happens when the user taps on the screen.  I've got a variable pToggle which, when the value is 1, starts the event.  I'm attempting to simulate the app on Venu 3 and I've got the Input Delegate but nothing yet on the view.mc, which I'm sure I will need to link to get the action to work.  Here is my code for the Input Delegate.  I could not find anything else in the documentation for the view.mc part - please advise.

class AppInputDelegate extends WatchUi.InputDelegate {

    function onTap(clickEvent) {
        System.println(clickEvent.getType());
        System.println("Screen tapped!");
        pToggle = 1;
        return true;
    }


}

  • Have you looked at the input sample in the SDK?

  • I did look at it, but I couldn't really understand it.  The code appears to be the one setting up the functions in the SDK backend instead of an example of how to actually implement them - but I'm sure I might be mistaken.

    InputDelegate.mc:

        //! Handle a screen tap event
        //! @param evt The click event that occurred
        //! @return true if handled, false otherwise
        public function onTap(evt as ClickEvent) as Boolean {
            _parentView.setAction($.ACTION_CLICK_TAP);
            return true;

    I saw this in the InputView.mc which I could try out -  but I'm still not sure how to do it.

        //! Set the action
        //! @param newAction The new action
        public function setAction(newAction as Action) as Void {
            _action = newAction;
            _actionHits++;
    
            if (_actionHits > _behaviorHits) {
                _behavior = $.BEHAVIOR_NONE;
                _behaviorHits = _actionHits;
            }
    
            WatchUi.requestUpdate();
        }

  • In your delegate, you set pToggle=1, but how is that defined?  Can it be seen in your view?   That's step 1.  Then after you set it and before the return, do a WatchUi.requestUpdate(), so your onUpdate gets called and reacts 

    I always use BehiviorDelegate and not InputDelegate BTW, but in either case, you want to have initialize, like this:

    class WUWDelegate extends Ui.BehaviorDelegate {
     
        function initialize() {
            BehaviorDelegate.initialize();
        }
  • Yes, in my view I have pToggle as a variable set to 0 by default in my view.mc file, something like this:

    if (pToggle == 1) {
        // something happens here
    } else if (pToggle == 0) {
        // nothing happens
    }


    so I need the user to tap the screen to turn pToggle into 1.

    I have a behaviorDelegate initialised, and the code is this:

    class AppDelegate extends WatchUi.BehaviorDelegate {
    
        function initialize() {
            BehaviorDelegate.initialize();
        }
    
        function onTap(clickEvent) {
            System.println(clickEvent.getType());
            System.println("Screen tapped! - behaviour");
            pToggle = 1;
            WatchUi.requestUpdate();
            return true;
        }
    
    }

    Please advise.

  • The code appears to be the one setting up the functions in the SDK backend instead of an example of how to actually implement them - but I'm sure I might be mistaken

    Not really. The Input sample basically shows examples of handling all kinds of different input and behaviors that are available to CIQ devices.

    What it does differently than a "normal app" is it displays information about the input/behaviors on the screen. For example, on a 5-button watch, if the user holds the UP button, a normal app would probably show a settings menu, but the Input sample would display "KEY_MENU" on the screen, among other things.

  • That's fine, and I'm aware that I also need to add a button press action if I'm to apply the app to non-touchscreen devices.  But I'm testing out the Venu 3 first as a touchscreen device, and I'm still unable to simulate the touch (and I've set the touch feature on in the sim).

    I found an old version of the input sample which shows a clearer example, but still no joy.  What do you think of the code?

    AppDelegate.mc:

    class AppDelegate extends WatchUi.BehaviorDelegate {
    
        function initialize() {
            BehaviorDelegate.initialize();
        }
    
        function onTap(clickEvent) {
            View.setActionString("CLICK_TYPE_TAP");
            System.println(action_string);
            System.println(clickEvent.getType());
            System.println("Screen tapped! - behaviour");
            pToggle = 1;
            WatchUi.requestUpdate();
            return true;
        }
    
    }

    AppView.mc:

    var pToggle = 0;

    class AppView extends WatchUi.View {

    function initialize() {
    System.println("App initialized");
    View.initialize();
    action_string = "NO_ACTION";
    behavior_string = "NO_BEHAVIOR";
    status_string = "NO_EVENT";
    button_string = "PUSH_BUTTONS";
    action_hits = 0;
    behavior_hits = 0;
    ....
    }

    function setActionString(new_string) {
    action_string = new_string;
    action_hits++;

    if (action_hits > behavior_hits) {
    behavior_string = "NO_BEHAVIOR";
    behavior_hits = action_hits;
    }
    System.println(action_string);
    WatchUi.requestUpdate();
    }

    function onUpdate(dc) {
    ...
    if (pToggle == 1) {
    // do the action when the user taps the screen
    } else if (pToggle == 0} {
    // do nothing
    }
    ...
    }

  • Just to make sure, what type of app are you building?  Widget or device-app?  You can't use onTap in a watch face.

  • I've been trying to send a reply but having difficulties posting.  Bottom line - spent hours on the forum, found an answer you gave someone else.

    I'll try to post the solution here later to help anyone having the same issue.

  • Hi, yes, I'm building an app not a watchface.
    
    I managed to find the answer, after spending hours trawling the forums - and the solution was in one you posted to someone else.  It's sad really that there aren't enough good examples in the documentation and sometime the sample apps don't give what you need.
    
    To help anyone else who are having the same problems, here's what I used to trigger the action for a screen tap or ESC key, whether the device is touchscreen or not:
    
    AppApp.mc:
    
    class AppApp extends Application.AppBase {
    …
        function getInitialView() {
            newView = new AppView();
            return [ newView, new AppDelegate(newView)];
        }
    }
    
    AppDelegate.mc:
    
    class AppDelegate extends WatchUi.BehaviorDelegate {
        var view;
        function initialize(v) {
            BehaviorDelegate.initialize();
            view = v;
        }
    
        function onTap(clickEvent) {
            view.pToggle = 1;
            System.println("click event type "+clickEvent.getType());
            System.println("Screen tapped!");
            WatchUi.requestUpdate();
            return true;
        }
    
        function onKey(keyEvent) {
            if (System.getDeviceSettings().isTouchScreen == false) {
                if (keyEvent.getKey() == WatchUi.KEY_ESC) {
                    view.pToggle = 1;
                    System.println("key event type "+keyEvent.getType());
                    System.println("Key pressed!");
                    WatchUi.requestUpdate();
                }
            }
            return true;
        }
    }
    
    AppView.mc:
    
    var pToggle = 0;
    
    class AppView extends WatchUi.View {
    
        function onUpdate(dc) {
            if (pToggle == 1) {
            // do the action when the user taps the screen or presses the Esc key
            } else if (pToggle == 0} {
            // do nothing
            }
    
        }