PageRenderTime 22ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/macro.ahk

http://github.com/Skiouros/Macro
AutoHotKey | 245 lines | 191 code | 47 blank | 7 comment | 41 complexity | 3bbf6b271d3af078291b8c8d1b31f1ca MD5 | raw file
  1. #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
  2. #Persistent
  3. #SingleInstance, Force
  4. ; #NoTrayIcon
  5. SetBatchLines, -1
  6. ListLines, Off
  7. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory
  8. global xml, currentXml, version, debug, AhkScript, AhkRecorder, Ini
  9. version := 0.9
  10. ProcessCommandLine()
  11. debug := 1
  12. if (!FileExist(A_ScriptDir . "\res"))
  13. Install()
  14. if (Ini.UpdateOnStart)
  15. Update()
  16. currentXml := A_ScriptDir . "\res\Profiles\Default.xml"
  17. xml := new Xml(currentXml)
  18. xml.Save(currentXml)
  19. Ini := new Ini(A_ScriptDir . "\res\settings.ini")
  20. ahkDll := A_ScriptDir . "\res\dll\AutoHotkey.dll"
  21. AhkRecorder := AhkDllThread(ahkDll)
  22. AhkScript := AhkDllThread(ahkDll)
  23. OnMessage(0x404, "AHK_NOTIFYICON") ; Detect clicks on tray icon
  24. OnMessage(0x4a, "RecieveData") ; For the recorder script
  25. PID := DllCall("GetCurrentProcessId")
  26. gui := new Main()
  27. if (Ini.Settings.ShowOnStart)
  28. gui.Show()
  29. DllCall( "RegisterShellHookWindow", UInt, gui.hwnd )
  30. MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
  31. OnMessage( MsgNum, "WindowActivated" )
  32. return
  33. Pressed:
  34. hotkey := Trim(RegExReplace(A_ThisHotkey, "([\$\*\<\>\~\#\!\^\+]|(?<!_)Up)"))
  35. debug ? debug(hotkey . " pressed")
  36. ; get all the info for the hotkey
  37. type := xml.Get("key", hotkey, "type")
  38. value := xml.Get("key", hotkey, "value")
  39. repeat := xml.Get("key", hotkey, "repeat")
  40. if (type = "textblock")
  41. delay := xml.Get("textblock", value, "delay")
  42. else if (type = "script")
  43. AhkScript.ahkFunction("OnEvent", hotkey, "Pressed", A_TimeSinceThisHotkey)
  44. if (repeat = "toggle")
  45. {
  46. toggle := !toggle
  47. if (!toggle)
  48. return
  49. while (toggle)
  50. {
  51. sleep, 10
  52. HandleKey(type, value, delay)
  53. }
  54. toggle := false
  55. return
  56. }
  57. else if (type != "script")
  58. HandleKey(type, value, delay)
  59. if (type != "script" && repeat = "None")
  60. KeyWait % Hotkey
  61. else if (type = "script")
  62. {
  63. While (GetKeyState(hotkey, "P") )
  64. AhkScript.ahkFunction("OnEvent", hotkey, "Down", A_TimeSinceThisHotkey)
  65. AhkScript.ahkFunction("OnEvent", hotkey, "Released", A_TimeSinceThisHotkey)
  66. }
  67. Return
  68. WindowActivated( wParam,lParam ) {
  69. global gui, PID
  70. ; Check to make sure that profile switching is on
  71. ; , the current window is not the script , and that the message was for a window being activated.
  72. static messageList := "4,32772"
  73. if wParam not in %messageList%
  74. return
  75. else if (!Ini.Settings.ProfileSwitching || WinActive("ahk_pid " . PID))
  76. return
  77. WinGet, proccessExe, ProcessPath, % "ahk_id " lParam
  78. debug ? debug(proccessExe . " activated.")
  79. Loop % A_ScriptDir . "\res\Profiles\*.xml"
  80. {
  81. if (A_LoopFileName = "Default.xml")
  82. Continue
  83. FileRead, text, % A_LoopFileLongPath
  84. RegExMatch(text, "`am)\<exe\>(.*)?\<", exe)
  85. StringSplit, exe, exe1, <
  86. if (proccessExe = exe1)
  87. {
  88. Control, ChooseString, % SubStr(A_LoopFileName, 1, -4),, % "ahk_id " . gui.drpProfiles.hwnd
  89. switchedProfile := 1
  90. break
  91. }
  92. }
  93. if (currentXml != A_ScriptDir . "\res\Profiles\Default.xml" && !switchedProfile)
  94. Control, ChooseString, Default,, % "ahk_id " . gui.drpProfiles.hwnd
  95. }
  96. Hotkeys(disable = 0) {
  97. debug ? debug("Turning " . (disable ? "off" : "on") . " hotkeys")
  98. keys := xml.List("keys", "|")
  99. Loop, Parse, keys, |
  100. if (A_LoopField)
  101. {
  102. ; Turn (on|off) the key
  103. options := xml.GetAttribute(A_LoopField)
  104. repeat := xml.Get("key", hotkey, "repeat")
  105. Hotkey, % "$" . options, % (disable ? "Off" : "Pressed"), % (disable ? "Off" : "On T" . ((repeat = "toggle") + 1))
  106. }
  107. }
  108. HandleKey(type, value, delay = -1) {
  109. text := xml.Get(type, value, "value")
  110. if (!text)
  111. return
  112. else if (type = "macro")
  113. PlayMacro(value)
  114. else if (type = "textblock")
  115. {
  116. StringReplace, text, text, ``n, `n, all
  117. SetKeyDelay % delay
  118. SendRaw % text
  119. SetKeyDelay, -1
  120. }
  121. }
  122. GetProfiles() {
  123. Loop, % A_ScriptDir . "\res\Profiles\*.xml"
  124. profiles .= A_LoopFileName . "|"
  125. return profiles
  126. }
  127. AHK_NOTIFYICON(wParam, lParam) {
  128. global gui
  129. if lParam = 0x201 ; WM_LBUTTONUP
  130. return
  131. else if lParam = 0x203 ; WM_LBUTTONDBLCLK
  132. gui.Show()
  133. }
  134. Install() {
  135. debug ? debug("Installing files")
  136. ; Small delay so the updater can exit
  137. Sleep, 1000
  138. FileCreateDir, % A_ScriptDir . "\res"
  139. FileCreateDir, % A_ScriptDir . "\lib"
  140. FileCreateDir, % A_ScriptDir . "\res\ahk"
  141. FileCreateDir, % A_ScriptDir . "\res\dll"
  142. FileCreateDir, % A_ScriptDir . "\res\profiles"
  143. FileInstall, lib\PlayMacro.ahk, lib\PlayMacro.ahk, 1
  144. FileInstall, res\dll\AutoHotkey.dll, res\dll\AutoHotkey.dll, 1
  145. FileInstall, res\dll\SciLexer.dll, res\dll\SciLexer.dll, 1
  146. FileInstall, res\ahk\updater.exe, res\ahk\updater.exe, 1
  147. FileInstall, res\ahk\Recorder.ahk, res\ahk\Recorder.ahk, 1
  148. }
  149. Update() {
  150. if (!A_IsCompiled)
  151. return
  152. UrlDownloadToFile, http://www.autohotkey.net/~zzzooo11/Macro/version.txt, % A_ScriptDir . "\v.txt"
  153. FileRead, ver, % A_ScriptDir . "\v.txt"
  154. FileDelete, % A_ScriptDir . "\v.txt"
  155. ; We have the latest verion
  156. if (version >= ver) {
  157. MsgBox, 64,, You have the latest verison.
  158. return
  159. }
  160. MsgBox, 4, Update, % "Installed Version: " . version . "`nCurrent Version: " . ver . "`n`nWould you like to update?"
  161. IfMsgBox, No
  162. return
  163. Run % A_ScriptDir . "\res\ahk\updater.exe"
  164. Exitapp
  165. }
  166. ProcessCommandLine() {
  167. global debugFile
  168. Loop, % (arg := {0: %false%}) [0]
  169. {
  170. if (%A_Index% == "/install")
  171. Install()
  172. else if (%A_Index% = "/debug")
  173. debug := 1, var := A_Index + 1, debugFile := %var%
  174. }
  175. }
  176. RecieveData(wParam, lParam)
  177. {
  178. global gui
  179. StringAddress := NumGet(lParam + 2*A_PtrSize) ; Retrieves the CopyDataStruct's lpData member.
  180. msg := StrGet(StringAddress) ; Copy the string out of the structure.
  181. StringSplit, msg, msg, `n
  182. Loop % msg0 - 1
  183. gui.Macro.currentMacro.Items.Add("", msg%A_Index%) ; Add the key to listview.
  184. Rows := gui.Macro.currentMacro.Items.Count
  185. gui.Macro.currentMacro.Items.Modify(rows, "vis") ; Makes sure the added key is visible.
  186. return true ; Returning 1 (true) is the traditional way to acknowledge this message.
  187. }
  188. #include <CGUI>
  189. #include <Xml>
  190. #include <ini>
  191. #include <Debug>
  192. #Include, %A_ScriptDir%\res\ahk\macro_recorder.ahk
  193. #Include, %A_ScriptDir%\res\ahk\profile_settings.ahk
  194. #Include, %A_ScriptDir%\res\ahk\textBlock.ahk
  195. #Include, %A_ScriptDir%\res\ahk\settings.ahk
  196. #Include, %A_ScriptDir%\res\ahk\main.ahk
  197. #Include, %A_ScriptDir%\res\ahk\settings.ahk
  198. #Include, %A_ScriptDir%\res\ahk\windows.ahk