1
votes

I am working on ListView section, in this, the user can search the content by name and directly move at the first element of List via pressing a keyboard button. Like, if you press button B from (right vertical manager) it will scroll the list and move focus to first record of B.

enter image description here

The code is working fine in simulator but it's not working on Touch device - I have BB 9380 Curve.

here is the code for :

LabelField a = new LabelField("A" , FOCUSABLE)
{
    protected void paint(Graphics graphics) 
    {
        graphics.setColor(0xC4C4C4);
        super.paint(graphics);
    }

    protected boolean navigationClick(int status, int time) 
    {
        //fieldChangeNotify(1);
        injectKey(Characters.LATIN_CAPITAL_LETTER_A);
        injectKey(Characters.LATIN_CAPITAL_LETTER_A);
        return true;
    }
};

private void injectKey(char key) 
{
    try 
    {
        searchList.setFocus();
        KeyEvent inject = new KeyEvent(KeyEvent.KEY_DOWN, key, 0);
        inject.post();
        /*inject.post();*/

    } catch (Exception e) {
        Log.d("In injectKey :: :: :: "+e.toString());
        MessageScreen.msgDialog("In Inject Key "+e.toString());
    }

}
1
I cropped your screenshot because you have included too much whitespace, and other area that just makes it harder to read your question.Nate
hmm k..do you have any Idea about the questionAK Joshi

1 Answers

1
votes

Alternate Solution

I would recommend a different strategy for this. Instead of trying to simulate key press events, I would define one method that handles a keypress of a certain letter, or a touch click on that same letter's LabelField.

Source: blackberry.com

So, you can have code that handles key presses by using

protected boolean keyChar( char character, int status, int time ) 
{
    // you might only want to do this for the FIRST letter entered,
    //   but it sounds like you already have the keypress handling
    //   the way you want it ...
    if( CharacterUtilities.isLetter(character) )
    {
        selectLetter(character);
        return true;
    }

    return super.keyChar( character, status, time );
}

and then also handle touch events:

LabelField a = new LabelField("A" , FOCUSABLE)
{
    protected void paint(Graphics graphics) 
    {
        graphics.setColor(0xC4C4C4);
        super.paint(graphics);
    }

    protected boolean navigationClick(int status, int time) 
    {
        char letter = getText().charAt(0);
        selectLetter(letter);
        return true;
    }
};

then, simply define a method that takes in one character, and scrolls to the start of that part of the list:

private void selectLetter(char letter);

Key Injection

If you really, really want to simulate key presses, though, you might try changing the code so that it injects two events: key down, and then key up (you're currently injecting two key down events). This might be causing problems.

    injectKey(Characters.LATIN_CAPITAL_LETTER_A, true);
    injectKey(Characters.LATIN_CAPITAL_LETTER_A, false);

with

private void injectKey(char key, boolean down) 
{
    try 
    {
        searchList.setFocus();
        int event = down ? KeyEvent.KEY_DOWN : KeyEvent.KEY_UP;
        KeyEvent inject = new KeyEvent(event, key, 0);
        inject.post();
    } catch (Exception e) { /** code removed for clarity **/
    }
}

Additional Note

For UIs, I like to trigger events on the key up, or unclick events. I think this makes a better experience for the user. So, you could replace keyChar() with keyUp() and navigationClick() with navigationUnclick() if you want to do this.