Guest User

KeyChord - v2 AutoHotKey Class

a guest
Jun 28th, 2024
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Autohotkey 5.22 KB | Source Code | 0 0
  1. /**
  2.  *  KeyChord.ahk
  3.  *  
  4.  *  A class for writing key chords in AutoHotKey.
  5.  *  Now combinations like "Ctrl+Win+d, x, u" are supported!
  6.  *  
  7.  *  @version 1.0
  8.  *  @author Komrad Toast ([email protected])
  9.  *  @see https://autohotkey.com/boards/<RELEASE PAGE LINK>
  10.  *  @license
  11.  *  Copyright (c) 2024 Tyler J. Colby (Komrad Toast)
  12.  *  
  13.  *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  14.  *  documentation files (the "Software"), to deal in the Software without restriction, including without limitation
  15.  *  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
  16.  *  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  17.  *  
  18.  *  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  19.  *  
  20.  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21.  *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22.  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  23.  *  IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24.  *  
  25. **/
  26.  
  27. #Requires AutoHotkey v2.0
  28.  
  29. /**
  30.  *  KeyChord class
  31.  *  This class provides a way to map keys to commands or nested key Chords.
  32.  *  It allows for executing commands or nested key Chords based on user input.
  33.  *  @class KeyChord
  34.  *  @method `Add(key)`
  35.  *  @method `Delete(key)`
  36.  *  @method `Clear()`
  37.  *  @method `Execute(timeout)`
  38. **/
  39. class KeyChord
  40. {
  41.     /**
  42.      *  @type {Map}
  43.      *  @private
  44.      *  @desc Map of keys to commands
  45.     **/
  46.     commands := Map()
  47.  
  48.     /**
  49.      *  @type {Map}
  50.      *  @private
  51.      *  @desc Map of keys to nested KeyChord instances
  52.     **/
  53.     nestedChords := Map()
  54.  
  55.     /**
  56.      *  Add a key-command mapping or a nested key Chord.
  57.      *  @public
  58.      *  @param {String} key The key to map
  59.      *  @param {String|Integer|Float|BoundFunc|KeyChord} command - The command or nested key Chord to map
  60.      *  @returns {Void}
  61.     **/
  62.     Add(key, command)
  63.     {
  64.         if IsObject(command) && (Type(command) == "KeyChord")
  65.         {
  66.             this.nestedChords.Set(key, command)
  67.         }
  68.         else
  69.         {
  70.             this.commands.Set(key, command)
  71.         }
  72.     }
  73.  
  74.     /**
  75.      *  Remove a key-command mapping or a nested key Chord.
  76.      *  @public
  77.      *  @param {String} key The key to remove
  78.      *  @returns {Void}
  79.     **/
  80.     Remove(key)
  81.     {
  82.         if this.commands.Has(key)
  83.         {
  84.             if (Type(key) == "KeyChord")
  85.                 this.nestedChords.Delete(key)
  86.             else
  87.                 this.commands.Delete(key)
  88.         }
  89.     }
  90.  
  91.     /**
  92.      *  Clear all key-command mappings and nested key Chords.
  93.      *  @public
  94.      *  @returns {Void}
  95.     **/
  96.     Clear()
  97.     {
  98.         commands := Map()
  99.         nestedChords := Map()
  100.     }
  101.  
  102.     /**
  103.      *  Execute the command or nested key Chord mapped to the user input.
  104.      *  @public
  105.      *  @param {Integer} timeout The timeout (in milliseconds) for user input
  106.      *  @returns {Void}
  107.     **/
  108.     Execute(timeout)
  109.     {
  110.         key := GetUserInput(timeout)
  111.  
  112.         if this.commands.Has(key)
  113.         {
  114.             command := this.commands.Get(key)
  115.             this.ExecuteCommand(command, timeout)
  116.             return
  117.         }
  118.         else if this.nestedChords.Has(key)
  119.         {
  120.             nestedChord := this.nestedChords.Get(key)
  121.             nestedChord.Execute(timeout)
  122.             return
  123.         }
  124.         else
  125.         {
  126.             MsgBox("Key not found: " . key, "Error")
  127.         }
  128.     }
  129.  
  130.     /**
  131.      *  Execute the given command based on its type.
  132.      *  @private
  133.      *  @param {BoundFunc|Boolean|Integer|String|Float|KeyChord} command The "command" to execute
  134.      *  @param {Integer} timeout The timeout (in milliseconds) for user input
  135.      *  @returns {Void}
  136.     **/
  137.     ExecuteCommand(command, timeout)
  138.     {
  139.         cmdType := Type(command)
  140.  
  141.         Switch cmdType
  142.         {
  143.             Case "String":
  144.                 Send(command)
  145.                 return
  146.             Case "Integer":
  147.                 Send(command)
  148.                 return
  149.             Case "Float":
  150.                 Send(command)
  151.                 return
  152.             Case "Boolean":
  153.                 Send(command)
  154.                 return
  155.             Case "KeyChord":
  156.                 this.ExecuteNestedChord(timeout)
  157.                 return
  158.             Case "BoundFunc":
  159.                 command.Call()
  160.                 return
  161.             Default:
  162.                 MsgBox("Invalid Key Chord type: " cmdType, "Error")
  163.                 return
  164.         }
  165.     }
  166.  
  167.     /**
  168.      *  Execute a nested key Chord.
  169.      *  @private
  170.      *  @param {Integer} timeout - The timeout (in milliseconds) for user input
  171.      *  @returns {Void}
  172.     **/
  173.     ExecuteNestedChord(timeout)
  174.     {
  175.         this.Execute(timeout)
  176.     }
  177.  
  178.     SendValue(val)
  179.     {
  180.         Send(val)
  181.         return
  182.     }
  183. }
Tags: autohotkey
Advertisement
Add Comment
Please, Sign In to add comment