Jump to content
Sign in to follow this  
Hyflex

_KeyPressed Accurate Timer/Counter?

Recommended Posts

Hyflex

I'm trying to create a timer switch button using _KeyPressed

$Timer = 100
While 1
If _IsPressed(31) Then
Sleep(100)
If _IsPressed(31) Then
MsgBox("","Time",$Timer)
ExitLoop ;
EndIf
Sleep(100)
$Timer = $Timer + $Timer
EndIf
WEnd

I need it so When pressed it starts the timer, when pressed again it stops the timer...

EDIT: I've semi got something working:

#include <misc.au3>

$Timer = 100
While 1
    If _IsPressed("31") Then
        While _IsPressed("31")
            Sleep(100)
            $Timer = $Timer + 100
        WEnd
        MsgBox(0, "Timer", "" & $Timer)
        ExitLoop
EndIf
WEnd

However, I want it so its only when it's re-pressed and need it into in a number of 2decimal places and in seconds, not miliseconds.</misc.au3>

EDIT:

$Timer = 1
While 1
    If _IsPressed("31") Then
        While _IsPressed("31")
            Sleep(1)
            $Timer = $Timer + 1
        WEnd
        $TimerValue = $Timer / 1000
        MsgBox(0, "Timer", "" & $TimerValue)
    EndIf
WEnd]
Edited by XxXGoD

Share this post


Link to post
Share on other sites
Melba23

XxXGoD,

Perhaps like this? :oops:

#include <misc.au3>

Local $hDLL = DllOpen("user32.dll")

Global $fTimer_31 = False, $nTotal_Time

While 1
    ; If key in question was pressed
    If _IsPressed("31", $hDLL) Then
        Switch $fTimer_31
            ; First time - start timer and set flag
            Case False
                $iBegin = TimerInit()
                $fTimer_31 = True
                ConsoleWrite("First press" & @CRLF)
            ; Second time - add time to total and clear flag
            Case True
                $nTime = TimerDiff($iBegin) / 1000
                $nTotal_Time += $nTime
                $fTimer_31 = False
                ConsoleWrite("Second press - Duration: " & StringFormat("%.1f", $nTime) & " secs"& @CRLF)
        EndSwitch
    ElseIf _IsPressed("1B", $hDLL) Then
        MsgBox(4096, "_IsPressed", "The Esc Key was pressed, therefore we will close the application." & @CRLF & @CRLF & _
                "The key was pressed in total for " & StringFormat("%.1f", $nTotal_Time) & " secs")
        DllClose($hDLL)
        ExitLoop
    EndIf
    ; Wait until key is released or it will keep firing
    While _IsPressed("31", $hDLL)
        Sleep(10)
    WEnd
    ; In case nothing was pressed
    Sleep(10)
WEnd

Note that it is advised to open/close the DLL if you use _IsPressed in a loop. :bye:

M23

Edit: Removed extraneaous characters from the code - see below. :doh:

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Andreik

Something like this?

#include <misc.au3>
$Timer = 100
While 1
    If _IsPressed("31") Then
  ;Wait for release
  Do
   Sleep(10)
  Until Not _IsPressed(31)
        Do
            Sleep(100)
            $Timer = $Timer + 100
        Until _IsPressed("31")
        MsgBox(0, "Timer", "Time: " & Round($Timer/1000,2) & " s")
        ExitLoop
EndIf
WEnd

When the words fail... music speaks

Share this post


Link to post
Share on other sites
Hyflex

Awesome guys, they both work as I need

Note: Fixed Melba's script:

ConsoleWrite("Second press - Duration: " & StringFormat("%.1f", $nTotal_Time) & " secs"& @CRLF)

Share this post


Link to post
Share on other sites
Melba23

XxXGoD,

Actually it should read $nTime - the additional "1111`" was the result of the final test run! :oops:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Hyflex

M23,

Thanks yours worked fantastic and easy to incorporate into my scripts :oops:

Share this post


Link to post
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
Sign in to follow this  

×