SHARE
TWEET

DynamicKeys class

a guest Aug 12th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #persistent
  2. #Singleinstance force
  3.  
  4.  
  5. notepadKeys := new DynamicKeys((*) => WinActive("ahk_exe notepad.exe"))
  6. notepadKeys.RegisterHotkey(["Ctrl", "A"], Func("SendText").bind("
  7. (
  8. oHttp := ComObjCreate("WinHttp.WinHttpRequest.5.1")
  9. oHttp.Open("GET", )
  10. oHttp.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
  11. oHttp.send()
  12. )"), "Http object snippet/hold to get next help")
  13. notepadKeys.RegisterHotkey(["Ctrl", "B"], () => MsgBox("Help.`nThis is notepad") && MsgBox("Close it"), "Help dialog")
  14. ; notepadKeys.RegisterHotkey(["Ctrl", "A", "B"], () => MsgBox("Close it"), "Help dialog") ; TODO
  15.  
  16.  
  17. alwaysOnWorkKeys := new DynamicKeys((*) => true)
  18. alwaysOnWorkKeys.RegisterHotkey(["Alt", "F5"], Func("msgbox").bind("text"), "Just a text")
  19.  
  20.  
  21.  
  22.  
  23. class DynamicKeys
  24. {
  25.     __New(predicate)
  26.     {
  27.         if !IsObject(predicate)
  28.             MsgBox "fail"
  29.         this.predicate := predicate
  30.         this.keys := {}
  31.         this.currentNode := this
  32.     }
  33.    
  34.     RegisterHotkey(keyList, action, description := "")
  35.     {
  36.         node := this
  37.         for i, key in keyList
  38.         {
  39.             if !keyVkCode := GetKeyVK(key)
  40.             {
  41.                 MsgBox "Invalid key: " key
  42.                 return false
  43.             }
  44.             keyVkCode := Format("{:X}", keyVkCode)
  45.             keyName := GetKeyName("vk" keyVkCode)
  46.             if !node.keys.HasKey(keyVkCode)
  47.             {
  48.                 node.keys.%keyVkCode% := { "name" : keyName , "keys" : {} }
  49.                 if i == 1 && this.currentNode == this
  50.                 {
  51.                     Hotkey "If", this.predicate
  52.                     Hotkey "vk" keyVkCode, ObjBindMethod(this, "private_onKeyPressed")
  53.                     Hotkey "If"
  54.                 }
  55.             }
  56.             node := node.keys.%keyVkCode%
  57.         }
  58.         node.description := description
  59.         node.action := action
  60.         return true
  61.     }
  62.    
  63.     private_onKeyPressed()
  64.     {
  65.         keyVkCode := Format("{:X}", GetKeyVK(A_ThisHotkey))
  66.         if !this.currentNode.keys.%keyVkCode%.HasKey("action") ; TODO: or key holded > 250 ms
  67.         {
  68.             this.private_disableNodeKeys(this.currentNode)
  69.             this.currentNode := this.currentNode.keys.%keyVkCode%
  70.             this.private_activateNodeKeys(this.currentNode)
  71.             this.private_showNodeHelp(this.currentNode)
  72.         }
  73.         else
  74.         {
  75.             this.currentNode.keys.%keyVkCode%.action.call()
  76.             this.private_disableNodeKeys(this.currentNode)
  77.             this.currentNode := this
  78.             this.private_activateNodeKeys(this.currentNode)
  79.         }
  80.     }
  81.    
  82.     private_disableNodeKeys(node)
  83.     {
  84.         Hotkey "If", this.predicate
  85.         for key in node.keys
  86.             Hotkey "vk" key, "Off"
  87.         Hotkey "If"
  88.     }
  89.    
  90.     private_activateNodeKeys(node)
  91.     {
  92.         Hotkey "If", this.predicate
  93.         for key in node.keys
  94.         {
  95.             Hotkey "vk" key, ObjBindMethod(this, "private_onKeyPressed")
  96.             Hotkey "vk" key, "On"
  97.         }
  98.         Hotkey "If"
  99.     }
  100.    
  101.     private_showNodeHelp(node)
  102.     {
  103.         text := ""
  104.         for _, v in node.keys
  105.             text .= v.name " - " v.description "`n"
  106.        
  107.         ToolTip text
  108.         SetTimer Func("ToolTip"), -2500
  109.     }
  110. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top