IBNobody

Autohotkey Keyboard Help GUI

Sep 4th, 2016
765
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Autohotkey Keyboard Help GUI
  2. ; v1.1 by IBNobody
  3.  
  4. ; 1. Go to KLE (http://www.keyboard-layout-editor.com/) create your keymap.
  5. ; 2. Download the keymap as a PNG.
  6. ; 3. Add/remove/replace lines in cLayerPicture with paths to your pictures.
  7. ;    Each picture represents a layer. Try to keep the pictures the same size.
  8. ; 4. Edit the script
  9. ; 5. Edit cLayerName to match the names of the layers.
  10. ; 6. Run this script.
  11.  
  12. ; Pressing any key or clicking on the window will cause the help window to close.
  13. ; Pressing a new hotkey defined as a different layout will change the displayed layout.
  14. ; Pressing up/down/left/right/=/-/+ will change the displayed layout
  15.  
  16. ; Use QMK/TMK and want a quick way to edit your Keyboard Layouts?
  17. ; https://docs.google.com/spreadsheets/d/1jSnt4iHeVmF_P8mvwC7ke3bwFeODJz2woejqeDRZtWI/edit?usp=sharing
  18.  
  19. ;{ --- Defines ----------------------------------------------------------------
  20.  
  21. #NoEnv
  22. #MaxHotkeysPerInterval 99000000
  23. #HotkeyInterval 99000000
  24. ;~ #KeyHistory 0
  25. ;~ ListLines Off
  26. Process, Priority, , H ;if unstable, comment or remove this line
  27. SetBatchLines, -1
  28. SetKeyDelay, -1, -1
  29. SetMouseDelay, -1
  30. SetDefaultMouseSpeed, 0
  31. SetWinDelay, -1
  32. SetControlDelay, -1
  33. SendMode Input
  34.  
  35. #InstallMouseHook
  36. #InstallKeybdHook
  37. #SingleInstance Force
  38. #WinActivateForce
  39. #MaxThreadsPerHotkey 1
  40.  
  41. DetectHiddenWindows On
  42. SetTitleMatchMode 2
  43. SetWorkingDir %A_ScriptDir%
  44. CoordMode, Pixel, Screen ; Pixel Searching
  45. ;}
  46.  
  47. ;{ --- Constants & Variables --------------------------------------------------
  48. ; Constants
  49.  
  50. ; Use Keyboard Layout Editor to "download" a PNG image of the keymap
  51. ; The examples below expect the PNG image to be in the same folder as the script.
  52. cLayerPicture := [".\atomic_qwerty.png"
  53.                  ,".\atomic_upper.png"
  54.                  ,".\atomic_lower.png"]
  55.                  
  56. cLayerName :=    ["ATOMIC - QWERTY"
  57.                  ,"ATOMIC - UPPER LAYER"
  58.                  ,"ATOMIC - LOWER LAYER"]
  59.  
  60. ; Set this to true to have the GUI cycle from the first to last layer and vice versa when
  61. ; the arrow keys are pressed. Otherwise, the GUI will stop at the first or last layer.
  62. cLoopOnCycle := true
  63.  
  64. ; Variables
  65. vPicHandle1 := 0
  66. vPicHandle2 := 0
  67. vvHotkeyHandle := ""
  68. vActivePicControl := 1
  69. vCurrentKeyboardLayer := 0
  70. ;}
  71.  
  72. ;{ --- Keybinds ---------------------------------------------------------------
  73. ; Change these to use whatever hotkey you want to use
  74. F1::ExitApp
  75. F2::KeyboardHelpGui(1)
  76. F3::KeyboardHelpGui(2)
  77. F4::KeyboardHelpGui(3)
  78. ;}
  79.  
  80. ;  --- Functions --------------------------------------------------------------
  81.  
  82. ;{ Function Description
  83.  
  84. ; Display, Swap, or Close Keyboard Help GUI
  85. KeyboardHelpGui(vNewKeyboardLayer)
  86. {
  87.     global vCurrentKeyboardLayer, cLayerPicture, cLayerName
  88.     vKeyboardPicture := cLayerPicture[vNewKeyboardLayer]
  89.     vKeyboardLayerName := cLayerName[vNewKeyboardLayer]
  90.    
  91.     ; If Keyboard Help GUI is already open...
  92.     if (KeyboardHelpGuiExists())
  93.     {
  94.         ; If a new picture is to be displayed, swap the picture. Otherwise, destroy the GUI.
  95.         if (vCurrentKeyboardLayer != vNewKeyboardLayer)
  96.         {
  97.             vCurrentKeyboardLayer := vNewKeyboardLayer
  98.             KeyboardHelpGuiSwap(vKeyboardPicture, vKeyboardLayerName)
  99.         }
  100.         else
  101.         {
  102.             KeyboardHelpGuiDestroy()
  103.         }
  104.     }
  105.     else
  106.     {  
  107.         vCurrentKeyboardLayer := vNewKeyboardLayer
  108.         KeyboardHelpGuiCreate(vKeyboardPicture, vKeyboardLayerName)
  109.     }
  110.     return
  111. }
  112.  
  113. KeyboardHelpGuiCycle(vDirection, vLoop)
  114. {
  115.     global vCurrentKeyboardLayer, cLayerPicture
  116.     vNewKeyboardLayer := vCurrentKeyboardLayer + vDirection
  117.  
  118.     if (vLoop)
  119.     {
  120.         if (vNewKeyboardLayer < 1)
  121.         {
  122.             vNewKeyboardLayer := cLayerPicture.MaxIndex()
  123.         }
  124.         else if (vNewKeyboardLayer > cLayerPicture.MaxIndex())
  125.         {
  126.             vNewKeyboardLayer := 1
  127.         }
  128.     }
  129.     else
  130.     {
  131.         if (vNewKeyboardLayer < 1)
  132.         {
  133.             vNewKeyboardLayer := 1
  134.         }
  135.         else if (vNewKeyboardLayer > cLayerPicture.MaxIndex())
  136.         {
  137.             vNewKeyboardLayer := cLayerPicture.MaxIndex()
  138.         }
  139.     }
  140.  
  141.     if (vNewKeyboardLayer != vCurrentKeyboardLayer)
  142.     {
  143.         KeyboardHelpGui(vNewKeyboardLayer)
  144.     }
  145.     return
  146. }
  147.  
  148. ; Check if Keyboard Help GUI is already open
  149. KeyboardHelpGuiExists()
  150. {
  151.     ; If Keyboard Help GUI is already open...
  152.     Gui KeyboardHelp: +LastFoundExist
  153.     return WinExist()
  154. }
  155.  
  156. ; Keyboard Help GUI OnClick Callback
  157. KeyboardHelpGuiHotkey()
  158. {
  159.  
  160.     global vHotkeyHandle, cLoopOnCycle
  161.     handle := vHotkeyHandle
  162.     GuiControl KeyboardHelp: , vHotkeyHandle, ""
  163.     GuiControl KeyboardHelp: Focus, vHotkeyHandle
  164.     if ((vHotkeyHandle == "Right") || (vHotkeyHandle == "Up") || (vHotkeyHandle == "=") || (vHotkeyHandle == "Right") || (vHotkeyHandle == "Up") || (vHotkeyHandle == "NumpadAdd"))
  165.     {
  166.         KeyboardHelpGuiCycle(1, cLoopOnCycle)
  167.     }
  168.     else if ((vHotkeyHandle == "Left") ||  (vHotkeyHandle == "Down") || (vHotkeyHandle == "-") || (vHotkeyHandle == "Left") ||  (vHotkeyHandle == "Down") || (vHotkeyHandle == "NumpadSub"))
  169.     {
  170.         KeyboardHelpGuiCycle(-1, cLoopOnCycle)
  171.     }
  172.     else
  173.     {
  174.         KeyboardHelpGuiDestroy()
  175.     }
  176.     return
  177. }
  178.  
  179. ; Keyboard Help GUI OnClick Callback
  180. KeyboardHelpGuiClick()
  181. {
  182.     GuiControl KeyboardHelp: Focus, vHotkeyHandle
  183.     KeyboardHelpGuiDestroy()
  184.     return
  185. }
  186.  
  187. ; Keyboard Help GUI OnClose Callback
  188. KeyboardHelpGuiClose(guiHwnd)
  189. {
  190.     KeyboardHelpGuiDestroy()
  191.     return
  192. }
  193.  
  194. ; Keyboard Help GUI onEscape Callback
  195. KeyboardHelpGuiEscape(guiHwnd)
  196. {
  197.     KeyboardHelpGuiDestroy()
  198.     return
  199. }
  200.  
  201. ; Create and show the Keyboard Help GUI
  202. KeyboardHelpGuiCreate(vKeyboardPicture, vTitle)
  203. {  
  204.     global vPicHandle1, vPicHandle2, vHotkeyHandle
  205.     global vActivePicControl := 1
  206.  
  207.     ; Create the GUI and set its parameters
  208.     Gui KeyboardHelp: new, , %vTitle%
  209.     Gui KeyboardHelp: +AlwaysOnTop -Resize -SysMenu
  210.  
  211.     ; Create a Hotkey control outside the bounds of the window to send any keypresses to a callback
  212.     Gui KeyboardHelp: Add, Hotkey, gKeyboardHelpGuiHotkey vvHotkeyHandle x-20 y-20
  213.     ;~ Gui KeyboardHelp: Add, Hotkey, gKeyboardHelpGuiHotkey vvHotkeyHandle x0 y500
  214.  
  215.     ; Create two image controls (one hidden) and set them to send any mouse clicks to a callback
  216.     Gui KeyboardHelp: Add, Picture, gKeyboardHelpGuiClick vvPicHandle1 x10 y10, %vKeyboardPicture%
  217.     Gui KeyboardHelp: Add, Picture, gKeyboardHelpGuiClick vvPicHandle2 x10 y10, %vKeyboardPicture%
  218.     GuiControl KeyboardHelp: Focus, vHotkeyHandle
  219.     GuiControl KeyboardHelp: Hide, vPicHandle2
  220.  
  221.     ; Show the GUI
  222.     Gui KeyboardHelp: Show, AutoSize Center, %vTitle%
  223.     return
  224. }
  225.  
  226. ; Swap the picture on the Keyboard Help GUI
  227. KeyboardHelpGuiSwap(vNewKeyboardPicture, vTitle)
  228. {
  229.     static
  230.     global vActivePicControl
  231.  
  232.     ; Update the hidden picture control with the new picture and display it
  233.     if (vActivePicControl == 1)
  234.     {
  235.         vActivePicControl := 2
  236.         GuiControl KeyboardHelp: , vPicHandle2, %vNewKeyboardPicture%
  237.         GuiControl KeyboardHelp: show, vPicHandle2
  238.         GuiControl KeyboardHelp: hide, vPicHandle1
  239.     }
  240.     else
  241.     {
  242.         vActivePicControl := 1
  243.         GuiControl KeyboardHelp: , vPicHandle1, %vNewKeyboardPicture%
  244.         GuiControl KeyboardHelp: show, vPicHandle1
  245.         GuiControl KeyboardHelp: hide, vPicHandle2
  246.     }
  247.     Gui KeyboardHelp: Show, AutoSize Center, %vTitle%
  248.     return
  249. }
  250.  
  251. ; Destroy the Keyboard Help GUI
  252. KeyboardHelpGuiDestroy()
  253. {
  254.     Gui KeyboardHelp: Destroy
  255.     return
  256. }
  257.  
  258. ;}
  259.  
  260. ;{ ShellHook Callback
  261. ;~ ShellMessage( wParam, lParam )
  262. ;~ {
  263.     ;~ WinGetTitle, Title, ahk_id %lParam%
  264.     ;~ WinGetClass, ClassOfWin, ahk_id %lParam%
  265.     ;~ ToolTip, w = %wParam%   l = %lParam%   Title = %Title%   Class = %ClassOfWin%
  266. ;~ }
  267. ;}
Advertisement
Add Comment
Please, Sign In to add comment