KeyboardOnScreen.ahk 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. ; On-Screen Keyboard (based on the v1 script by Jon)
  2. ; https://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 unset to use
  20. ; the primary:
  21. k_Monitor := unset
  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. ;---- Create a Gui window for the on-screen keyboard:
  25. MyGui := Gui("-Caption +ToolWindow +AlwaysOnTop +Disabled")
  26. MyGui.SetFont("s" k_FontSize " " k_FontStyle, k_FontName)
  27. MyGui.MarginY := 0, MyGui.MarginX := 0
  28. ;---- Alter the tray icon menu:
  29. A_TrayMenu.Delete
  30. A_TrayMenu.Add k_MenuItemHide, k_ShowHide
  31. A_TrayMenu.Add "&Exit", (*) => ExitApp()
  32. A_TrayMenu.Default := k_MenuItemHide
  33. ;---- Add a button for each key:
  34. ; The keyboard layout:
  35. k_Layout := [
  36. ["``", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Backspace:3"],
  37. ["Tab:3", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\"],
  38. ["CapsLock:3", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter:2"],
  39. ["LShift:3", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "Shift:3"],
  40. ["LCtrl:2", "LWin:2", "LAlt:2", "Space:2", "RAlt:2", "RWin:2", "AppsKey:2", "RCtrl:2"]
  41. ]
  42. ; Traverse the keys of the keyboard layout:
  43. for n, k_Row in k_Layout
  44. for i, k_Key in k_Row
  45. {
  46. k_KeyWidthMultiplier := 1
  47. ; Get custom key width multiplier:
  48. if RegExMatch(k_Key, "(.+):(\d)", &m)
  49. {
  50. k_Key := m[1]
  51. k_KeyWidthMultiplier := m[2]
  52. }
  53. ; Get localized key name:
  54. k_KeyNameText := GetKeyNameText(k_Key, 0, 1)
  55. ; Windows key names start with left or right so replace it:
  56. if (k_Key = "LWin" || k_Key = "RWin")
  57. k_KeyNameText := "Win"
  58. ; Truncate the key name:
  59. if (StrLen(k_Key) > 1)
  60. k_KeyNameText := Trim(SubStr(k_KeyNameText, 1, 5))
  61. else
  62. k_KeyNameText := k_Key
  63. ; Convert to uppercase:
  64. k_KeyNameText := StrUpper(k_KeyNameText)
  65. ; Calculate object dimensions based on chosen font size:
  66. k_KeyHeight := k_FontSize * 3
  67. opt := "h" k_KeyHeight " w" k_KeyHeight * k_KeyWidthMultiplier " -Wrap x+m"
  68. if (i = 1)
  69. opt .= " y+m xm"
  70. ; Add the button:
  71. Btn := MyGui.Add("Button", opt, k_KeyNameText)
  72. ; When a key is pressed by the user, click the corresponding button on-screen:
  73. Hotkey("~*" k_Key, k_KeyPress.bind(Btn))
  74. }
  75. ;---- Position the keyboard at the bottom of the screen (taking into account
  76. ; the position of the taskbar):
  77. MyGui.Show("Hide") ; Required to get the window's calculated width and height.
  78. ; Calculate window's X-position:
  79. MonitorGetWorkArea(k_Monitor?, &WL, , &WR, &WB)
  80. MyGui.GetPos(, , &k_width, &k_height)
  81. k_xPos := (WR - WL - k_width) / 2 ; Calculate position to center it horizontally.
  82. ; The following is done in case the window will be on a non-primary monitor
  83. ; or if the taskbar is anchored on the left side of the screen:
  84. k_xPos += WL
  85. ; Calculate window's Y-position:
  86. k_yPos := WB - k_height
  87. ;---- Show the window:
  88. MyGui.Show("x" k_xPos " y" k_yPos " NA")
  89. ;---- Function definitions:
  90. k_KeyPress(BtnCtrl, *)
  91. {
  92. BtnCtrl.Opt("Default") ; Highlight the last pressed key.
  93. ControlClick(, BtnCtrl, , , , "D")
  94. KeyWait(SubStr(A_ThisHotkey, 3))
  95. ControlClick(, BtnCtrl, , , , "U")
  96. }
  97. k_ShowHide(*)
  98. {
  99. static isVisible := true
  100. if isVisible
  101. {
  102. MyGui.Hide
  103. A_TrayMenu.Rename k_MenuItemHide, k_MenuItemShow
  104. isVisible := false
  105. }
  106. else
  107. {
  108. MyGui.Show
  109. A_TrayMenu.Rename k_MenuItemShow, k_MenuItemHide
  110. isVisible := true
  111. }
  112. }
  113. GetKeyNameText(Key, Extended := false, DoNotCare := false)
  114. {
  115. Params := (GetKeySC(Key) << 16) | (Extended << 24) | (DoNotCare << 25)
  116. KeyNameText := Buffer(64, 0)
  117. DllCall("User32.dll\GetKeyNameText", "Int", Params, "Ptr", KeyNameText, "Int", 32)
  118. return StrGet(KeyNameText)
  119. }