Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $+F13:: {
- ; toggle macro recording with the specified name
- MacroRecorder.Toggle("M1.ahk")
- }
- F2:: {
- Run "M1.ahk"
- }
- class MacroRecorder {
- static __New() {
- ih := this.KeyHook := InputHook("V")
- ih.KeyOpt("{All}", "N")
- ih.DownKeys := Map()
- ih.TimeSincePriorKey := 0
- ih.OnKeyDown := ObjBindMethod(this, "OnKeyDown")
- ih.OnKeyUp := ObjBindMethod(this, "OnKeyUp")
- }
- static Toggle(FileName?) {
- HotKeyWait() ; wait for hotkey to release
- ih := this.KeyHook
- IsSet(FileName) && this.FileName := FileName ; if filename variable exist, create this.FileName property
- if ih.InProgress {
- ih.Stop()
- this.Save()
- ToolTip "Recording stopped"
- SetTimer ToolTip, -1500
- } else {
- ih.TimeSincePriorKey := 0
- this.TempStore := "#Requires AutoHotkey v2.0`n"
- ih.Start()
- ToolTip "Recording started"
- }
- return ih.InProgress ; returns true if inputhook is started
- }
- static Save(*) {
- saveFile := this.FileName
- if this.KeyHook.InProgress
- this.Toggle()
- fileObj := FileOpen(saveFile, "w`n")
- fileObj.Write(this.TrimOutput())
- fileObj.Close()
- }
- static TrimOutput() {
- if A_ThisHotkey {
- temp := this.TempStore
- line := StrLen(temp)
- loop parse A_ThisHotkey {
- switch A_LoopField {
- ; find the position of the first hotkey prefix pressed
- case "^": line := Min(InStr(temp, "Control Down",, -1), line)
- case "!": line := Min(InStr(temp, "Alt Down",, -1), line)
- case "+": line := Min(InStr(temp, "Shift Down",, -1), line)
- case "#": line := Min(InStr(temp, "Win Down",, -1), line)
- }
- }
- temp := SubStr(temp, 1, line)
- temp := RegExReplace(temp, "(Sleep\(\d+\)\R)?Send.*\Z") ; remove the last sleep and the last found Send prefix
- }
- return temp
- }
- static OnKeyDown(ih, VK, SC) {
- elapsedTime := A_TickCount - (ih.TimeSincePriorKey || A_TickCount)
- ih.TimeSincePriorKey := A_TickCount
- key := GetKeyName(Format("vk{:x}sc{:x}", VK, SC))
- if ih.DownKeys.Get(key, "") ; prevent auto-repeat
- return
- ih.DownKeys.Set(key, 1)
- Text := ""
- if elapsedTime
- Text .= "Sleep(" elapsedTime ")`n"
- Text .= 'Send("{' key ' Down}")`n'
- ToolTip Text
- this.TempStore .= Text
- }
- static OnKeyUp(ih, VK, SC) {
- elapsedTime := A_TickCount - (ih.TimeSincePriorKey || A_TickCount)
- ih.TimeSincePriorKey := A_TickCount
- key := GetKeyName(Format("vk{:x}sc{:x}", VK, SC))
- ih.DownKeys.Has(key) && ih.DownKeys.Delete(key)
- Text := ""
- if elapsedTime
- Text .= "Sleep(" elapsedTime ")`n"
- Text .= 'Send("{' key ' Up}")`n'
- this.TempStore .= Text
- }
- }
- ; wait for all keys in the hotkey to release
- HotKeyWait() {
- if !A_ThisHotkey
- return
- RegExMatch(A_ThisHotkey, "i)(?:([$~*<>!^#+]+)|(?:(\S+) +& +))?(\S+)", &M)
- keys := []
- if M[1] { ; get prefix symbols
- index := 1
- loop StrLen(M[1]) {
- str := SubStr(M[1], index++, 1)
- LR := ""
- (str = "<") && LR := "L"
- (str = ">") && LR := "R"
- switch LR ? SubStr(M[1], index++, 1) : str {
- case "!": keys.Push(LR "Alt")
- case "^": keys.Push(LR "Ctrl")
- case "+": keys.Push(LR "Shift")
- case "#": keys.Push((LR||"L") "Win")
- }
- }
- } else if M[2] { ; get custom prefix if any
- keys.Push(M[2])
- }
- keys.Push(M[3]) ; get the rest of the hotkey
- for k in keys {
- KeyWait k
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement