Button gets triggered three times?

Hi!

I’m developing an UR cap and I have a few buttons which when I press them it should increment an integer by 1. I’ve managed to do this and the integer is incrementing but when i press the button it triggers three times causing the integer to go up by three instead of one.

Code for one of the buttons:

@Input
(id = addMagazine)
public void AddMagazineButtonPressed(InputEvent event){
if (event.getEventType() == InputEvent.EventType.ON_PRESSED);{

	System.out.println("ADD_MAGAZINE");
	Mag = Mag+1;
	System.out.println(Mag);
	}
}

Try to press the button a while (e.g. 5seconds) and tell me if the Button triggers as well 3times or even more…
If it triggers more time the longer you press it, then you will ned to implement a deley in your Function so that your function normally runs longer than you press the button. Otherwise the program maybe running a few times in that time you press the button once.

For example you press the button 3seconds and the program just need 1 second to process and than it starts from the beginning wich means it runs 3times while you press the button once.

Hope you understand what i mean.

I tried your method and now I see that the button only triggers once when pressed down, then twice when I let go of it? But I have no code in my code which references ON_RELEASE so I’m not sure why it triggers twice upon release.

Does anyone have any idea why this is happening? I’ve looked through the code for hours and I see no reason why it would trigger once when I press the button then twice when I let go of it. Doesn’t make sense at all to me

You may try something as followed for your button:

@Input(id = "your_button")
public void buttonClicked(InputEvent event) {
	if(event.getEventType() == InputEvent.EventType.ON_CHANGE) {
		// Some Code which should be executed
                my_example_function();
	}
}

As well as i understand why “ON_PRESSED” seems to be more understandful for Buttons… this method with “ON_CHANGE” worked for me and got no issues with it.

1 Like

I did the change you suggested and it didn’t really change anything, it still triggers once when i press it down and the twice after I let it go.

	@Input
	(id = addMagazine)
public void AddMagazineButtonPressed(InputEvent event){ 
if (event.getEventType() == InputEvent.EventType.ON_CHANGE);{
	
	System.out.println("ADD_MAGAZINE");
	Mag = Mag+1;
	System.out.println(Mag);
	}
}

Is there an option for you to test you’re Program on a real bot or another VM?
Just to make sure the problem is based on you’re code and not maybe a software problem/missconfiguration of you’re URSim.

I don’t know what was wrong but I just copy pasted your code and changed the ID and events to match what I’ve already set up and now it works? I guess there must be some typo in the code I posted which made it act up, I have no idea what kind of typo though? Nevertheless the issue is gone and I’m a happy man again! Thanks!

I am no java expert, but did you notice the semicolon? If this is not syntax error, then it would fire on all events.

Sharp eyes!
I guess you are right on this one,