KeyboardOnScreen.ahk 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. ; On-Screen Keyboard (requires XP/2k/NT) -- by Jon
  2. ; http://www.autohotkey.com
  3. ; This script creates a mock keyboard at the bottom of your screen that shows
  4. ; the keys you are pressing in real time. I made it to help me to learn to
  5. ; touch-type (to get used to not looking at the keyboard). The size of the
  6. ; on-screen keyboard can be customized at the top of the script. Also, you
  7. ; can double-click the tray icon to show or hide the keyboard.
  8. ;---- Configuration Section: Customize the size of the on-screen keyboard and
  9. ; other options here.
  10. ; Changing this font size will make the entire on-screen keyboard get
  11. ; larger or smaller:
  12. k_FontSize = 10
  13. k_FontName = Verdana ; This can be blank to use the system's default font.
  14. k_FontStyle = Bold ; Example of an alternative: Italic Underline
  15. ; Names for the tray menu items:
  16. k_MenuItemHide = Hide on-screen &keyboard
  17. k_MenuItemShow = Show on-screen &keyboard
  18. ; To have the keyboard appear on a monitor other than the primary, specify
  19. ; a number such as 2 for the following variable. Leave it blank to use
  20. ; the primary:
  21. k_Monitor =
  22. ;---- End of configuration section. Don't change anything below this point
  23. ; unless you want to alter the basic nature of the script.
  24. ;---- Alter the tray icon menu:
  25. Menu, Tray, Add, %k_MenuItemHide%, k_ShowHide
  26. Menu, Tray, Add, &Exit, k_MenuExit
  27. Menu, Tray, Default, %k_MenuItemHide%
  28. Menu, Tray, NoStandard
  29. ;---- Calculate object dimensions based on chosen font size:
  30. k_KeyWidth = %k_FontSize%
  31. k_KeyWidth *= 3
  32. k_KeyHeight = %k_FontSize%
  33. k_KeyHeight *= 3
  34. k_KeyMargin = %k_FontSize%
  35. k_KeyMargin /= 6
  36. k_SpacebarWidth = %k_FontSize%
  37. k_SpacebarWidth *= 25
  38. k_KeyWidthHalf = %k_KeyWidth%
  39. k_KeyWidthHalf /= 2
  40. k_KeySize = w%k_KeyWidth% h%k_KeyHeight%
  41. k_Position = x+%k_KeyMargin% %k_KeySize%
  42. ;---- Create a GUI window for the on-screen keyboard:
  43. Gui, Font, s%k_FontSize% %k_FontStyle%, %k_FontName%
  44. Gui, -Caption +E0x200 +ToolWindow
  45. TransColor = F1ECED
  46. Gui, Color, %TransColor% ; This color will be made transparent later below.
  47. ;---- Add a button for each key. Position the first button with absolute
  48. ; coordinates so that all other buttons can be positioned relative to it:
  49. Gui, Add, Button, section %k_KeySize% xm+%k_KeyWidth%, 1
  50. Gui, Add, Button, %k_Position%, 2
  51. Gui, Add, Button, %k_Position%, 3
  52. Gui, Add, Button, %k_Position%, 4
  53. Gui, Add, Button, %k_Position%, 5
  54. Gui, Add, Button, %k_Position%, 6
  55. Gui, Add, Button, %k_Position%, 7
  56. Gui, Add, Button, %k_Position%, 8
  57. Gui, Add, Button, %k_Position%, 9
  58. Gui, Add, Button, %k_Position%, 0
  59. Gui, Add, Button, %k_Position%, -
  60. Gui, Add, Button, %k_Position%, =
  61. Gui, Add, Button, %k_Position%, Bk
  62. Gui, Add, Button, xm y+%k_KeyMargin% h%k_KeyHeight%, Tab ; Auto-width.
  63. Gui, Add, Button, %k_Position%, Q
  64. Gui, Add, Button, %k_Position%, W
  65. Gui, Add, Button, %k_Position%, E
  66. Gui, Add, Button, %k_Position%, R
  67. Gui, Add, Button, %k_Position%, T
  68. Gui, Add, Button, %k_Position%, Y
  69. Gui, Add, Button, %k_Position%, U
  70. Gui, Add, Button, %k_Position%, I
  71. Gui, Add, Button, %k_Position%, O
  72. Gui, Add, Button, %k_Position%, P
  73. Gui, Add, Button, %k_Position%, [
  74. Gui, Add, Button, %k_Position%, ]
  75. Gui, Add, Button, %k_Position%, \
  76. Gui, Add, Button, xs+%k_KeyWidthHalf% y+%k_KeyMargin% %k_KeySize%, A
  77. Gui, Add, Button, %k_Position%, S
  78. Gui, Add, Button, %k_Position%, D
  79. Gui, Add, Button, %k_Position%, F
  80. Gui, Add, Button, %k_Position%, G
  81. Gui, Add, Button, %k_Position%, H
  82. Gui, Add, Button, %k_Position%, J
  83. Gui, Add, Button, %k_Position%, K
  84. Gui, Add, Button, %k_Position%, L
  85. Gui, Add, Button, %k_Position%, `;
  86. Gui, Add, Button, %k_Position%, '
  87. Gui, Add, Button, x+%k_KeyMargin% h%k_KeyHeight%, Enter ; Auto-width.
  88. ; The first button below adds %A_Space% at the end to widen it a little,
  89. ; making the layout of keys next to it more accurately reflect a real keyboard:
  90. Gui, Add, Button, xm y+%k_KeyMargin% h%k_KeyHeight%, Shift%A_Space%%A_Space%
  91. Gui, Add, Button, %k_Position%, Z
  92. Gui, Add, Button, %k_Position%, X
  93. Gui, Add, Button, %k_Position%, C
  94. Gui, Add, Button, %k_Position%, V
  95. Gui, Add, Button, %k_Position%, B
  96. Gui, Add, Button, %k_Position%, N
  97. Gui, Add, Button, %k_Position%, M
  98. Gui, Add, Button, %k_Position%, `,
  99. Gui, Add, Button, %k_Position%, .
  100. Gui, Add, Button, %k_Position%, /
  101. Gui, Add, Button, xm y+%k_KeyMargin% h%k_KeyHeight%, Ctrl ; Auto-width.
  102. Gui, Add, Button, h%k_KeyHeight% x+%k_KeyMargin%, Win ; Auto-width.
  103. Gui, Add, Button, h%k_KeyHeight% x+%k_KeyMargin%, Alt ; Auto-width.
  104. Gui, Add, Button, h%k_KeyHeight% x+%k_KeyMargin% w%k_SpacebarWidth%, Space
  105. ;---- Show the window:
  106. Gui, Show
  107. k_IsVisible = y
  108. WinGet, k_ID, ID, A ; Get its window ID.
  109. WinGetPos,,, k_WindowWidth, k_WindowHeight, A
  110. ;---- Position the keyboard at the bottom of the screen (taking into account
  111. ; the position of the taskbar):
  112. SysGet, k_WorkArea, MonitorWorkArea, %k_Monitor%
  113. ; Calculate window's X-position:
  114. k_WindowX = %k_WorkAreaRight%
  115. k_WindowX -= %k_WorkAreaLeft% ; Now k_WindowX contains the width of this monitor.
  116. k_WindowX -= %k_WindowWidth%
  117. k_WindowX /= 2 ; Calculate position to center it horizontally.
  118. ; The following is done in case the window will be on a non-primary monitor
  119. ; or if the taskbar is anchored on the left side of the screen:
  120. k_WindowX += %k_WorkAreaLeft%
  121. ; Calculate window's Y-position:
  122. k_WindowY = %k_WorkAreaBottom%
  123. k_WindowY -= %k_WindowHeight%
  124. WinMove, A,, %k_WindowX%, %k_WindowY%
  125. WinSet, AlwaysOnTop, On, ahk_id %k_ID%
  126. WinSet, TransColor, %TransColor% 220, ahk_id %k_ID%
  127. ;---- Set all keys as hotkeys. See www.asciitable.com
  128. k_n = 1
  129. k_ASCII = 45
  130. Loop
  131. {
  132. Transform, k_char, Chr, %k_ASCII%
  133. StringUpper, k_char, k_char
  134. if k_char not in <,>,^,~,,`,
  135. Hotkey, ~*%k_char%, k_KeyPress
  136. ; In the above, the asterisk prefix allows the key to be detected regardless
  137. ; of whether the user is holding down modifier keys such as Control and Shift.
  138. if k_ASCII = 93
  139. break
  140. k_ASCII++
  141. }
  142. return ; End of auto-execute section.
  143. ;---- When a key is pressed by the user, click the corresponding button on-screen:
  144. ~*Backspace::
  145. ControlClick, Bk, ahk_id %k_ID%, , LEFT, 1, D
  146. KeyWait, Backspace
  147. ControlClick, Bk, ahk_id %k_ID%, , LEFT, 1, U
  148. return
  149. ; LShift and RShift are used rather than "Shift" because when used as a hotkey,
  150. ; "Shift" would default to firing upon release of the key (in older AHK versions):
  151. ~*LShift::
  152. ~*RShift::
  153. ~*LCtrl:: ; Must use Ctrl not Control to match button names.
  154. ~*RCtrl::
  155. ~*LAlt::
  156. ~*RAlt::
  157. ~*LWin::
  158. ~*RWin::
  159. StringTrimLeft, k_ThisHotkey, A_ThisHotkey, 3
  160. ControlClick, %k_ThisHotkey%, ahk_id %k_ID%, , LEFT, 1, D
  161. KeyWait, %k_ThisHotkey%
  162. ControlClick, %k_ThisHotkey%, ahk_id %k_ID%, , LEFT, 1, U
  163. return
  164. ~*,::
  165. ~*'::
  166. ~*Space::
  167. ~*Enter::
  168. ~*Tab::
  169. k_KeyPress:
  170. StringReplace, k_ThisHotkey, A_ThisHotkey, ~
  171. StringReplace, k_ThisHotkey, k_ThisHotkey, *
  172. SetTitleMatchMode, 3 ; Prevents the T and B keys from being confused with Tab and Backspace.
  173. ControlClick, %k_ThisHotkey%, ahk_id %k_ID%, , LEFT, 1, D
  174. KeyWait, %k_ThisHotkey%
  175. ControlClick, %k_ThisHotkey%, ahk_id %k_ID%, , LEFT, 1, U
  176. Return
  177. k_ShowHide:
  178. if k_IsVisible = y
  179. {
  180. Gui, Cancel
  181. Menu, Tray, Rename, %k_MenuItemHide%, %k_MenuItemShow%
  182. k_IsVisible = n
  183. }
  184. else
  185. {
  186. Gui, Show
  187. Menu, Tray, Rename, %k_MenuItemShow%, %k_MenuItemHide%
  188. k_IsVisible = y
  189. }
  190. return
  191. GuiClose:
  192. k_MenuExit:
  193. ExitApp