显示按键.ahk 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ; Config area
  3. ; Font settings
  4. fontSize = 20
  5. fontName = Verdana
  6. fontStyle = Bold
  7. ; The max number of keys that are listed on the screen
  8. numButtons := 5
  9. ; Gui transparecy
  10. transparent := true
  11. ; Distance from the buttons to the edge of the window
  12. winMargin := 25
  13. ; When this is true, old keys are cleared after the speed timer interval
  14. useSpeedTimer := true
  15. speedTimer := 1000
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17. ; Font metrics functions
  18. FontLen(text)
  19. {
  20. global
  21. return StrLen(text) * fontSize + 25
  22. }
  23. FontHeight()
  24. {
  25. global
  26. return (fontSize * 3)
  27. }
  28. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  29. ; Gui Setup
  30. Gui +ToolWindow +AlwaysOnTop
  31. Gui Font, s%fontSize% %fontStyle%, %fontName%
  32. ; Create the buttons with 0 width
  33. Loop % numButtons
  34. {
  35. height := FontHeight()
  36. if (A_Index == 1)
  37. Gui Add, Button, w0 h%height% X%winMargin% ym
  38. else
  39. Gui Add, Button, w0 h%height% ym
  40. }
  41. ; Show the gui with a default 200 width
  42. Gui Show, w200, Screenkey.ahk
  43. ; Save the window id
  44. WinGet k_ID, ID, A
  45. If (transparent)
  46. {
  47. TransColor = F1ECED
  48. Gui Color, %TransColor%
  49. WinSet TransColor, %TransColor% 220, ahk_id %k_ID%
  50. }
  51. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  52. ; Hotkeys
  53. CaptureKeyboardInputs()
  54. {
  55. global
  56. static keys
  57. keys=Space,Enter,Tab,Esc,BackSpace,Del,Ins,Home,End,PgDn,PgUp,Up,Down,Left,Right,CtrlBreak,ScrollLock,PrintScreen,CapsLock
  58. ,Pause,AppsKey,NumLock,Numpad0,Numpad1,Numpad2,Numpad3,Numpad4,Numpad5,Numpad6,Numpad7,Numpad8,Numpad9,NumpadDot
  59. ,NumpadDiv,NumpadMult,NumpadAdd,NumpadSub,NumpadEnter,NumpadIns,NumpadEnd,NumpadDown,NumpadPgDn,NumpadLeft,NumpadClear
  60. ,NumpadRight,NumpadHome,NumpadUp,NumpadPgUp,NumpadDel,Media_Next,Media_Play_Pause,Media_Prev,Media_Stop,Volume_Down,Volume_Up
  61. ,Volume_Mute,Browser_Back,Browser_Favorites,Browser_Home,Browser_Refresh,Browser_Search,Browser_Stop,Launch_App1,Launch_App2
  62. ,Launch_Mail,Launch_Media,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21,F22
  63. ,1,2,3,4,5,6,7,8,9,0,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
  64. ,[,],',=,\,/
  65. Loop Parse, keys, `,
  66. Hotkey, ~*%A_LoopField%, KeyHandleLabel, UseErrorLevel
  67. return
  68. KeyHandleLabel:
  69. KeyHandle()
  70. return
  71. }
  72. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  73. ; Hotkey handles
  74. current := 1
  75. clear := false
  76. KeyHandle()
  77. {
  78. global
  79. keyname := RegExReplace(A_ThisHotKey, "~\*", "")
  80. dispkey := Up(keyname)
  81. mods := ModifierNames(keyname)
  82. dispkey := mods . dispkey
  83. len := FontLen(dispkey)
  84. ; resize the button with what needs to be displayed
  85. GuiControl Move, Button%current%, w%len%
  86. ControlSetText Button%current%, %dispkey%, ahk_id %k_ID%
  87. if (current == 1 && clear)
  88. {
  89. ; Clear the rest of buttons - resize them to 0 width
  90. Loop % numButtons - 1
  91. {
  92. curr := A_Index + 1
  93. GuiControl Move, Button%curr%, w0
  94. }
  95. }
  96. ; move to the next button
  97. current++
  98. if (current > numButtons)
  99. {
  100. current := 1
  101. clear := true
  102. }
  103. else
  104. {
  105. ; reposition the remaining buttons
  106. Loop
  107. {
  108. if (A_Index < current)
  109. continue
  110. prev := A_Index - 1
  111. GuiControlGet Button%prev%, Pos
  112. newX := % Button%prev%X + Button%prev%W
  113. GuiControl Move, Button%A_Index%, X%newX%
  114. if (A_Index >= numButtons)
  115. break
  116. }
  117. }
  118. ; calculate the new total width of the window
  119. winWidth := 0
  120. Loop % numButtons
  121. {
  122. GuiControlGet Button%A_Index%, Pos
  123. w := Button%A_Index%W
  124. winWidth := w + winWidth
  125. }
  126. WinMove ahk_id %k_ID%,,,, winWidth + 2 * winMargin
  127. ; install the speed timer that clears all the buttons when the next key is hit
  128. if (useSpeedTimer)
  129. {
  130. SetTimer HandleSpeedType, %speedTimer%
  131. }
  132. }
  133. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  134. ; Helper functions
  135. IsUpperCase(key)
  136. {
  137. caps := GetKeyState("CapsLock", "T")
  138. shift := GetKeyState("Shift", "P")
  139. if (caps && !shift || !caps && shift)
  140. {
  141. return true
  142. }
  143. return false
  144. }
  145. ModifierNames(key)
  146. {
  147. mods := ""
  148. shift := GetKeyState("Shift", "P")
  149. ctrl := GetKeyState("Ctrl", "P")
  150. alt := GetKeyState("Alt", "P")
  151. win := GetKeyState("LWin", "P")
  152. special := "[,],',=,\,/,;,``,."
  153. if (StrLen(key) == 1)
  154. {
  155. if (key >= "0" && key <="9")
  156. shift := false
  157. if (key >= "a" && key <="z")
  158. shift := false
  159. if (key >= "A" && key <="Z")
  160. shift := false
  161. if (Instr(special, key))
  162. shift := false
  163. }
  164. if (shift)
  165. mods := mods . "Shift+"
  166. if (ctrl)
  167. mods := mods . "Ctrl+"
  168. if (alt)
  169. mods := mods . "Alt+"
  170. if (win)
  171. mods := mods . "Win+"
  172. return (mods)
  173. }
  174. Up(inkey)
  175. {
  176. outkey := ""
  177. if (IsUpperCase(inkey) && StrLen(inkey) == 1 && inkey >= "a" && inkey <= "z")
  178. {
  179. StringUpper outkey, inkey
  180. return outkey
  181. }
  182. shift := GetKeyState("Shift", "P")
  183. if (shift)
  184. {
  185. if (inkey == "1")
  186. return "!"
  187. if (inkey == "2")
  188. return "@"
  189. if (inkey == "3")
  190. return "#"
  191. if (inkey == "4")
  192. return "$"
  193. if (inkey == "5")
  194. return "%"
  195. if (inkey == "6")
  196. return "^"
  197. if (inkey == "7")
  198. return "&&"
  199. if (inkey == "8")
  200. return "*"
  201. if (inkey == "9")
  202. return "("
  203. if (inkey == "0")
  204. return ")"
  205. if (inkey == "-")
  206. return "_"
  207. if (inkey == "=")
  208. return "+"
  209. if (inkey == "[")
  210. return "{"
  211. if (inkey == "]")
  212. return "}"
  213. if (inkey == "\")
  214. return "|"
  215. if (inkey == ",")
  216. return "<"
  217. if (inkey == ".")
  218. return ">"
  219. if (inkey == "/")
  220. return "?"
  221. if (inkey == "``")
  222. return "~"
  223. if (inkey == ";")
  224. return ":"
  225. if (inkey == "'")
  226. {
  227. quote = "
  228. return quote
  229. }
  230. }
  231. return inkey
  232. }
  233. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  234. ; Install hotkeys
  235. CaptureKeyboardInputs()
  236. ; special handling for special characters
  237. ~*;::
  238. ~*,::
  239. ~*.::
  240. ~*`::
  241. KeyHandle()
  242. return
  243. HandleSpeedType:
  244. current := 1
  245. clear := true
  246. SetTimer HandleSpeedType, Off
  247. return
  248. GuiClose:
  249. ExitApp