Hyflex Posted March 5, 2012 Posted March 5, 2012 (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 March 5, 2012 by XxXGoD
Moderators Melba23 Posted March 5, 2012 Moderators Posted March 5, 2012 (edited) XxXGoD,Perhaps like this? #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) WEndNote that it is advised to open/close the DLL if you use _IsPressed in a loop. M23Edit: Removed extraneaous characters from the code - see below. Edited March 5, 2012 by Melba23 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 ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Andreik Posted March 5, 2012 Posted March 5, 2012 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
Hyflex Posted March 5, 2012 Author Posted March 5, 2012 Awesome guys, they both work as I need Note: Fixed Melba's script: ConsoleWrite("Second press - Duration: " & StringFormat("%.1f", $nTotal_Time) & " secs"& @CRLF)
Moderators Melba23 Posted March 5, 2012 Moderators Posted March 5, 2012 XxXGoD,Actually it should read $nTime - the additional "1111`" was the result of the final test run! M23 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 ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Hyflex Posted March 5, 2012 Author Posted March 5, 2012 M23, Thanks yours worked fantastic and easy to incorporate into my scripts
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now