07-27-2008 01:55 PM
I'd like my app to conveniently handle the trackball clicks on capable devices, which provide a separate key for accessing the menu. However, I'd still like to work on older trackwheel devices, which don't have a separate menu key.
To this end, I've noticed that I can start by overriding the Screen.trackwheelClick() method. However, I'd like to find a way to differentiate between trackwheel and trackball clicks, and not use any magic numbers in my code.
My own quick testing has found that the status parameter of trackwheelClick() is different on the two kinds of devices. With a 7100t (wheel), its "17039360", and with an 8820 (ball), its "536870912". If I convert to binary, both these numbers look quite clean. However, I'd rather use official API constants for my checks in trackwheelClick().
Any recommendations?
Solved! Go to Solution.
07-28-2008 10:21 AM
07-28-2008 10:40 AM - last edited on 07-29-2008 04:07 AM
Hi,
Overriding Screen.trackwheelClick() isn't recommended for various reasons, including the fact that it may be called internally by other events such as the invocation of menu buttons, etc.
An elegant way to support/differentiate trackwheel vs. trackball usage with a single build is to check the instance parameter of the Screen.onMenu() event. On a trackball device this value will be 65536 when the trackball is clicked or 1073741824 when the main menu button is used (default = 0 for trackwheel devices). You can therefore implement your logic as below. This also has the desired effect that the menu will also be displayed in the correct screen location depending on whether the main menu button (menu bottom left) vs. trackball (context menu, bottom) or trackwheel menu (menu top right) is clicked. You can obviously override any of these to capture and action just the click (for trackwheel or trackball).
private final static int MENU_CONTEXT = 65536; private final static int MENU_MAIN = 1073741824; public boolean onMenu(int instance) { if (instance == MENU_MAIN) //main menu button pressed, display menu bottom left { super.onMenu(instance); return false; } else if (instance == MENU_CONTEXT) //trackball click (context menu, display bottom) - override here { return openItem(); } else //trackwheel click, display main menu top right (or override if required) { super.onMenu(instance); return false; } }
Hope that helps,
James
07-28-2008 07:44 PM
James,
I was actually investigating that approach before I read up on trackwheelClick(). But again, I don't like the idea of magic numbers that aren't tied to constants provided by the API. As such, I wonder if those values to come from constants defined in the API (some version of it). However, for the time being, your solution probably is the best approach, so I'll go ahead and use it. (and if anyone wonders, on my 7100t simulator, instance=0)
Thanks.
07-28-2008 07:45 PM
Wouldn't the following make more sense (and be easier to remember, and less error prone)?
private final static int MENU_CONTEXT = 0x10000; private final static int MENU_MAIN = 0x40000000;
07-29-2008 04:04 AM
You'll find these constants in the JDE starting with 4.2 in the net.rim.device.api.ui.component.Menu class - see below - but these obviously aren't defined in JDE 4.1 since the menu key and the concept of a contextual menu didn't exist until OS 4.2.
public static final int INSTANCE_CONTEXT //Menu instance is the context one. public static final int INSTANCE_CONTEXT_SELECTION //Menu instance is during selection. public static final int INSTANCE_DEFAULT //Menu instance is the default one.
Kind regards,
James