/AutoHotkey.ahk

https://github.com/marvinrodas/AutoHotkey-Scripts · AutoHotKey · 354 lines · 227 code · 50 blank · 77 comment · 32 complexity · fb7300f1e02b50b38ea1d97b882022cc MD5 · raw file

  1. ;;; # = Win
  2. ;;; ^ = Ctrl
  3. ;;; ! = Alt
  4. ;;; + = Shift
  5. ;;;;;;;;;; Setup ;;;;;;;;;;
  6. ;#Warn All
  7. #SingleInstance force
  8. #NoEnv
  9. #WinActivateForce
  10. #Include %A_ScriptDir% ; Sets the directory for future includes
  11. SetWorkingDir %A_ScriptDir%
  12. SendMode Input
  13. ListLines Off
  14. SetTitleMatchMode 2 ; Match anywhere in title
  15. WinGet explorerPid, PID, ahk_class Shell_TrayWnd
  16. GroupAdd explorer, ahk_pid %explorerPid%
  17. AutoReload()
  18. ;;;;;;;;;; Constants ;;;;;;;;;;
  19. WM_USER = 0x0400
  20. WM_WA_IPC = %WM_USER%
  21. IPC_GET_REPEAT = 251
  22. IPC_SET_REPEAT = 253
  23. IPC_STARTPLAY = 102
  24. ;;;;;;;;;; Lib Includes ;;;;;;;;;; (must come first for auto-execute sections to run)
  25. #Include lib\Clipboard.ahk
  26. #include lib\ClipboardData.ahk
  27. #Include extern\Functions.ahk
  28. ;;;;;;;;;; Hotkey includes ;;;;;;;;;;
  29. #Include WinSwitch.ahk
  30. ;;;;;;;;;; Functions ;;;;;;;;;;
  31. isHotkeyCombo(hotkey, withinTime = 500) {
  32. return ((A_PriorHotKey = hotkey) && (withinTime <= 0 || A_TimeSincePriorHotkey < withinTime))
  33. }
  34. handleHotkeyCombo(hotkey, sendForCombo = "", sendForDefault = "", withinTime = 500) {
  35. if (isHotkeyCombo(hotkey, withinTime)) {
  36. if (sendForCombo != "") {
  37. Send % sendForCombo
  38. }
  39. return True
  40. } else {
  41. if (sendForDefault != "") {
  42. Send % sendForDefault
  43. }
  44. return False
  45. }
  46. }
  47. getHotkeyRepeatCount(withinTime = 0) {
  48. static repeatCount = {}
  49. thisHotKey := A_ThisHotKey
  50. if ((A_PriorHotKey = thisHotKey) && (withinTime <= 0 || A_TimeSincePriorHotkey < withinTime)) {
  51. repeatCount[thisHotKey]++
  52. } else {
  53. repeatCount[thisHotKey] := 0
  54. }
  55. return repeatCount[thisHotKey]
  56. }
  57. ;;;;;;;;;; Hotkeys ;;;;;;;;;;
  58. ;;; Send home dir
  59. ;;; Sending any other way than SendPlay causes it to send on keyup rather than keydown
  60. #\::
  61. count := getHotkeyRepeatCount(1000)
  62. if (count = 0)
  63. SendPlay c:\users\russell\
  64. else if (count = 1)
  65. SendPlay dropbox\
  66. else if (count = 2)
  67. SendPlay progs\
  68. else if (count = 3)
  69. SendPlay ahk\
  70. return
  71. ;;; Minimize active window
  72. #Escape::WinMinimize,A
  73. ;;; Firefox
  74. #IfWinActive, ahk_class MozillaWindowClass
  75. ~^f::return
  76. ;;; Clear and dismiss the search box on ctrl+f + ctrl+d
  77. ^d::handleHotkeyCombo("~^f", "{Delete}{Esc}", "^d")
  78. #IfWinActive
  79. ;;; Auto-reloads the script when saved in an editor with ctrl+s
  80. #IfWinActive, AutoHotkey.ahk
  81. ~^s::
  82. #IfWinActive, WinSwitch.ahk
  83. ~^s::
  84. AutoReload("Off")
  85. ToolTip, Reloading...
  86. Sleep 500
  87. Reload
  88. ; Reload failed
  89. AutoReload()
  90. ToolTip
  91. return
  92. #IfWinActive
  93. ;;; AHK hotkeys
  94. #+s::ListHotkeys
  95. #+a::Edit
  96. ;;; IntelliJ/PhpStorm fixes
  97. #IfWinActive, JetBrains ahk_class SunAwtFrame
  98. ^f::Send ^f^a
  99. ;;; Dismiss the search box on ctrl+f + ctrl+d
  100. ;;; Bug in AHK causes A_PriorHotkey to be "~^f" due to its usage elsewhere in the script
  101. ;;; (even though this version of hotkey still behaves as if the ~ was missing)
  102. ^d::handleHotkeyCombo("~^f", "{Delete}{Esc}", "^d")
  103. #IfWinActive
  104. ;;; SciTE has issues with some numpad keys
  105. #IfWinActive, ahk_class SciTEWindow
  106. NumpadAdd::+
  107. NumpadSub::-
  108. #IfWinActive
  109. ;;; Always-On-Top toggle
  110. #z::WinSet, AlwaysOnTop, Toggle, A
  111. ;;; Paste snippets
  112. #v::
  113. ToolTip Clipboard Mode...
  114. Input, Key, L1,{Esc}{Enter}
  115. ToolTip
  116. ClipboardSave()
  117. if (Key = "s") {
  118. html=
  119. (LTrim
  120. <span style="font-size: 10pt; font-family: 'Courier New',monospace; padding: 0 1px; color: #444444;
  121. background: #F8F8FF; border: 1px solid #DEDEDE;">x</span>
  122. )
  123. ClipboardSetHtml(html)
  124. Send ^v
  125. Send {Backspace}
  126. } else if (Key = "d") {
  127. html=
  128. (LTrim
  129. <div></div><div style="font-size: 10pt; font-family: 'Courier New',monospace; padding: 0 0.2em; color: #444444;
  130. background: #F8F8FF; border: 1px solid #DEDEDE;">
  131. <br></div><div></div>
  132. )
  133. ClipboardSetHtml(html)
  134. Send ^v
  135. Send {Up}
  136. } else if (Key = "e") {
  137. ClipboardSetHtml("<span>x</span>")
  138. Send ^v
  139. Send {Backspace}
  140. } else {
  141. if (!InStr(ErrorLevel, "EndKey"))
  142. SoundBeep
  143. return
  144. }
  145. Sleep 500
  146. ClipboardRestore()
  147. return
  148. ;;;
  149. $^q::Send !{F4}
  150. ;;; Fix the ridiculously broken play/pause button behavior caused by intellitype
  151. ;;; (set the button to disabled in the intellitype settings). Must use a keyboard
  152. ;;; hook here to intercept it from itype.exe. We can then send through the exact
  153. ;;; same keystroke and everything works how it should.
  154. $Media_Play_Pause::
  155. ; Make sure winamp is runing
  156. if (!WinExist("ahk_class BaseWindow_RootWnd")) {
  157. Run winamp.exe
  158. WinWaitActive ahk_class BaseWindow_RootWnd
  159. }
  160. SendInput {Media_Play_Pause}
  161. return
  162. ;;; Run winamp, load media library, set to local media, focus search box
  163. #w::
  164. Run winamp.exe
  165. DetectHiddenWindows on ;For ml_pmp_window
  166. WinWait ahk_class ml_pmp_window ;Top level window that should reflect when the media library is ready
  167. WinWaitActive ahk_class BaseWindow_RootWnd ;main window
  168. ControlFocus SysTreeView321
  169. SendPlay l ;Don't SendInput/SendEvent, it triggers the win+l lockscreen
  170. Sleep 50
  171. Send {Tab}
  172. return
  173. ;;; Winamp: toggle repeat with status tooltip
  174. #IfWinExist ahk_class Winamp v1.x
  175. ^!Ins::
  176. SendMessage WM_WA_IPC, 0, IPC_GET_REPEAT
  177. if (ErrorLevel != "FAIL") {
  178. newVal := 1 - ErrorLevel ; Toggle between 1 and 0
  179. SendMessage WM_WA_IPC, newVal, IPC_SET_REPEAT
  180. ToolTip2("Repeat: " . (newVal ? "ON" : "OFF"), 1000)
  181. }
  182. return
  183. #IfWinExist
  184. ;;; Winamp: start playing from the beginning of the playlist. Strangely, this is not exposed
  185. ;;; in the Winamp UI (as far as I can tell). This is slightly different than the exposed
  186. ;;; "Start of list" function which moves to the start of the list but doesn't start playing.
  187. #IfWinExist ahk_class ahk_class Winamp v1.x
  188. ^!Home::PostMessage WM_WA_IPC, 0, IPC_STARTPLAY
  189. #IfWinExist
  190. ;;; winamp hotkeys
  191. #IfWinActive, ahk_class BaseWindow_RootWnd
  192. ;;; Override this script's default minimize behavior. Sending a minimize message to the active winamp
  193. ;;; window (ahk_class BaseWindow_RootWnd) causes weird behavior -- the winamp window only partially
  194. ;;; minimizes, and restoring it causes this other proxy window to initially gain the focus, which causes
  195. ;;; problems for this script. Minimizing the proxy window instead makes everything work normally.
  196. #Escape::WinMinimize ahk_class Winamp v1.x
  197. ^e::ControlFocus Winamp Playlist Editor
  198. ^m::ControlFocus SysTreeView321 ;Media library
  199. #IfWinActive
  200. ;;; Make RWin by itself act as AppsKey
  201. ~RWin Up::
  202. ; AHK takes care of bypassing the start menu
  203. if (A_PriorKey = "RWin")
  204. Send {AppsKey}
  205. return
  206. ;RWin & Browser_Refresh::return
  207. ;RWin::AppsKey
  208. ;;; Remap browser buttons to mouse buttons
  209. Browser_Back::LButton
  210. Browser_Forward::RButton
  211. ;;; mintty
  212. #IfWinActive ahk_class mintty
  213. ;;; Replace mintty's buggy intra-app window switching
  214. ^+Tab::WinSwitch(-1)
  215. ^Tab::WinSwitch(1)
  216. ;;; mintty sends the same control sequence for space and shift+space
  217. ;;; so this mapping has to be set externally
  218. +Space::Send {Esc}
  219. #IfWinActive
  220. ;;; Show the start menu
  221. #+r::Send ^{Esc} ; Doing it with the WinKey gets messy
  222. ;;; Show the run dialog
  223. ;#^+r::Send {RWin down}r{RWin up}
  224. ;;; Restore focus to the previously focused window (rather than the taskbar window)
  225. ;;; when pressing Esc in the run dialog or start menu
  226. #IfWinActive Run ahk_class #32770 ahk_group explorer
  227. ~Esc:: ;Fallthrough
  228. #IfWinActive Start menu ahk_class DV2ControlHost
  229. ~Esc::Send !{Esc}
  230. #IfWinActive
  231. ;;; Explorer
  232. #IfWinActive ahk_class CabinetWClass
  233. ;;; Copy item to shortcuts folder
  234. ^s::
  235. fileName := GetSelectionViaClipboard()
  236. SplitPath fileName, , , , nameNoExt
  237. shortcutPath := EnvGet("USERPROFILE") . "\dropbox\progs\shortcuts\" . nameNoExt . ".lnk"
  238. FileCreateShortcut % fileName, % shortcutPath
  239. Run explorer /select`,%shortcutPath%
  240. return
  241. ;;; Edit
  242. ^e::Send {AppsKey}e
  243. ;;; Sometimes explorer windows don't respond to Ctrl+W. They always respond to Alt+F4.
  244. ^w::Send !{F4}
  245. ;;;
  246. ^+Tab::WinSwitch(-1)
  247. ^Tab::WinSwitch(1)
  248. #IfWinActive
  249. ;;; Remote Desktop
  250. #IfWinActive ahk_class TscShellContainerClass
  251. ; Almost all keys are swallowed by remote desktop's keyboard hook which gets installed
  252. ; on top of AHK's. CapsLock is an exception, which makes it the perfect key to bind to.
  253. ;
  254. ; Be careful if an Alt or Win modifier here. The modifier's keyup event may trigger a
  255. ; system action. AHK is supposed to work around this but it doesn't seem to work in this
  256. ; case. See http://www.autohotkey.com/forum/topic22378.html for a related discussion.
  257. ^+CapsLock::
  258. ; Need a short sleep here for focus to restore properly.
  259. Sleep 50
  260. WinMinimize
  261. return
  262. #IfWinActive
  263. ;;; Notepad2
  264. #IfWinActive ahk_class Notepad2U
  265. ;;; Copy line if nothing selected
  266. ^c::
  267. StatusBarGetText status
  268. if (InStr(status, "Sel Ln 0")) {
  269. Send ^+c
  270. return
  271. }
  272. send ^c
  273. return
  274. ;;; Cut line if nothing selected
  275. ^x::
  276. StatusBarGetText status
  277. if (InStr(status, "Sel Ln 0")) {
  278. Send ^+x
  279. return
  280. }
  281. send ^x
  282. return
  283. ^d::Send ^+d
  284. ^w::Send !{F4}
  285. ^+w::Send ^w
  286. ^/::Send ^q
  287. #IfWinActive
  288. ;;; Convert clipboard to plain text
  289. #c::Clipboard := Clipboard
  290. ;;; Close system tray balloon notification
  291. #+b::SendMessage 0x41c, , , , ahk_class tooltips_class32 ;TTM_POP=0x41c
  292. ;;; Fix weird behavior with right-alt key in X-Windows
  293. #IfWinActive ahk_class cygwin/x X rl
  294. RAlt::LAlt
  295. #IfWinActive
  296. ;;;;;;;;;; Hotstrings ;;;;;;;;;;
  297. :*:dp\::c:\users\russell\dropbox\progs\
  298. ;;;;;;;;;; Testing ;;;;;;;;;;
  299. F13::
  300. MsgBox % A_PriorKey
  301. return
  302. F14::
  303. ClipboardSetHtml("<b>test</b>")
  304. return