Results 1 to 6 of 6

Thread: Trouble running the button hold script.

  1. #1
    Join Date
    Apr 2007
    Posts
    18

    Default Trouble running the button hold script.

    I'm trying to use the button hold script below, and keep getting errors. The error is ":2: attempt to call global 'clock' (a nil value)". Any help would be appreciated.

    Also, I was wondering is KeyDown and KeyLastEvent tied to the specific button that triggers the event?

    Thanks.

    Code:
    -- initialize variables 
    t = clock()
    KeyDown = KeyDown or t 
    KeyLastEvent = KeyLastEvent or KeyDown
    
    print("t "..t)
    print("KeyDown "..KeyDown)
    print("KeyLastEvent "..KeyLastEvent)
     
    -- if the last press event happened more than .5 before, assume new press 
    if t - KeyLastEvent > 0.5 then 
      KeyDown = t 
    end 
    -- save current time as last press event 
    KeyLastEvent = t 
    -- if we had a new press event before now
    if t - KeyDown > 0 then 
      -- send a FFW 
      TriggerEvent ("FFW",18) 
    else 
      -- send a next 
      TriggerEvent ("next",18) 
    end

  2. #2
    Join Date
    Jul 2005
    Location
    Uithuizen, The Netherlands
    Posts
    576

    Default

    hi,

    first of all have a look in the 'variable inspector'

    You'll find that the clock function can be called with 'os.clock', same thing with 'TriggerEvent' should be 'gir.TriggerEvent'

    try this (added additional feedback):
    Code:
    -- initialize variables 
    t = os.clock()
    KeyDown = KeyDown or t 
    KeyLastEvent = KeyLastEvent or KeyDown
    
    print("t "..t)
    print("KeyDown "..KeyDown)
    print("KeyLastEvent "..KeyLastEvent)
     
    -- if the last press event happened more than .5 before, assume new press 
    local diff = t - KeyLastEvent print('diff= '..diff)
    
    if diff > 0.5 then KeyDown = t  print('assuming new press') end 
    
    -- save current time as last press event 
    KeyLastEvent = t 
    
    -- if we had a new press event before now
    local diff2 = t - KeyDown print('diff2= '..diff2)
    
    if diff2 > 0 then 
      -- send a FFW 
      gir.TriggerEvent ("FFW",18) print('ffw event sent')
    else 
      -- send a next 
      gir.TriggerEvent ("next",18) print('next event sent')
    end
    Last edited by blubberhoofd; March 26th, 2008 at 09:07 AM.

  3. #3
    Join Date
    Apr 2007
    Posts
    18

    Default

    Thanks for the help! It got the script working.

    Now that it's working, I'm having trouble with it working correctly. I've tried triggering it off of key up and key down events, but the script always triggered before I actually let go of the button. It's almost as if I'm getting a fluttering signal from my remote control. I've gone into the event settings for the IR trigger and adjusted the anti repeat with no improvement as well as gone into event mapping and adjust the repeat frequency and other settings with no improvement.

    Not sure what to do. We I look at the logger, I only see a record of the button down transition.

  4. #4
    Join Date
    Jul 2005
    Location
    Uithuizen, The Netherlands
    Posts
    576

    Default

    hi,

    Did a litte rewrite, for testing purposes I've used an interval of 0.5 seconds. You might want to tweak this for better performance.
    BTW: I trigger it with 'raw' <down> event(s) with 'timeout' at 200 ms.

    Code:
    local Press = os.clock()
    if Lastpress == nil then Lastpress = Press end
    local Diff = Press-Lastpress 
    print('Press= '..Press..'; Lastpress= '..Lastpress..'; Diff= '..Diff)
    
    if Diff == 0 or Diff > 0.5 then print('first press in some time...')
    --set timer to trigger 'next' event after 0.5 seconds
    t = gir.CreateTimer("", "gir.TriggerEvent ('NEXT',18) print('...next event sent')", "print('triggering forward...')", false, true) 
    t:Arm(500)
    end
    
    if Diff > 0 and Diff < 0.5 then print('repeated press...')
    --cancel timer and trigger 'forward' event
    t:Cancel()
    gir.TriggerEvent ("FFWD",18) print('...ffw event sent')
    end
    
    Lastpress = Press
    Last edited by blubberhoofd; March 28th, 2008 at 10:19 PM.

  5. #5
    Join Date
    Apr 2007
    Posts
    18

    Default

    Thanks for the help and the revised script. I'm trying to wrap my head around it still. Is the intent of this script to detect the difference between short and long key presses? This would be pretty embarrassing if it wasn't!

    The script triggers immediately after I press the button down. It doesn't matter if I hold the button down for any duration because the script triggers as soon as I press the button down.

    Now if I press the button a multiple times, I can get the different events to trigger.

  6. #6
    Join Date
    Jul 2005
    Location
    Uithuizen, The Netherlands
    Posts
    576

    Default

    hi,

    the script doesn't detect long keypresses.
    When it detects single keypresses it executes the next event, when it detects repeated keypresses in 0.5 seconds it triggers the forward event.

    in my setup I use a serial Igorplug device, only capable of generating <down> events.
    When holding down a key the same <down> event gets generated lots of times.

    What event behaviour does your remote generate?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •