Jump to content

Recommended Posts

Posted (edited)

Hi everyone,

I would like to share a simple snippet where I use for my hotkey script.

I was searching for a solution whereby pressing a same key, I could have different outcome according to the way the key is being pressed.

1) Double tap/press fast (like double click the mouse) - outcome A

2) Long press or HOLD for awhile (2-4 sec) - outcome B

#Include <Timers.au3>

Global $interval_timer, $counter
HotKeySet("{F10}", "_terminate")



While 1
    Sleep(10)
WEnd



Func _terminate() 
    Local $itv_timer
    $itv_timer = _Timer_Diff($interval_timer)
    $interval_timer = _Timer_Init()
    ;ConsoleWrite($itv_timer & @CRLF) ;to review the timer difference so I can decide the range to capture double tap sequence
    If $itv_timer > 100 And $itv_timer < 250 Then
        $counter = 0
        $interval_timer = _Timer_Init()
        MsgBox("","","double tap detected")
    Elseif $counter >= 25 Then      
        $counter = 0
        $interval_timer = _Timer_Init()
        MsgBox("","","long press detected")
    ElseIf $itv_timer < 50 Then 
        $counter = $counter + 1
        ConsoleWrite($counter & @CRLF)
    Else
        $counter = 0
    EndIf
EndFunc

The trick here is to collect and use the frequency of the hotkey is called, and the time difference between each time the hotkey is being called.

My coding style is not efficient and can be such a mess most of the time, any suggestion or better coding are welcome.

I hope you find this one helpful.:)

Edited by guner7
Posted (edited)

Have you seen _Timer_GetIdleTime( )??

MM also I think you can set the sleep in your loop way higher since hotkey is an event..

Edited by Bilgus
Posted (edited)

I like doing away with the extra dependency on timer.au3 better

 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

HotKeySet("{F10}", "_terminate")

While 1
    Sleep(100000)
WEnd

Func _terminate()
    Local Static $itv_timer = 0
    Local Static $counter = 0
    Local $itv_diff = TimerInit() - $itv_timer
    ;ConsoleWrite($itv_diff & @CRLF) ;to review the timer difference so I can decide the range to capture double tap sequence

    If $counter >= 25 Then
        $counter = 0
        MsgBox("", "", "long press detected")
    ElseIf $itv_diff < 1000000 Then
        $counter += 1
        ConsoleWrite($counter & @CRLF)
    ElseIf $itv_diff < 5000000 Then
        $counter = 0
        MsgBox("", "", "double tap detected")
    Else
        $counter = 0
    EndIf
    $itv_timer = TimerInit()
EndFunc

 

Edited by Bilgus
??
Posted

Hi Bilgus,

I never aware of _Timer_GetIdleTime() before this..

Suppose they can be replace with TimerInit() and TimerDiff() without timer.au3.

However your code give me an error as following:

(8) : ==> No variable given for "Dim", "Local", "Global", "Struct" or "Const" statement.:
Local Static $itv_timer = 0
Local ^ ERROR

Any fix?

Posted

try this at the top of script 

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

 

I repasted the code but It runs fine on my PC

maybe try changing names or barring that remove the static keyword and paste the variable at the top of script as Global

  • 6 years later...
Posted
41 minutes ago, SharkyEXE said:

MsgBox("", "", "once press detected")

#Include <Timers.au3>

Global $interval_timer, $counter
HotKeySet("{F10}", "_terminate")
HotKeySet("{F11}", "_ExitScript")
Func _ExitScript()
    Exit
EndFunc

While 1
    Sleep(10)
    If $counter = 1 And _Timer_Diff($interval_timer) >= 550 Then
        MsgBox("","","single press detected")
        $counter = 0
        $interval_timer = _Timer_Init()
    EndIf
WEnd

Func _terminate()
    Local $itv_timer
    $itv_timer = _Timer_Diff($interval_timer)
    $interval_timer = _Timer_Init()
    ;ConsoleWrite($itv_timer & @CRLF) ;to review the timer difference so I can decide the range to capture double tap sequence
    If $itv_timer > 100 And $itv_timer < 250 Then
        MsgBox("","","double tap detected")
        $counter = 0
        $interval_timer = _Timer_Init()
    Elseif $counter >= 25 Then
        MsgBox("","","long press detected")
        $counter = 0
        $interval_timer = _Timer_Init()
    ElseIf $itv_timer < 50 Then
        $counter = $counter + 1
        ConsoleWrite($counter & @CRLF)
    Else
        $counter = 1
    EndIf
EndFunc

 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

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
×
×
  • Create New...