Advertisement
S1L1R

timers

Jan 24th, 2023 (edited)
845
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; DONT RUN THIS FILE JUST USE: #include Lsleep.ahk IN ANOTHER SCRIPT TO USE THE FUNCTIONS
  2.  
  3. global Frequency
  4. SetBatchLines, -1
  5. Process, Priority,, A
  6. DllCall("QueryPerformanceFrequency", "Int64*", Frequency)  
  7.  
  8. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  9. ;;               Lumi                  ;;
  10. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  11.  
  12. lSleep(s_time, ByRef start = "") {
  13.     Critical
  14.     DllCall("QueryPerformanceCounter", "Int64*", CounterBefore)
  15.     if (start != "")
  16.         CounterBefore := start
  17.     Frequency ? Frequency : DllCall("QueryPerformanceFrequency", "Int64*", Frequency)
  18.     if (s_time > 20) {
  19.         DllCall("QueryPerformanceCounter", "Int64*", CounterAfter)
  20.         Critical Off
  21.         Sleep % s_time - (1000 * (CounterAfter - CounterBefore) / Frequency) - 20
  22.     }
  23.     Critical
  24.     End := (CounterBefore + ( Frequency * (s_time/1000))) - 1
  25.     loop
  26.     {
  27.         DllCall("QueryPerformanceCounter", "Int64*", CounterAfter)
  28.         if (End <= CounterAfter)
  29.             break
  30.     }
  31.     Critical Off
  32. }
  33.  
  34. ; s_time = time in milliseconds to sleep, can be input as a decimal, as accurate as frequency (10 MHz)
  35. ; start = optional parameter, should pass in a QPC timestamp as a variable, will sleep to s_time
  36. ;         after the the timestamp
  37. ;         e.g. lSleep(20, ext) followed by lSleep(40, ext) will only sleep a total 40 ms, not 60 ms
  38. ; the return statement returns the time actually slept, will slow down the sleep marginally
  39.  
  40.  
  41. MeasureTime(ByRef begin, ByRef end) {
  42.     Frequency ? Frequency : DllCall("QueryPerformanceFrequency", "Int64*", Frequency)
  43.     return Round(( 1000 * (end - begin) / Frequency), 3)
  44. }
  45.  
  46. ; begin/end = pass in two QPC timestamps as variables, returns a rounded difference, mostly a helper func
  47.  
  48. HoldKey(keyName, duration, wait := 0) {
  49.     SendInput {%keyName% down}
  50.     lSleep(duration)
  51.     SendInput {%keyName% up}
  52.     lSleep(wait)
  53.     return
  54. }
  55.  
  56. ; keyName = should be a key name or vk code that AHK recognizes
  57. ; duration = how long in milliseconds the key should be held
  58. ; wait = optional parameter, will sleep after the key release (useful for archwing movement)
  59.  
  60. TimedKeyLoop(keyName, timeBetweenInputs, endTime, ByRef start := "") {
  61.     start ? start : DllCall("QueryPerformanceCounter", "Int64*", start)
  62.     Frequency ? Frequency : DllCall("QueryPerformanceFrequency", "Int64*", Frequency)
  63.     tkl_end := (start + ( Frequency * (endTime/1000))) - 1
  64.     loop {
  65.         SendInput {%keyName%}
  66.         DllCall("QueryPerformanceCounter", "Int64*", tkl_sleep)
  67.         tkl_sleep_end := (tkl_sleep + ( Frequency * (timeBetweenInputs/1000))) - 1
  68.         loop {
  69.             DllCall("QueryPerformanceCounter", "Int64*", tkl_sleep_time)
  70.             if (tkl_end < tkl_sleep_time)
  71.                 break 2
  72.             (tkl_sleep_end < tkl_sleep_time) ? break : lSleep(0.3, tkl_sleep_time)
  73.         }
  74.         until (tkl_sleep_end < tkl_sleep_time)
  75.         DllCall("QueryPerformanceCounter", "Int64*", tkl_time)
  76.     }
  77.     until (tkl_end < tkl_time)
  78.     return
  79. }
  80.  
  81. ; presses a key at desired interval for desired duration
  82. ;
  83. ; keyName = should be a key name or vk code that AHK recognizes
  84. ; timeBetweenInputs = time between the key presses
  85. ; endTime = how long in milliseconds the macro will loop for
  86. ; start = see the start variable in lsleep
  87.  
  88. /*
  89. F1::
  90. DllCall("QueryPerformanceCounter", "Int64*", Before)
  91.  
  92. ;insert code here
  93.  
  94. DllCall("QueryPerformanceCounter", "Int64*", After)
  95. MsgBox % 1000 * (After - Before) / Frequency
  96. return
  97. */
  98.  
  99. ; used to check duration of a macro, leave it commented out just copy paste it somewhere else
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement