P.I. Engineering User Forum

Product Support, Technical Questions and Answers, and Examples for P.I. Engineering Products

You are not logged in.

#1 2018-09-20 13:43:06

Mosent
Member
Registered: 2018-09-20
Posts: 1

Press/Hold Key Functionality

I've been trying to find a way to have a key have two different functions based on if the key was briefly pressed or if it was held for a delay. I came up with a solution that works, but it doesn't feel right. It's somewhat glitchy if double pressed and whatnot. Here's what I have:


Dim Test1 As Boolean = False
Dim nl As String = VbCrLf
   
    If State 'Pressed [MWTAG10]
        Test1 = True
        MW3.insertDelay(300)
        If Test1 = True
            MW3.sendUnicodeString("Held" & nl)
        Else
            MW3.sendUnicodeString("Pressed" & nl)
        End If
    Else 'Released [MWTAG11]
        Test1 = False   
    End If 'Press-Release [/MWTAG10/MWTAG11]


Is there a better way of doing this? Is there a "kill" function that stops a key without you having to add variable checks into the code? While writing this I thought of a simple way of preventing the key from running again until it completes a cycle, but I'm still not convinced that it's best. This is the second version that feels less buggy, but not quite smooth:


    Dim Test2 As Boolean = False
    Dim Test1 As Boolean = False
    Dim nl As String = VbCrLf
   
        If State 'Pressed [MWTAG10]
            If Test2 = False
                Test1 = True
                Test2 = True
                MW3.insertDelay(300)
                If Test1 = True
                    MW3.sendUnicodeString("Held" & nl)
                Else
                    MW3.sendUnicodeString("Pressed" & nl)
                End If
                Test2 = False
            End If
        Else 'Released [MWTAG11]
            Test1 = False   
        End If 'Press-Release [/MWTAG10/MWTAG11]


If anyone has any advice on the subject, I'd love to hear from you. Otherwise, maybe this can help someone who's as new to programming as I am. Thanks!

Offline

#2 2018-11-13 12:15:46

PIE Liam
Technical Support Specialist
From: Williamston, MI
Registered: 2016-01-21
Posts: 165
Website

Re: Press/Hold Key Functionality

I really appreciate you sharing this with our users, looks good.

Offline

Board footer

Powered by FluxBB