Click and Hold button in Perspective

Hi,
I would like to use a “click and hold” button to prevent accidentally triggering critical stuff. Such as deleting a entry from a database. I am not using small popups for confirmation but I dont like those since they add more complexity to the interface.

Below is a example i found:
http://sonsoleslp.github.io/react-click-n-hold/

Thank you
Jakub

2 Likes

I would use a normal button with scripts placed in the onMouseDown, onMouseUp, and onMouseLeave Events, in conjunction with a custom property: Button.custom.clickReleased

onMouseDown:

self.custom.clickReleased = False

# Untested in live environment - might require slight tweaks
def confirm_if_click_held():
    from time import sleep
    sleep(1)
    if not self.custom.clickReleased:
        # logic here

system.util.invokeAsynchronous(confirm_if_click_held) # note no parens

onMouseLeave AND onMouseUp:

self.custom.clickReleased = True
2 Likes

For the animation, you could even achieve this as well by setting the style class to the animated version which has a duration of your hold time, and set it back to the non animated when not held

Hi I am trying to implement this and am running into trouble. I’m attempting to use a button to actuate a valve that closes a very large tank lid, which is why I’d like the click and hold function. The idea is that an operator can’t just tap the screen to close the lid and walk away, or worse yet, try to snag something out of the tank while it’s closing and accidentally smash an arm. We want them to have to hold the button in to stay near the HMI while it closes. Here’s what I have:

self.custom.clickReleased = False

Untested in live environment - might require slight tweaks

def confirm_if_click_held():
from time import sleep
sleep(1)
if not self.custom.clickReleased:
valve.close_output = 1
valve.open_output = 0
else:
valve.close_output = 0
valve.open_output = 0

system.util.invokeAsynchronous(confirm_if_click_held). # note no parens

And then I set the clickReleased custom property = True on the mouse up and mose leave events. I feel like I am missing something here because it’s not working. The valve tags i’m attempting to write are not being written. If I remove the logic and just simply try to write the valve tags on the mouse down and up events it works like this:
Mouse down event()
valve.close_output = 1
valve.open_output = 0

Mouse up event()
valve.close_output = 0
valve.open_output = 0

Do you have any advice?

There’s no good answer at this time. What you are looking for is a Momentary button, like real buttons typically used for “jog” type operations. Perspective doesn’t have one, largely because there’s currently no good way to ensure the “button” is released reliably. Particular in the case of broken comms.

There’s a pretty good chance your Gateway logs will have errors associated with your script because there was an unintended . after the final paren. I’ve removed it from my “example”. Try removing it from yours, and then then run it again. If it fails again, then we need more information about what specific line is failing, or what behavior is not occurring which you think should be.

Here’s my script, verbatim:

def runAction(self, event):
self.custom.clickReleased = False

# Untested in live environment - might require slight tweaks
def confirm_if_click_held():
    from time import sleep
    sleep(1)
    if not self.custom.clickReleased:
    	self.custom.Pos0_Bit = 1
    	self.custom.Pos1_Bit = 0
	else:
        self.custom.Pos0_Bit = 0
	self.custom.Pos1_Bit = 0
system.util.invokeAsynchronous(confirm_if_click_held) # note no parens

I got rid of the period. Still no luck, and it’s giving me an error on my else statement. I use the exact same syntax for if/then statements in numerous other scripts throughout this project, so I feel really dumb right now because I don’t understand why there’s an error on the else statement. Unfortunately, I have some other errors going on in my gateway right now, which is flooding my wrapper file, so I can’t see what the logs say when I attempt to run this.

Are you saying there is a red mark under the else statement, or that an exception is being thrown at the else statement?

You’re mixing your indentation method.

image

Indentation must always be the same, you have a mix of spaces and tabs.

3 Likes