显示按键.ahk 6.5 KB

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