Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
tweethumbs
Posts: 41
Registered: ‎04-24-2012
My Device: storm
Accepted Solution

KeyDown Event Problem

Hi I have a problem using the keyDown event I would want to get the values of the virtual keyboard using the keyDown method in my custom field but the screen is taking up the event when I tried to overide the keyDown in the screen and return false It still does not go through the keyDown my CustomField. Is there anyway to do this? Thanks in advance

Retired
PBernhardt
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30

Re: KeyDown Event Problem

The key methods all bubble up starting from the field that has focus, going through it's parent managers until it reaches one that consumes the event (returns true). You don't have to return true to use the event, just if it doesn't make sense for any parent of the field to handle it.

 

Anyway, I would expect the problem is that your field didn't have focus, or the listener is not registered.

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Contributor
tweethumbs
Posts: 41
Registered: ‎04-24-2012
My Device: storm

Re: KeyDown Event Problem

hi below is a example of the code that I used it still does not go to the keyChar in the field what am I doing wrong tnx for the advice public final class MyScreen extends MainScreen { public MyScreen() { Field ff = new MyFields(); add(ff); } protected boolean keyChar(char character, int status, int time) { System.out.println("KeyChar In Screen"); return(false); } public class MyFields extends Field{ private int ttwidth; private int hhheight; private Graphics g; public MyFields() { } protected void layout(int width, int height) { ttwidth = Display.getWidth(); hhheight = Display.getHeight(); setExtent(ttwidth, hhheight); } protected void paint(Graphics graphics) { } protected boolean keyChar(char character, int status, int time) { System.out.println("KeyChar In Field"); return(true); } } }
Contributor
tweethumbs
Posts: 41
Registered: ‎04-24-2012
My Device: storm

Re: KeyDown Event Problem

sori about that there no indentation

 

sori about that there seem to be not indentation

 

public final class MyScreen extends MainScreen
{
    public MyScreen()
    {        
        Field ff = new MyFields();
        add(ff);
    }
    
    protected boolean keyChar(char character, int status, int time) {
        System.out.println("KeyChar In Screen");
        return(false);
    }
    
    public class MyFields extends Field{
        private int ttwidth;
        private int hhheight;
        private Graphics g;
        
        public MyFields() {
            
        }
        
        protected void layout(int width, int height) {
            ttwidth = Display.getWidth();
            hhheight = Display.getHeight();
            setExtent(ttwidth, hhheight);
        }

        protected void paint(Graphics graphics) {
        }
        
        protected boolean keyChar(char character, int status, int time) {
            System.out.println("KeyChar In Field");
            return(true);
        }
    }
}

Developer
peter_strange
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: KeyDown Event Problem

[ Edited ]

I believe that this statement is not correct:

 

"The key methods all bubble up starting from the field that has focus.."

 

In fact I think the Screen gets the keyChar method first, and will pass it down as appropriate.  You usually do this with a super.keyChar(...);

 

So in your code you would do the following:

 

In screen:

 

    protected boolean keyChar(char character, int status, int time) {
        System.out.println("KeyChar In Screen");
        return super.keyChar(character, status, time);

    }

 

This should mean it gets to the Field in focus, which I presume to be your Field.  To make sure it is Focusable, you might like to do this:

 

        public MyFields() {
            super.(Field.FOCUSABLE);
        }

 

Let us know how you get on with this. 

Trusted Contributor
pigling
Posts: 104
Registered: ‎02-05-2012
My Device: Bold 9900 (primary) Z10 LE (secondary)

Re: KeyDown Event Problem

Peter's suggestion works as I just test it.

 

BTW, there is another way to set focusable on Field

    public MyScreen()
    {        
        Field ff = new MyFields();
        add(ff);
        
        getDelegate().setFieldWithFocus(ff);
    }

 so that you don't have to use

public MyFields() {
            super.(Field.FOCUSABLE);
        }

 Either can work.

Developer
peter_strange
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: KeyDown Event Problem

"Either can work."

 

Not sure I agree with this.  I don't think think the following is valid, certainly I can't see it in the JavaDocs.:

 

"getDelegate().setFieldWithFocus(ff);"

 

I would recommend the approach I outlined.   

Trusted Contributor
pigling
Posts: 104
Registered: ‎02-05-2012
My Device: Bold 9900 (primary) Z10 LE (secondary)

Re: KeyDown Event Problem

That expression comes from the sample "OpenGLSpriteGameDemo".