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
Developer
Posts: 196
Registered: ‎12-14-2009
My Device: Not Specified
Accepted Solution

ButtonField that works for normal click and also performs a different action with a key down and click

[ Edited ]

I have a bunch of buttons on my main screen. They currently do one thing when clicked. I'd like to make them do a different thing when ALT-clicked. I constructed them like:

 

 

ButtonField mycoolbuttonname = new ButtonField("Awesome",ButtonField.CONSUME_CLICK);

 

 

Then I assign each one a ChangeListener which then directs that specific listener to specific code.

 

How can I direct an ALT-click to a different FieldChangeListener code?

 

I'm think somehow with: KeyDown API

 

Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: ButtonField that works for normal click and also performs a different action with a key down and click

Yes, you right. You only have to override the keyDown method for your buttons

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

Re: ButtonField that works for normal click and also performs a different action with a key down and click

If you want to react differently to ALT-click, you don't need keyDown.  Override navigationClick and analyze status parameter to see if STATUS_ALT bit is set. Like this:

 

protected boolean navigationClick(int status, int time) {
  if ((status & KeypadListener.STATUS_ALT) != 0) {
    // do your ALT-click processing here
  } else {
    fieldChangeNotify(FieldChangeListener.PROGRAMMATIC);
  }
  return true;
}

 I'm using FieldChangeListener.PROGRAMMATIC as a substitute for ButtonField.NEVER_DIRTY. You might do it differently - setDirty(false); before return true; is also a good way to do it.

 

 

Likewise, STATUS_SHIFT will check whether you SHIFT-clicked. Take a look in KeypadListener for the rest of the flags.

----------------------------------------------------------
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: 196
Registered: ‎12-14-2009
My Device: Not Specified

Re: ButtonField that works for normal click and also performs a different action with a key down and click

[ Edited ]

I like the override way.

 

But, I'm trying that and getting "The method fieldChangeNotify(int) in the type Field is not applicable for the arguments(long)" error

 

Here's how I have one of my buttons and change listener. I have mutltiple buttons and listeners so I want to do it for each button.

 

That code you give does this right?: overrides the click and executes alt-custom-code on alt-click, if no alt-click it is telling button it is clean and unaffected and should continue with its regular ways. But I'm getting that error code.

 

 

	ButtonField mycoolbuttonname = new ButtonField("Awesome",ButtonField.CONSUME_CLICK)
        {
        	      	protected boolean navigationClick(int status, int time) {
        		
        		  if ((status & KeypadListener.STATUS_ALT) != 0) {
        			  //code to execute for mycoolbutton on alt-click
        		  } else {
        			  fieldChangeNotify(ButtonField.NEVER_DIRTY);
        		  }
        		  return true;  
        		}
        };
        mycoolbuttonname.setChangeListener(sungo);
	FieldChangeListener sungo = new FieldChangeListener()
   	 {
   	 public void fieldChanged(Field field, int context)
    	{
   	//code to execute for mycoolbutton on normal click

     	};

 

 

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

Re: ButtonField that works for normal click and also performs a different action with a key down and click

No, no, no - don't pass NEVER_DIRTY to fieldChangeNotify - it is normally used in ButtonField constructor.

 

fieldChangeNotify accepts int, and FieldChangeListener.PROGRAMMATIC is exactly that. And that's what I've written in my piece of code. Why did you decide to replace it?

----------------------------------------------------------
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: 196
Registered: ‎12-14-2009
My Device: Not Specified

Re: ButtonField that works for normal click and also performs a different action with a key down and click

[ Edited ]

I admit to having brain-fry from hours of work right now.Smiley Happy 

But I was getting an error wih Programmatic too.

 

Looks like I lose the enter-key way of activating a button too. I think with KeyDown it will still work with Touch,Enter, and Navigation click.

 

Must research.

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

Re: ButtonField that works for normal click and also performs a different action with a key down and click

No way you could get that error with PROGRAMMATIC - replace the parameter and compile again.

 

As for losing any other ways of activating a button - overriding navigationClick will not affect any other default processing but the click itself. What exactly are you trying to achieve?

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

Re: ButtonField that works for normal click and also performs a different action with a key down and click

[ Edited ]

I have multiple buttons on a screen. Right now all work with: 1. Trackpad-click 2. Touch-click 3. Enter-click

 

I want each button to be able to do two things:

 

1. One thing with an ALT-activate(by any of the three ways as seen above)

2. Another thing with normal activate(by any of the three ways as seen above)

 

Overriding Navigation click will not allow for ALT-Enter and Alt-Touch(The way I want it is that these two do #1 from above too) though. Correct or no?

 

In the simplest terms possible:

 

Each button will do one specific thing on activate and another specific different thing on ALT-activate.

 

Right now, they are working absolutely fine doing one thing. But I am trying to find a way to also make them do something else on ALT-activate.

 

I figure if I can somehow make a way to get the true/false of if ALT is pressed during a field change and then do an if/then based on that information in each fieldChanged method then I ought to be good.

 

Update: Well, I got the errors to go away with your above code. At least Alt-Trackpad press is working. I'm going with it. Thanks!

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

Re: ButtonField that works for normal click and also performs a different action with a key down and click

By touch, do you mean just touch or click? Click (or touch on Torch), if unprocessed, translates into navigationClick, so you've got that.

 

For Alt-Enter, override keyChar and check the status parameter there as well.  If it's not Enter, simply return super.keyChar(ch, status, time).

 

If you want TAP on Storm to work like click, that will be much more difficult. Start by overriding keyDown and keyUp and create a state "tracker" which constantly updates the status of Alt.

----------------------------------------------------------
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: 196
Registered: ‎12-14-2009
My Device: Not Specified

Re: ButtonField that works for normal click and also performs a different action with a key down and click

[ Edited ]

I gave up. Alt-trackpad press was working but it was also firing the regular trackpad event too. So I had two things happening with the ALT-press. 

 

My regular code works with a click in Storm and touch in Torch which is the behavior I like.

 

This code below(the one from this forum thread) will not give you a useable alt-click and it will not work with touch in a Torch.

 

 

ButtonField mycoolbuttonname = new ButtonField("Awesome",ButtonField.CONSUME_CLICK)
        {
        	      	protected boolean navigationClick(int status, int time) {
        		
        		  if ((status & KeypadListener.STATUS_ALT) != 0) {
        			  //code to execute for mycoolbutton on alt-click
        		  } else {
        			  fieldChangeNotify(FieldChangeListener.PROGRAMMATIC);
        		  }
        		  setDirty(false);
        		  return true;  
        		}
        };
        mycoolbuttonname.setChangeListener(sungo);
	FieldChangeListener sungo = new FieldChangeListener()
   	 {
   	 public void fieldChanged(Field field, int context)
    	{
   	//code to execute for mycoolbutton on normal click

     	};