Question

I use the following code to set the sensitivity of the trackball

public class Main extends UiApplication {

    public static void main(String[] args) {
        Main theApp = new Main();
        theApp.enterEventDispatcher();
    }

    public Main() {
        if (Trackball.isSupported()) {
            Trackball.setFilter(Trackball.FILTER_ACCELERATION);
            Trackball.setSensitivityX(20);
            Trackball.setSensitivityY(20);
        }
        pushScreen(new LoginScreen());
    }
}

Here's the screen used:

public class LoginScreen extends MainScreen {
    public LoginScreen() {
        super(MainScreen.NO_VERTICAL_SCROLL | MainScreen.NO_VERTICAL_SCROLLBAR);
        add(...) // SOME COMPONENTS ARE ADDED HERE
    }
}

When I check the sensitivity of trackball in LoginScreen using Trackball.getSensitivityX() and Trackball.getSensitivityY() in navigationMovement, it returns "2147483647", meanwhile if I checked it after setting it immediately in Main it returns "20" !


So I moved the setting block inside the LoginScreen itself as follows:

 public class LoginScreen extends MainScreen {
        public LoginScreen() {
            super(MainScreen.NO_VERTICAL_SCROLL | MainScreen.NO_VERTICAL_SCROLLBAR);
            if (Trackball.isSupported()) {
                Trackball.setFilter(Trackball.FILTER_ACCELERATION);
                Trackball.setSensitivityX(20);
                Trackball.setSensitivityY(20);
            }
            add(...) // SOME COMPONENTS ARE ADDED HERE
        }
    }

It also returns "2147483647" via using Trackball.getSensitivityX() and Trackball.getSensitivityY() in navigationMovement.


Finally, based on some web searches, I moved the condition inside the navigationMovement as follows:

protected boolean navigationMovement(int dx, int dy, int status, int time) {
        if (Trackball.isSupported()) {
            Trackball.setFilter(Trackball.FILTER_ACCELERATION);
            Trackball.setSensitivityX(20);
            Trackball.setSensitivityY(20);
        }
        return super.navigationMovement(dx, dy, status, time);      
    }

the problem becomes that the navigation movement jumps from field at index 0 to index 2 to index 4 ... etc bypassing on field with each movement !


How to correctly set the sensitivity of trackball for a screen ?

Was it helpful?

Solution

I also can't understand this API design, in my experience these methods didn't work. As a workaround, you can call the following methods in Screen class:

Screen.setTrackballSensitivityXOffset
Screen.setTrackballSensitivityYOffset
Screen.setTrackballFilter

With the first two methods, you can add an offset passed as parameter to the current user sensitivity. Values are in the range [-100, 100], and the special value Integer.MAX_VALUE is also allowed to set the value as "unset". (Don't ask me what unset means).

Now comes the hacky part. Assuming 100 is the max value for sensitivity, if we'd like to set it to 100, we first need to obtain the difference between the desired value and the actual value. Then you set the sensitivity offset to the screen. For the sake of brevity, I'm not checking the value is in range:

int desiredSensitivityX = 100;  //your value here
int offsetX = desiredSensitivityX  - Trackball.getSensitivityXForSystem(); // make sure offset is not out of range
this.setTrackballSensitivityXOffset(offsetX); //here "this" is your screen

You could put that code inside the screen constructor. It only works for that screen. Once you leave that screen, the sensitivity (actually the offset) goes back to normal.

Finally, the last method, also in screen class, allows you to set a trackball filter only for that screen. So you can put this line in your constructor:

this.setTrackballFilter(Trackball.FILTER_ACCELERATION);

and it should be back to normal after you leave your screen.

Hope it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top