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
Posts: 33
Registered: ‎06-16-2010
My Device: Blackberry 8520
My Carrier: Rogers
Accepted Solution

How to make Keychar response faster? or I should use other method?

Hello, I have the following keychar() code here:

 

protected boolean keyChar(char key, int status, int time) {

if (key == Characters.some key){
        	// do something;
            return true;          
        }   
if (key == Characters.some key){
        	// do something;
            return true;          
        }  
                  .
                  .
                  . assigned 20 different keys to do different things   
                  .
                  .
if (key == Characters.some key){
        	// do something;
            return true;          
        }  
    return super.keyChar(key, status, time);
    }  

I assigned 20 different keys to do different things, I guess it's because there are lots of if statement, it slows down the device. If I click the keys too fast, the device wont response that well. For example, I click a key for 5 times quickly, the device only detects 3 clicks. When I click different key for many times quickly, and the device detects all the keys I pressed, but it takes time for the device to finish the tasks that I assigned to the keys after the key pressing done. 

 

I tried to created threads for "//do something" instead of puting code in if statement in the keychar, but it still didn't work properly. I was wondering is there is any way to optimize it for this situation?

Highlighted
Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: How to make Keychar response faster? or I should use other method?

I'd say try a switch statement:

protected boolean keyChar(char key, int status, int time) {

    switch(key)
    {
        case Characters.some key:
            // do something
            break;
        case Characters.some key:
            // do something
            break;
            .
            .
            . assigned 20 different keys to do different things
            .
            .
        case Characters.some key:
            // do something
            break;
        default:
            return super.keyChar(key, status, time);
    }
    return true;
}

It will definitly make it easier to manage code and it might provide a small performance boost (never tested).

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to make Keychar response faster? or I should use other method?

[ Edited ]

What sort of processing are you doing on character input?  It should not have the impact you suggest, unless you are running your processing on the Event Thread.  I would profile your processing and make sure you are not doing anything unexpected or particularly resource intensive. 

 

Are you seeing this on a device or on the Simulator?

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: How to make Keychar response faster? or I should use other method?

To elaborate a little on what Peter has just said:

 

Check your // do something parts.  Unless the stuff you are doing there is really simple and fast, you'd better offload that processing into a separate Thread or, in case of UI interactions, invokeLater().

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: How to make Keychar response faster? or I should use other method?

As others just posted, you should definitely move any time-consuming operations out of the event thread. You say that you tried that; that it did not work properly may be an implementation issue.

 

I'd look at implementing a producer/consumer pattern. The keyChar method can produce notifications that go on an event queue (not the UI event queue!) and a background consumer thread can read the notifications and do the right thing for each one. (The notification could be as simple as an Integer that identifies the action to perform.) There are lots of examples on the web of how to implement the producer/consumer pattern. Although RIM's Java doesn't have a Queue class, it's relatively easy to implement.

 

Unfortunately, you can't move the key lookup into the background thread, because you need to know what value to return from keyChar(). Your series of "if" statements can be improved by chaining them together with else; otherwise every test gets evaluated even if the key matches the first test. As a series of if..else tests, it would be as efficient as a switch statement.

 

Here are a couple of alternatives that might speed up the lookup. They are both based on creating an action object (usually a Runnable) for each body of one of your if blocks:

  1. Turn the key into a Character and use it to look up an action object in a hash table. That has more-or-less constant access time.
  2. Create an array of the target key values, sorted in ascending order. Use Arrays.binarySearch to find the index of the key. Then index into an array of corresponding action objects.



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Contributor
Posts: 33
Registered: ‎06-16-2010
My Device: Blackberry 8520
My Carrier: Rogers

Re: How to make Keychar response faster? or I should use other method?

[ Edited ]

Thanks for all your guys reply. Actually the keychar responses and detects the keys fine, I think I got problems with the "do something" . I will try to modify it

 

 

 

I modified my "do something" code, it works better now. Thanks for all your guys Smiley Happy