Jump to content

Recommended Posts

Posted (edited)

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
  • Moderators
Posted (edited)

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

Posted

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
  • Moderators
Posted

XxXGoD,

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

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

  Reveal hidden contents

 

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
  • Recently Browsing   0 members

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