Jump to content

Hotkey problem with a simple timer


Recommended Posts

I wrote a timer which is activated by an hotkey and when it reaches 10 second it will play a sound.

The problem is that if I press "x" times the hotkey then the timer will play the sound "x" times.

I cannot find a way to reset the timer using the hotkey, without playing the sounds many times.

For example:

- I press "f" to let it start

- Timer is started, and I want to reset it when reaches 5 seconds so I press again "f"

- Then, I press "f" after 3 seconds 

- I wait the end of the script, but instead of playing the sound only once, it will be played three times. :(

I tried to put HotKeySet("{f}" ) inside the count function to fix it but doing so, the timer will not reset.

 
HotKeySet("{f}", "Count")

Idle()

Func Idle()
   While True
      ToolTip("Seconds Count = " & "OFF", 0, 0, "", 1)    
      Sleep(100)
   WEnd
EndFunc

Func Count()
   
      $Sec = 10
      Local $Count = 0, $begin = TimerInit()
     
      While $Sec > $Count
          $dif = TimerDiff($begin)
          $dif2 = StringLeft($dif, StringInStr($dif, ".") -1)
          $Count = int($dif/1000)
          ToolTip("Seconds Count = " & $Count, 0, 0, "", 1)   
          Sleep(20)   
      WEnd
      
      SoundPlay(@WindowsDir & "\Media\Ding.wav", 1) 

   Return
EndFunc

 

Link to comment
Share on other sites

Basically what you want to do is to register a one-time timer on your hotkey press, which after the period plays the sound. When the timer is running, and hotkey is pressed again, you want to unregister the previous timer and register a new one.

Link to comment
Share on other sites

HotKeySet("{f}", "Count")

Idle()

Func Idle()
   While True
      ToolTip("Seconds Count = " & "OFF", 0, 0, "", 1)    
      Sleep(100)
        HotKeySet("{f}", "Count")
   WEnd
EndFunc

Func Count()
      HotKeySet("{f}")
      $Sec = 3
      Local $Count = 0, $begin = TimerInit()
     
      While $Sec > $Count
          $dif = TimerDiff($begin)
          $dif2 = StringLeft($dif, StringInStr($dif, ".") -1)
          $Count = int($dif/1000)
          ToolTip("Seconds Count = " & $Count, 0, 0, "", 1)   
          Sleep(20)   
      WEnd
      
      SoundPlay(@WindowsDir & "\Media\Ding.wav", 1) 

   Return
EndFunc

this works for me

Link to comment
Share on other sites

Basically what you want to do is to register a one-time timer on your hotkey press, which after the period plays the sound. When the timer is running, and hotkey is pressed again, you want to unregister the previous timer and register a new one.

 

Yes :)

I've tried also this but doesnt work:

   HotKeySet("{f}")
   Send("{f}")
   HotKeySet("{f}", "Count")
 
When the timer reach the end It will play the sound "x" times, where "x" = n° of "f" hotkey pressed
HotKeySet("{f}", "Count")

Idle()

Func Idle()
   While True
      ToolTip("Seconds Count = " & "OFF", 0, 0, "", 1)    
      Sleep(100)
   WEnd
EndFunc

Func Count()
      HotKeySet("{f}")
      Send("{f}")
      HotKeySet("{f}", "Count")
      $Sec = 10
      Local $Count = 0, $begin = TimerInit()
     
      While $Sec > $Count
          $dif = TimerDiff($begin)
          $dif2 = StringLeft($dif, StringInStr($dif, ".") -1)
          $Count = int($dif/1000)
          ToolTip("Seconds Count = " & $Count, 0, 0, "", 1)   
          Sleep(20)   
     
     WEnd
      
      SoundPlay(@WindowsDir & "\Media\Ding.wav", 1) 

   Return
EndFunc
Edited by Newscritpz
Link to comment
Share on other sites

Hi,

Take a look at my "Advanced HotKeySet" link in my signature (under My Snippets), and play with the flags.

You should get what you expect :)

Edit: Note that the example is a little bit "complicated", you can strip the half of the code if you don't need a GUI/disable hotkeys outside your GUI.

Br, FireFox.

Edited by FireFox
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...