• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

GUIKeyEvent 1.4a

Version 1.31 introduced the possibility to detect when ANY keyboard key is pressed or released,
but this functionality is limited to JASS/Lua users only as Blizzard never
implemented any GUI functionality for this new feature.

I've seen some requests and questions from GUI users wondering how they
can implement this feature, but it's usually difficult as they're not
used to work with code.

GUIKeyEvent changes that!

# Features
  • Detect when any keyboard key is pressed or released, by any player.
  • Detect if a meta key ("Shift", "Control", "Alt" and "META") was pressed or released with the key.

# How it Works
  • The system registers every key press/release combination for every enabled player.
  • The system then detects whenever a player presses or releases a key and stores the data into GUI variables you can use with your triggers.
  • The system uses the popular GUI event "Game - Value of Real Variable" to trigger all your GUIKey events.

# How to Use
  • Use the event "Game - GUIKeyEvent becomes Equal to 1.00" as your trigger event to detect a GUIKey event.
  • You then have the option to use 5 variables that's been set for you by the GUIKeyEvent system:
    • "_GUIKeyPlayer" = This is the player that pressed or released the keyboard key that triggered the event.
    • "_GUIKeyTrigger" = This is the keyboard key that was pressed or released with this event.
      • Use an "Integer Comparison" in order to detect which key was pressed. Example: "_GUIKeyTrigger Equal to GUIKEY_W"
    • "_GUIKeyMeta" = This holds meta key data for the pressed or released key. Was Shift, Control, Alt or the META key pressed or released together with the key?
      • Use an "Integer Comparison" in order to detect which meta key was pressed. Example: "_GUIKeyMeta Equal to GUIMETAKEY_SHIFT". You may also check for multiple at once, like "_GUIKeyMeta Equal to GUIMETAKEY_SHIFT_CONTROL_ALT"
    • "_GUIKeyIsPressed" = Is the keyboard key pressed?
      • Use a "Boolean Comparison" in order to detect if the key is pressed or not. Example: "_GUIKeyIsPressed Equal to True"
    • "_GUIKeyIsReleased" = Is the keyboard key released?
      • Use a "Boolean Comparison" in order to detect if the key is pressed or not. Example: "_GUIKeyIsReleased Equal to True"
  • You can turn off the system at any time with the "Trigger - Turn off GUIKeyEvent_System" action and then turn it on with "Trigger - Turn on GUIKeyEvent_System".

# How to Install
  • 1. Check the "(optional) GUIKeyEvent Demo(s)" folder in the Demo Map for some examples of how to use GUIKeyEvent before installing.
  • 2. Different instruction depending on:
    • a. If you're installing from fresh:
      • a1. Open the Demo Map and copy the "GUIKeyEvent"-folder.
      • a2. Open your map and paste the folder into the Trigger Editor.
      • a3. Go through the "GUIKeyEvent Settings" trigger.
    • b. If you're already using any version prior to GUIKeyEvent 1.4a, then follow this:
      • b1. Make sure to note down your GUIKeyEvent settings from the "GUIKeyEvent Settings" trigger.
      • b2. If you keep any of your own custom triggers in the "GUIKeyEvent"-folder, make sure to move them somewhere else.
      • b3. Delete the "GUIKeyEvent"-folder.
      • b4. Open the Demo Map and copy the "GUIKeyEvent"-folder.
      • b5. Open your map and paste the folder into the Trigger Editor.
      • b6. Update any GUIKeyEvent settings in the "GUIKeyEvent Settings" trigger that you had noted down.
    • c. If you're using any 1.4a+ version, then follow this:
      • c1. Delete the "GUIKeyEvent System Code & Variables"-folder in your map.
      • c2. Open the Demop Map and copy the "GUIKeyEvent System Code & Variables"-folder.
      • c3. Open your map and paste the folder into the "GUIKeyEvent"-folder.
  • 3. Done!

# Additional Notes
  • WASD Movement Detect KeyInput
    • Events
      • Game - GUIKeyEvent becomes Equal to 1.00
    • Conditions
      • Or - Any (Conditions) are true
        • Conditions
          • _GUIKeyTrigger Equal to GUIKEY_W
          • _GUIKeyTrigger Equal to GUIKEY_A
          • _GUIKeyTrigger Equal to GUIKEY_S
          • _GUIKeyTrigger Equal to GUIKEY_D
    • Actions
      • -------- Check if the key pressed OR released was the "W"-key: --------
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • _GUIKeyTrigger Equal to GUIKEY_W
        • Then - Actions
          • -------- Set the players current "W"-boolean status to true or false, depending on if the key was pressed or released --------
          • Set VariableSet WASD_W_IsPressed[(Player number of _GUIKeyPlayer)] = _GUIKeyIsPressed
        • Else - Actions
      • -------- Check if the key pressed OR released was the "A"-key: --------
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • _GUIKeyTrigger Equal to GUIKEY_A
        • Then - Actions
          • -------- Set the players current "A"-boolean status to true or false, depending on if the key was pressed or released --------
          • Set VariableSet WASD_A_IsPressed[(Player number of _GUIKeyPlayer)] = _GUIKeyIsPressed
        • Else - Actions
      • -------- Check if the key pressed OR released was the "S"-key: --------
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • _GUIKeyTrigger Equal to GUIKEY_S
        • Then - Actions
          • -------- Set the players current "S"-boolean status to true or false, depending on if the key was pressed or released --------
          • Set VariableSet WASD_S_IsPressed[(Player number of _GUIKeyPlayer)] = _GUIKeyIsPressed
        • Else - Actions
      • -------- Check if the key pressed OR released was the "D"-key: --------
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • _GUIKeyTrigger Equal to GUIKEY_D
        • Then - Actions
          • -------- Set the players current "D"-boolean status to true or false, depending on if the key was pressed or released --------
          • Set VariableSet WASD_D_IsPressed[(Player number of _GUIKeyPlayer)] = _GUIKeyIsPressed
        • Else - Actions



# 1.4a
  • Every "GUIKEY_"-variable now have "Initial Value" set in the Trigger Editor.
  • This allow users to freely delete any "GUIKEY_" or "GUIMETAKEY_" variables they don't need, without causing syntax errors.
  • If a key is to be restored, check the "How to Restore a GUIKEY or GUIMETAKEY" in the "GUIKeyEvent Variables" folder.
  • "Lightweight" version is no longer necessary and code related to it has been removed.
  • Slight optimization in RegisterKey function by setting a player variable instead if calling Player(x) multiple times.
  • Improved overall documentation across triggers and READ ME.
  • (Demo Map Only) Added a simple demo trigger that shows how to enable/disable the system.

# 1.3a
  • Optimized GUIKeyEvent System Code (~572 to 331 lines), saves some kB.
  • The following keys are no longer "lightweight":
    • GUIKEY_VOLUME_MUTE
    • GUIKEY_VOLUME_DOWN
    • GUIKEY_VOLUME_UP
    • GUIKEY_MEDIA_NEXT_TRACK
    • GUIKEY_MEDIA_PREV_TRACK
    • GUIKEY_MEDIA_STOP
    • GUIKEY_MEDIA_PLAY_PAUSE

# 1.2a:
  • Added a "Lightweight" version.
    • This version only detect the more common keys and uses 54 less variables.
    • The map loading time is improved as well.
  • Added a new string array variable "GUIKEY_NAME" that can be used to get the name of the keys. Could be useful for debugging, etc.
  • Fixed an issue that caused the OSKEY_OEM_CLEAR key to not be registered.

# 1.1a:
  • The system is now initialized at RunInitializationTriggers in main (in order to not have GUIKeyEvent Settings overwritten by InitGlobals).
  • Removed desync prone GetPlayerController/GetPlayerSlotState at map init and instead added GUIKeyEvent_S_EnablePlayer to the GUIKeyEvent Settings trigger that allows the user to manually set players to use.
  • "GUIKeyEvent_S_BlockRepeat" setting set to true by default in the GUIKeyEvent Settings trigger.
  • Renamed the variable "_GUIKeyIsDown" to "_GUIKeyIsPressed".
  • Added a new boolean variable "_GUIKeyIsReleased", that can be used instead of "_GUIKeyIsPressed Equal to False"

# 1.0a:
  • Release.


JASS:
//===========================================================================
//
//  GUIKeyEvent 1.4a
//  Author: lolreported
//
//  Important Note:
//  Please do not edit anything below,
//  unless you know what you're doing.
//
//===========================================================================

library GUIKeyEvent
    globals
        // For init
        private constant integer CONVERT_OS_KEY_TYPE_INDEX_START = $08
        private constant integer CONVERT_OS_KEY_TYPE_INDEX_END   = $FE
        
        // Value combined of all possible meta keys.
        // MetaKeys are "none"(0), "shift"(1), "control"(2), "alt"(4) and "META"(8) (windows key).
        private constant integer META_KEY_MAX = 15
        
        //===========================================================================
        // Only used during system setup to keep track on the current key to register
        private integer currentKey
        
        //===========================================================================
        // Used by "udg_GUIKeyEvent_S_BlockRepeat" setting
        private integer array playerPrevKey
        private boolean array playerKeyWasDown
    endglobals
    
    // This function runs whenever a registrered player presses/releases a registered event key
    private function OnGUIKeyEvent takes nothing returns boolean
        local integer playerId
        // Set globals whenever a player presses or releases a key
        // Set player that triggered the oskey event
        set udg__GUIKeyPlayer     = GetTriggerPlayer()
        // Convert oskey to handle id in order to be able to compare with GUI key variables
        set udg__GUIKeyTrigger    = GetHandleId(BlzGetTriggerPlayerKey())
        // Set boolean equal to if the key was pressed or released
        set udg__GUIKeyIsPressed  = BlzGetTriggerPlayerIsKeyDown()
        
        // ===========================================================================================
        // Ignore new events if the key is held down and udg_GUIKeyEvent_S_BlockRepeat is set to true.
        if (udg_GUIKeyEvent_S_BlockRepeat) then
            set playerId = GetPlayerId(udg__GUIKeyPlayer)
            if (playerKeyWasDown[playerId] == udg__GUIKeyIsPressed and playerPrevKey[playerId] == udg__GUIKeyTrigger) then
                return false
            endif
            set playerPrevKey[playerId]    = udg__GUIKeyTrigger
            set playerKeyWasDown[playerId] = udg__GUIKeyIsPressed
        endif
        // ===========================================================================================
        // Set meta key for pressed key
        set udg__GUIKeyMeta       = BlzGetTriggerPlayerMetaKey()
        set udg__GUIKeyIsReleased = not udg__GUIKeyIsPressed

        // Trigger event(s)
        set udg_GUIKeyEvent = 0.0
        set udg_GUIKeyEvent = 1.0
        //set udg_GUIKeyEvent = 0.0
        return false
    endfunction
    
    private function RegisterKey takes nothing returns boolean
        local integer playerId
        local player p
        local integer metaKey
        local oskeytype key
        
        //ConvertOsKeyType starts at index 8 and ends at index 254 ($08 to $FE)
        set currentKey = currentKey + 1
        if (currentKey > CONVERT_OS_KEY_TYPE_INDEX_END) then
          // No more keys to register, finish.
          call DestroyTrigger(GetTriggeringTrigger())
          return false
        endif

        // Get the current oskey and register player key events
        set key = ConvertOsKeyType(currentKey)
        set playerId = 0
        loop
            // Only register oskey events for players that are active
            // Registers both keyDown/keyUp and all metaKey combinations.
            if (udg_GUIKeyEvent_S_EnablePlayer[playerId + 1]) then
                set p = Player(playerId)
                set metaKey = 0
                loop
                    call BlzTriggerRegisterPlayerKeyEvent(udg_GUIKeyEvent_System, p, key, metaKey, false)
                    call BlzTriggerRegisterPlayerKeyEvent(udg_GUIKeyEvent_System, p, key, metaKey, true)
                    set metaKey = metaKey + 1
                    exitwhen(metaKey > META_KEY_MAX)
                endloop
            endif
            
            set playerId = playerId + 1
            exitwhen(playerId >= bj_MAX_PLAYERS)
        endloop
        
        set p   = null
        set key = null
        
        // Run this trigger again, until all keys have been registered
        call TriggerEvaluate(GetTriggeringTrigger())
        return false
    endfunction

    private function InitGUIKeyNames takes nothing returns nothing
        set udg_GUIKEY_NAME[$08] = "BACKSPACE"
        set udg_GUIKEY_NAME[$09] = "TAB"
        set udg_GUIKEY_NAME[$0C] = "CLEAR"
        set udg_GUIKEY_NAME[$0D] = "RETURN"
        set udg_GUIKEY_NAME[$10] = "SHIFT"
        set udg_GUIKEY_NAME[$11] = "CONTROL"
        set udg_GUIKEY_NAME[$12] = "ALT"
        set udg_GUIKEY_NAME[$13] = "PAUSE"
        set udg_GUIKEY_NAME[$14] = "CAPSLOCK"
        set udg_GUIKEY_NAME[$15] = "KANA"
        set udg_GUIKEY_NAME[$15] = "HANGUL"
        set udg_GUIKEY_NAME[$17] = "JUNJA"
        set udg_GUIKEY_NAME[$18] = "FINAL"
        set udg_GUIKEY_NAME[$19] = "HANJA"
        set udg_GUIKEY_NAME[$19] = "KANJI"
        set udg_GUIKEY_NAME[$1B] = "ESCAPE"
        set udg_GUIKEY_NAME[$1C] = "CONVERT"
        set udg_GUIKEY_NAME[$1D] = "NONCONVERT"
        set udg_GUIKEY_NAME[$1E] = "ACCEPT"
        set udg_GUIKEY_NAME[$1F] = "MODECHANGE"
        set udg_GUIKEY_NAME[$20] = "SPACE"
        set udg_GUIKEY_NAME[$21] = "PAGEUP"
        set udg_GUIKEY_NAME[$22] = "PAGEDOWN"
        set udg_GUIKEY_NAME[$23] = "END"
        set udg_GUIKEY_NAME[$24] = "HOME"
        set udg_GUIKEY_NAME[$25] = "LEFT"
        set udg_GUIKEY_NAME[$26] = "UP"
        set udg_GUIKEY_NAME[$27] = "RIGHT"
        set udg_GUIKEY_NAME[$28] = "DOWN"
        set udg_GUIKEY_NAME[$29] = "SELECT"
        set udg_GUIKEY_NAME[$2A] = "PRINT"
        set udg_GUIKEY_NAME[$2B] = "EXECUTE"
        set udg_GUIKEY_NAME[$2C] = "PRINTSCREEN"
        set udg_GUIKEY_NAME[$2D] = "INSERT"
        set udg_GUIKEY_NAME[$2E] = "DELETE"
        set udg_GUIKEY_NAME[$2F] = "HELP"
        set udg_GUIKEY_NAME[$30] = "0"
        set udg_GUIKEY_NAME[$31] = "1"
        set udg_GUIKEY_NAME[$32] = "2"
        set udg_GUIKEY_NAME[$33] = "3"
        set udg_GUIKEY_NAME[$34] = "4"
        set udg_GUIKEY_NAME[$35] = "5"
        set udg_GUIKEY_NAME[$36] = "6"
        set udg_GUIKEY_NAME[$37] = "7"
        set udg_GUIKEY_NAME[$38] = "8"
        set udg_GUIKEY_NAME[$39] = "9"
        set udg_GUIKEY_NAME[$41] = "A"
        set udg_GUIKEY_NAME[$42] = "B"
        set udg_GUIKEY_NAME[$43] = "C"
        set udg_GUIKEY_NAME[$44] = "D"
        set udg_GUIKEY_NAME[$45] = "E"
        set udg_GUIKEY_NAME[$46] = "F"
        set udg_GUIKEY_NAME[$47] = "G"
        set udg_GUIKEY_NAME[$48] = "H"
        set udg_GUIKEY_NAME[$49] = "I"
        set udg_GUIKEY_NAME[$4A] = "J"
        set udg_GUIKEY_NAME[$4B] = "K"
        set udg_GUIKEY_NAME[$4C] = "L"
        set udg_GUIKEY_NAME[$4D] = "M"
        set udg_GUIKEY_NAME[$4E] = "N"
        set udg_GUIKEY_NAME[$4F] = "O"
        set udg_GUIKEY_NAME[$50] = "P"
        set udg_GUIKEY_NAME[$51] = "Q"
        set udg_GUIKEY_NAME[$52] = "R"
        set udg_GUIKEY_NAME[$53] = "S"
        set udg_GUIKEY_NAME[$54] = "T"
        set udg_GUIKEY_NAME[$55] = "U"
        set udg_GUIKEY_NAME[$56] = "V"
        set udg_GUIKEY_NAME[$57] = "W"
        set udg_GUIKEY_NAME[$58] = "X"
        set udg_GUIKEY_NAME[$59] = "Y"
        set udg_GUIKEY_NAME[$5A] = "Z"
        set udg_GUIKEY_NAME[$5B] = "LMETA"
        set udg_GUIKEY_NAME[$5C] = "RMETA"
        set udg_GUIKEY_NAME[$5D] = "APPS"
        set udg_GUIKEY_NAME[$5F] = "SLEEP"
        set udg_GUIKEY_NAME[$60] = "NUMPAD0"
        set udg_GUIKEY_NAME[$61] = "NUMPAD1"
        set udg_GUIKEY_NAME[$62] = "NUMPAD2"
        set udg_GUIKEY_NAME[$63] = "NUMPAD3"
        set udg_GUIKEY_NAME[$64] = "NUMPAD4"
        set udg_GUIKEY_NAME[$65] = "NUMPAD5"
        set udg_GUIKEY_NAME[$66] = "NUMPAD6"
        set udg_GUIKEY_NAME[$67] = "NUMPAD7"
        set udg_GUIKEY_NAME[$68] = "NUMPAD8"
        set udg_GUIKEY_NAME[$69] = "NUMPAD9"
        set udg_GUIKEY_NAME[$6A] = "MULTIPLY"
        set udg_GUIKEY_NAME[$6B] = "ADD"
        set udg_GUIKEY_NAME[$6C] = "SEPARATOR "
        set udg_GUIKEY_NAME[$6D] = "SUBTRACT"
        set udg_GUIKEY_NAME[$6E] = "DECIMAL"
        set udg_GUIKEY_NAME[$6F] = "DIVIDE"
        set udg_GUIKEY_NAME[$70] = "F1"
        set udg_GUIKEY_NAME[$71] = "F2"
        set udg_GUIKEY_NAME[$72] = "F3"
        set udg_GUIKEY_NAME[$73] = "F4"
        set udg_GUIKEY_NAME[$74] = "F5"
        set udg_GUIKEY_NAME[$75] = "F6"
        set udg_GUIKEY_NAME[$76] = "F7"
        set udg_GUIKEY_NAME[$77] = "F8"
        set udg_GUIKEY_NAME[$78] = "F9"
        set udg_GUIKEY_NAME[$79] = "F10"
        set udg_GUIKEY_NAME[$7A] = "F11"
        set udg_GUIKEY_NAME[$7B] = "F12"
        set udg_GUIKEY_NAME[$7C] = "F13"
        set udg_GUIKEY_NAME[$7D] = "F14"
        set udg_GUIKEY_NAME[$7E] = "F15"
        set udg_GUIKEY_NAME[$7F] = "F16"
        set udg_GUIKEY_NAME[$80] = "F17"
        set udg_GUIKEY_NAME[$81] = "F18"
        set udg_GUIKEY_NAME[$82] = "F19"
        set udg_GUIKEY_NAME[$83] = "F20"
        set udg_GUIKEY_NAME[$84] = "F21"
        set udg_GUIKEY_NAME[$85] = "F22"
        set udg_GUIKEY_NAME[$86] = "F23"
        set udg_GUIKEY_NAME[$87] = "F24"
        set udg_GUIKEY_NAME[$90] = "NUMLOCK"
        set udg_GUIKEY_NAME[$91] = "SCROLLLOCK"
        set udg_GUIKEY_NAME[$92] = "OEM_NEC_EQUAL"
        set udg_GUIKEY_NAME[$92] = "OEM_FJ_JISHO"
        set udg_GUIKEY_NAME[$93] = "OEM_FJ_MASSHOU"
        set udg_GUIKEY_NAME[$94] = "OEM_FJ_TOUROKU"
        set udg_GUIKEY_NAME[$95] = "OEM_FJ_LOYA"
        set udg_GUIKEY_NAME[$96] = "OEM_FJ_ROYA"
        set udg_GUIKEY_NAME[$A0] = "LSHIFT"
        set udg_GUIKEY_NAME[$A1] = "RSHIFT"
        set udg_GUIKEY_NAME[$A2] = "LCONTROL"
        set udg_GUIKEY_NAME[$A3] = "RCONTROL"
        set udg_GUIKEY_NAME[$A4] = "LALT"
        set udg_GUIKEY_NAME[$A5] = "RALT"
        set udg_GUIKEY_NAME[$A6] = "BROWSER_BACK"
        set udg_GUIKEY_NAME[$A7] = "BROWSER_FORWARD"
        set udg_GUIKEY_NAME[$A8] = "BROWSER_REFRESH"
        set udg_GUIKEY_NAME[$A9] = "BROWSER_STOP"
        set udg_GUIKEY_NAME[$AA] = "BROWSER_SEARCH"
        set udg_GUIKEY_NAME[$AB] = "BROWSER_FAVORITES"
        set udg_GUIKEY_NAME[$AC] = "BROWSER_HOME"
        set udg_GUIKEY_NAME[$AD] = "VOLUME_MUTE"
        set udg_GUIKEY_NAME[$AE] = "VOLUME_DOWN"
        set udg_GUIKEY_NAME[$AF] = "VOLUME_UP"
        set udg_GUIKEY_NAME[$B0] = "MEDIA_NEXT_TRACK"
        set udg_GUIKEY_NAME[$B1] = "MEDIA_PREV_TRACK"
        set udg_GUIKEY_NAME[$B2] = "MEDIA_STOP"
        set udg_GUIKEY_NAME[$B3] = "MEDIA_PLAY_PAUSE"
        set udg_GUIKEY_NAME[$B4] = "LAUNCH_MAIL"
        set udg_GUIKEY_NAME[$B5] = "LAUNCH_MEDIA_SELECT"
        set udg_GUIKEY_NAME[$B6] = "LAUNCH_APP1"
        set udg_GUIKEY_NAME[$B7] = "LAUNCH_APP2" 
        set udg_GUIKEY_NAME[$BA] = "OEM_1"
        set udg_GUIKEY_NAME[$BB] = "OEM_PLUS"
        set udg_GUIKEY_NAME[$BC] = "OEM_COMMA"
        set udg_GUIKEY_NAME[$BD] = "OEM_MINUS"
        set udg_GUIKEY_NAME[$BE] = "OEM_PERIOD"
        set udg_GUIKEY_NAME[$BF] = "OEM_2"
        set udg_GUIKEY_NAME[$C0] = "OEM_3"
        set udg_GUIKEY_NAME[$DB] = "OEM_4"
        set udg_GUIKEY_NAME[$DC] = "OEM_5"
        set udg_GUIKEY_NAME[$DD] = "OEM_6"
        set udg_GUIKEY_NAME[$DE] = "OEM_7"
        set udg_GUIKEY_NAME[$DF] = "OEM_8"
        set udg_GUIKEY_NAME[$E1] = "OEM_AX"
        set udg_GUIKEY_NAME[$E2] = "OEM_102"
        set udg_GUIKEY_NAME[$E3] = "ICO_HELP"
        set udg_GUIKEY_NAME[$E4] = "ICO_00"
        set udg_GUIKEY_NAME[$E5] = "PROCESSKEY"
        set udg_GUIKEY_NAME[$E6] = "ICO_CLEAR"
        set udg_GUIKEY_NAME[$E7] = "PACKET"
        set udg_GUIKEY_NAME[$E9] = "OEM_RESET"
        set udg_GUIKEY_NAME[$EA] = "OEM_JUMP"
        set udg_GUIKEY_NAME[$EB] = "OEM_PA1"
        set udg_GUIKEY_NAME[$EC] = "OEM_PA2"
        set udg_GUIKEY_NAME[$ED] = "OEM_PA3"
        set udg_GUIKEY_NAME[$EE] = "OEM_WSCTRL"
        set udg_GUIKEY_NAME[$EF] = "OEM_CUSEL"
        set udg_GUIKEY_NAME[$F0] = "OEM_ATTN"
        set udg_GUIKEY_NAME[$F1] = "OEM_FINISH"
        set udg_GUIKEY_NAME[$F2] = "OEM_COPY"
        set udg_GUIKEY_NAME[$F3] = "OEM_AUTO"
        set udg_GUIKEY_NAME[$F4] = "OEM_ENLW"
        set udg_GUIKEY_NAME[$F5] = "OEM_BACKTAB"
        set udg_GUIKEY_NAME[$F6] = "ATTN"
        set udg_GUIKEY_NAME[$F7] = "CRSEL"
        set udg_GUIKEY_NAME[$F8] = "EXSEL"
        set udg_GUIKEY_NAME[$F9] = "EREOF"
        set udg_GUIKEY_NAME[$FA] = "PLAY"
        set udg_GUIKEY_NAME[$FB] = "ZOOM"
        set udg_GUIKEY_NAME[$FC] = "NONAME"
        set udg_GUIKEY_NAME[$FD] = "PA1"
        set udg_GUIKEY_NAME[$FE] = "OEM_CLEAR"
    endfunction
    
    public function GUIKeyEventInit takes nothing returns nothing
        local trigger t
        
        // Create the trigger that handles oskey input from players
        set udg_GUIKeyEvent_System = CreateTrigger()
        call TriggerAddCondition(udg_GUIKeyEvent_System, Condition(function OnGUIKeyEvent))

        // Initializes the GUIKey Name array
        call InitGUIKeyNames()

        // Register oskey events for players
        // Run each key as a new thread to avoid op-limit (but shouldn't be an issue most likely anyway).
        // Results in ~100k operations for 24 players
        set currentKey = CONVERT_OS_KEY_TYPE_INDEX_START - 1
        set t = CreateTrigger()
        call TriggerAddCondition(t, Condition(function RegisterKey))
        call TriggerEvaluate(t)
        set t = null
    endfunction
endlibrary
Contents

GUIKeyEvent 1.4a (Map)

Reviews
MyPad
Very interesting. Bridging the gap between GUI and vJASS is always an appreciated gesture for the users. The testmap plays out like a charm as well. There aren't many changes to be made here codewise and suggestions that have been made pertaining...
JASS:
set playerIsActive[playerId] = (GetPlayerController(Player(playerId)) == MAP_CONTROL_USER and GetPlayerSlotState(Player(playerId)) == PLAYER_SLOT_STATE_PLAYING)

This is unsafe during initialization, it causes desyncs.
Thanks, I suspected so, but couldn't find any concrete answer.

I'll look for another approach. The downside to not have it at map init is due to the noticeable lag caused by the event creation.
 
Updated to version 1.1a.

Changes:
  • The system is now initialized at RunInitializationTriggers in main (in order to not have GUIKeyEvent Settings overwritten by InitGlobals).
  • Removed desync prone GetPlayerController/GetPlayerSlotState at map init and instead added GUIKeyEvent_S_EnablePlayer to the GUIKeyEvent Settings trigger that allows the user to manually set players to use.
  • "GUIKeyEvent_S_BlockRepeat" setting set to true by default in the GUIKeyEvent Settings trigger.
  • Renamed the variable "_GUIKeyIsDown" to "_GUIKeyIsPressed".
  • Added a new boolean variable "_GUIKeyIsReleased", that can be used instead of "_GUIKeyIsPressed Equal to False"

I plan to release a "lightweight" version as well, which removes a lot of the keys I don't think most users will use.


2022-04-24 Edit:
Updated to version 1.2a.

Changes:
  • Added a "Lightweight" version.
    • This version only detect the more common keys and uses 54 less variables.
    • The map loading time is improved as well.
  • Added a new string array variable "GUIKEY_NAME" that can be used to get the name of the keys. Could be useful for debugging, etc.
  • Fixed an issue that caused the OSKEY_OEM_CLEAR key to not be registered.
The Lightweight version is a seperate map.
 
Last edited:
Very interesting. Bridging the gap between GUI and vJASS is always an appreciated gesture for the users. The testmap plays out like a charm as well.

  • The LWKeyCheck function has an additional return statement just below the huge body of or-conditional statements. Perhaps this was supposed to be a placeholder?

    JASS:
    private function LWKeyCheck takes integer currentKey returns boolean        // Returns false if the key is not a lightweight key
        return not (currentKey == $15 or /*
            */ // huge body of conditions /*
            */ currentKey == $FE)
        return true
    endfunction

  • In the same function, I think it would be better if a boolean array variable would be used to hold the appropriate lightweight key indices (by setting the value at said indices to true), though it does come with a downside of having to initialize the array first. Still, since the LWKey values aren't expected to change, you may choose to implement the code changes.

    JASS:
    globals
        private boolean array isLWKey
    endglobals
    
    private function LWKeyInit takes nothing returns nothing
        set isLWKey[$15] = true
        // body
        set isLWKey[$FE] = true
    endfunction
    private function LWKeyCheck takes integer currentKey returns boolean
        return not isLWKey[currentKey]
    endfunction

There aren't many changes to be made here codewise and suggestions that have been made pertaining to the code are quite minor. Still, a 1.31 version of the same system would be nice, if feasibly compatible with the current Reforged version.

Status:

  • Approved
 
1.3a released, changes:
  • Optimized GUIKeyEvent System Code (~572 to 331 lines), saves some kB.
  • The following keys are no longer "lightweight":
    • GUIKEY_VOLUME_MUTE
    • GUIKEY_VOLUME_DOWN
    • GUIKEY_VOLUME_UP
    • GUIKEY_MEDIA_NEXT_TRACK
    • GUIKEY_MEDIA_PREV_TRACK
    • GUIKEY_MEDIA_STOP
    • GUIKEY_MEDIA_PLAY_PAUSE

JASS:
//===========================================================================
//
//  GUIKeyEvent 1.3a
//  Author: lolreported
//
//
//  Important Note:
//  Do not touch any of the code below, unless you
//  know what you're doing. I'll not help debug any errors
//  resulting from careless editing.
//
//===========================================================================

library GUIKeyEvent
    globals
        // If true, static if causes certain code not to be compiled for Lightweight version.
        // Will cause errors if set to false in lightweight version as GUI variables is missing.
        private constant boolean IS_LIGHTWEIGHT_VERSION = false
   
        // For init
        private constant integer CONVERT_OS_KEY_TYPE_INDEX_START = $08
        private constant integer CONVERT_OS_KEY_TYPE_INDEX_END   = $FE
       
        // Value combined of all possible meta keys.
        // MetaKeys are "none"(0), "shift"(1), "control"(2), "alt"(4) and "META"(8) (windows key).
        private constant integer META_KEY_MAX = 15
       
        //===========================================================================
        private integer currentKey    // Only used during system setup to keep track on the current key to register
        private boolean array isLWKey // Only used during system setup to check if a key is lightweight
       
        //===========================================================================
        // Used by "udg_GUIKeyEvent_S_BlockRepeat" setting
        private integer array playerPrevKey
        private boolean array playerKeyWasDown
    endglobals
   
    // This function runs whenever a registrered player presses/releases a registered event key
    private function OnGUIKeyEvent takes nothing returns boolean
        local integer playerId
        // Set globals whenever a players press a key
        set udg__GUIKeyPlayer     = GetTriggerPlayer() // Set player that triggered the oskey event
        set udg__GUIKeyTrigger    = GetHandleId(BlzGetTriggerPlayerKey()) // Convert oskey to handle id in order to be able to compare with GUI key variables
        set udg__GUIKeyIsPressed  = BlzGetTriggerPlayerIsKeyDown() // Set boolean equal to if the key was pressed or released
       
        // ===========================================================================================
        // Ignore new events if the key is held down and udg_GUIKeyEvent_S_BlockRepeat is set to true.
        if (udg_GUIKeyEvent_S_BlockRepeat) then
            set playerId = GetPlayerId(udg__GUIKeyPlayer)
            if (playerKeyWasDown[playerId] == udg__GUIKeyIsPressed and playerPrevKey[playerId] == udg__GUIKeyTrigger) then
                return false
            endif
            set playerPrevKey[playerId]    = udg__GUIKeyTrigger
            set playerKeyWasDown[playerId] = udg__GUIKeyIsPressed
        endif
        // ===========================================================================================
       
        set udg__GUIKeyMeta       = BlzGetTriggerPlayerMetaKey() // Set meta key for pressed key
        set udg__GUIKeyIsReleased = not udg__GUIKeyIsPressed

        // Trigger events
        set udg_GUIKeyEvent = 0.0
        set udg_GUIKeyEvent = 1.0
        //set udg_GUIKeyEvent = 0.0
        return false
    endfunction
   
    private function RegisterKey takes nothing returns boolean
        local integer playerId
        local integer metaKey
        local oskeytype key
       
        //ConvertOsKeyType starts at index 8 and ends at index 254 ($08 to $FE)
        set currentKey = currentKey + 1
        if (currentKey > CONVERT_OS_KEY_TYPE_INDEX_END) then
          // No more keys to register, finish.
          call DestroyTrigger(GetTriggeringTrigger())
          return false
        endif
       
        // If lightweight version, only register certain keys
        if (not IS_LIGHTWEIGHT_VERSION or isLWKey[currentKey]) then
            // Get the current oskey and register player key events
            set key = ConvertOsKeyType(currentKey)
            set playerId = 0
            loop
                // Only register oskey events for players that are active
                // Registers both keyDown/keyUp and all metaKey combinations.
                if (udg_GUIKeyEvent_S_EnablePlayer[playerId + 1]) then
                    set metaKey = 0
                    loop
                        call BlzTriggerRegisterPlayerKeyEvent(udg_GUIKeyEvent_System, Player(playerId), key, metaKey, false)
                        call BlzTriggerRegisterPlayerKeyEvent(udg_GUIKeyEvent_System, Player(playerId), key, metaKey, true)
                        set metaKey = metaKey + 1
                        exitwhen(metaKey > META_KEY_MAX)
                    endloop
                endif
                set playerId = playerId + 1
                exitwhen(playerId >= bj_MAX_PLAYERS)
            endloop
            set key = null
        endif
       
        // Run this trigger again, until all keys have been registered
        call TriggerEvaluate(GetTriggeringTrigger())
        return false
    endfunction
   
    private function InitGUIKey takes integer key, boolean isKeyLW, string keyName returns integer
        set isLWKey[key] = isKeyLW
        set udg_GUIKEY_NAME[key] = keyName
        return key
    endfunction
   
    private function InitGUIKeys takes nothing returns nothing
        // "Lightweight" keys
        set udg_GUIKEY_BACKSPACE               = InitGUIKey($08, true, "BACKSPACE")
        set udg_GUIKEY_TAB                     = InitGUIKey($09, true, "TAB")
        set udg_GUIKEY_CLEAR                   = InitGUIKey($0C, true, "CLEAR")
        set udg_GUIKEY_RETURN                  = InitGUIKey($0D, true, "RETURN")
        set udg_GUIKEY_SHIFT                   = InitGUIKey($10, true, "SHIFT")
        set udg_GUIKEY_CONTROL                 = InitGUIKey($11, true, "CONTROL")
        set udg_GUIKEY_ALT                     = InitGUIKey($12, true, "ALT")
        set udg_GUIKEY_PAUSE                   = InitGUIKey($13, true, "PAUSE")
        set udg_GUIKEY_CAPSLOCK                = InitGUIKey($14, true, "CAPSLOCK")
        set udg_GUIKEY_ESCAPE                  = InitGUIKey($1B, true, "ESCAPE")
        set udg_GUIKEY_SPACE                   = InitGUIKey($20, true, "SPACE")
        set udg_GUIKEY_PAGEUP                  = InitGUIKey($21, true, "PAGEUP")
        set udg_GUIKEY_PAGEDOWN                = InitGUIKey($22, true, "PAGEDOWN")
        set udg_GUIKEY_END                     = InitGUIKey($23, true, "END")
        set udg_GUIKEY_HOME                    = InitGUIKey($24, true, "HOME")
        set udg_GUIKEY_LEFT                    = InitGUIKey($25, true, "LEFT")
        set udg_GUIKEY_UP                      = InitGUIKey($26, true, "UP")
        set udg_GUIKEY_RIGHT                   = InitGUIKey($27, true, "RIGHT")
        set udg_GUIKEY_DOWN                    = InitGUIKey($28, true, "DOWN")
        set udg_GUIKEY_SELECT                  = InitGUIKey($29, true, "SELECT")
        set udg_GUIKEY_PRINT                   = InitGUIKey($2A, true, "PRINT")
        set udg_GUIKEY_EXECUTE                 = InitGUIKey($2B, true, "EXECUTE")
        set udg_GUIKEY_PRINTSCREEN             = InitGUIKey($2C, true, "PRINTSCREEN")
        set udg_GUIKEY_INSERT                  = InitGUIKey($2D, true, "INSERT")
        set udg_GUIKEY_DELETE                  = InitGUIKey($2E, true, "DELETE")
        set udg_GUIKEY_HELP                    = InitGUIKey($2F, true, "HELP")
        set udg_GUIKEY_0                       = InitGUIKey($30, true, "0")
        set udg_GUIKEY_1                       = InitGUIKey($31, true, "1")
        set udg_GUIKEY_2                       = InitGUIKey($32, true, "2")
        set udg_GUIKEY_3                       = InitGUIKey($33, true, "3")
        set udg_GUIKEY_4                       = InitGUIKey($34, true, "4")
        set udg_GUIKEY_5                       = InitGUIKey($35, true, "5")
        set udg_GUIKEY_6                       = InitGUIKey($36, true, "6")
        set udg_GUIKEY_7                       = InitGUIKey($37, true, "7")
        set udg_GUIKEY_8                       = InitGUIKey($38, true, "8")
        set udg_GUIKEY_9                       = InitGUIKey($39, true, "9")
        set udg_GUIKEY_A                       = InitGUIKey($41, true, "A")
        set udg_GUIKEY_B                       = InitGUIKey($42, true, "B")
        set udg_GUIKEY_C                       = InitGUIKey($43, true, "C")
        set udg_GUIKEY_D                       = InitGUIKey($44, true, "D")
        set udg_GUIKEY_E                       = InitGUIKey($45, true, "E")
        set udg_GUIKEY_F                       = InitGUIKey($46, true, "F")
        set udg_GUIKEY_G                       = InitGUIKey($47, true, "G")
        set udg_GUIKEY_H                       = InitGUIKey($48, true, "H")
        set udg_GUIKEY_I                       = InitGUIKey($49, true, "I")
        set udg_GUIKEY_J                       = InitGUIKey($4A, true, "J")
        set udg_GUIKEY_K                       = InitGUIKey($4B, true, "K")
        set udg_GUIKEY_L                       = InitGUIKey($4C, true, "L")
        set udg_GUIKEY_M                       = InitGUIKey($4D, true, "M")
        set udg_GUIKEY_N                       = InitGUIKey($4E, true, "N")
        set udg_GUIKEY_O                       = InitGUIKey($4F, true, "O")
        set udg_GUIKEY_P                       = InitGUIKey($50, true, "P")
        set udg_GUIKEY_Q                       = InitGUIKey($51, true, "Q")
        set udg_GUIKEY_R                       = InitGUIKey($52, true, "R")
        set udg_GUIKEY_S                       = InitGUIKey($53, true, "S")
        set udg_GUIKEY_T                       = InitGUIKey($54, true, "T")
        set udg_GUIKEY_U                       = InitGUIKey($55, true, "U")
        set udg_GUIKEY_V                       = InitGUIKey($56, true, "V")
        set udg_GUIKEY_W                       = InitGUIKey($57, true, "W")
        set udg_GUIKEY_X                       = InitGUIKey($58, true, "X")
        set udg_GUIKEY_Y                       = InitGUIKey($59, true, "Y")
        set udg_GUIKEY_Z                       = InitGUIKey($5A, true, "Z")
        set udg_GUIKEY_LMETA                   = InitGUIKey($5B, true, "LMETA")
        set udg_GUIKEY_RMETA                   = InitGUIKey($5C, true, "RMETA")
        set udg_GUIKEY_APPS                    = InitGUIKey($5D, true, "APPS")
        set udg_GUIKEY_SLEEP                   = InitGUIKey($5F, true, "SLEEP")
        set udg_GUIKEY_NUMPAD0                 = InitGUIKey($60, true, "NUMPAD0")
        set udg_GUIKEY_NUMPAD1                 = InitGUIKey($61, true, "NUMPAD1")
        set udg_GUIKEY_NUMPAD2                 = InitGUIKey($62, true, "NUMPAD2")
        set udg_GUIKEY_NUMPAD3                 = InitGUIKey($63, true, "NUMPAD3")
        set udg_GUIKEY_NUMPAD4                 = InitGUIKey($64, true, "NUMPAD4")
        set udg_GUIKEY_NUMPAD5                 = InitGUIKey($65, true, "NUMPAD5")
        set udg_GUIKEY_NUMPAD6                 = InitGUIKey($66, true, "NUMPAD6")
        set udg_GUIKEY_NUMPAD7                 = InitGUIKey($67, true, "NUMPAD7")
        set udg_GUIKEY_NUMPAD8                 = InitGUIKey($68, true, "NUMPAD8")
        set udg_GUIKEY_NUMPAD9                 = InitGUIKey($69, true, "NUMPAD9")
        set udg_GUIKEY_MULTIPLY                = InitGUIKey($6A, true, "MULTIPLY")
        set udg_GUIKEY_ADD                     = InitGUIKey($6B, true, "ADD")
        set udg_GUIKEY_SEPARATOR               = InitGUIKey($6C, true, "SEPARATOR ")
        set udg_GUIKEY_SUBTRACT                = InitGUIKey($6D, true, "SUBTRACT")
        set udg_GUIKEY_DECIMAL                 = InitGUIKey($6E, true, "DECIMAL")
        set udg_GUIKEY_DIVIDE                  = InitGUIKey($6F, true, "DIVIDE")
        set udg_GUIKEY_F1                      = InitGUIKey($70, true, "F1")
        set udg_GUIKEY_F2                      = InitGUIKey($71, true, "F2")
        set udg_GUIKEY_F3                      = InitGUIKey($72, true, "F3")
        set udg_GUIKEY_F4                      = InitGUIKey($73, true, "F4")
        set udg_GUIKEY_F5                      = InitGUIKey($74, true, "F5")
        set udg_GUIKEY_F6                      = InitGUIKey($75, true, "F6")
        set udg_GUIKEY_F7                      = InitGUIKey($76, true, "F7")
        set udg_GUIKEY_F8                      = InitGUIKey($77, true, "F8")
        set udg_GUIKEY_F9                      = InitGUIKey($78, true, "F9")
        set udg_GUIKEY_F10                     = InitGUIKey($79, true, "F10")
        set udg_GUIKEY_F11                     = InitGUIKey($7A, true, "F11")
        set udg_GUIKEY_F12                     = InitGUIKey($7B, true, "F12")
        set udg_GUIKEY_F13                     = InitGUIKey($7C, true, "F13")
        set udg_GUIKEY_F14                     = InitGUIKey($7D, true, "F14")
        set udg_GUIKEY_F15                     = InitGUIKey($7E, true, "F15")
        set udg_GUIKEY_F16                     = InitGUIKey($7F, true, "F16")
        set udg_GUIKEY_F17                     = InitGUIKey($80, true, "F17")
        set udg_GUIKEY_F18                     = InitGUIKey($81, true, "F18")
        set udg_GUIKEY_F19                     = InitGUIKey($82, true, "F19")
        set udg_GUIKEY_F20                     = InitGUIKey($83, true, "F20")
        set udg_GUIKEY_F21                     = InitGUIKey($84, true, "F21")
        set udg_GUIKEY_F22                     = InitGUIKey($85, true, "F22")
        set udg_GUIKEY_F23                     = InitGUIKey($86, true, "F23")
        set udg_GUIKEY_F24                     = InitGUIKey($87, true, "F24")
        set udg_GUIKEY_NUMLOCK                 = InitGUIKey($90, true, "NUMLOCK")
        set udg_GUIKEY_SCROLLLOCK              = InitGUIKey($91, true, "SCROLLLOCK")
        set udg_GUIKEY_LSHIFT                  = InitGUIKey($A0, true, "LSHIFT")
        set udg_GUIKEY_RSHIFT                  = InitGUIKey($A1, true, "RSHIFT")
        set udg_GUIKEY_LCONTROL                = InitGUIKey($A2, true, "LCONTROL")
        set udg_GUIKEY_RCONTROL                = InitGUIKey($A3, true, "RCONTROL")
        set udg_GUIKEY_LALT                    = InitGUIKey($A4, true, "LALT")
        set udg_GUIKEY_RALT                    = InitGUIKey($A5, true, "RALT")
        set udg_GUIKEY_OEM_1                   = InitGUIKey($BA, true, "OEM_1")
        set udg_GUIKEY_OEM_PLUS                = InitGUIKey($BB, true, "OEM_PLUS")
        set udg_GUIKEY_OEM_COMMA               = InitGUIKey($BC, true, "OEM_COMMA")
        set udg_GUIKEY_OEM_MINUS               = InitGUIKey($BD, true, "OEM_MINUS")
        set udg_GUIKEY_OEM_PERIOD              = InitGUIKey($BE, true, "OEM_PERIOD")
        set udg_GUIKEY_OEM_2                   = InitGUIKey($BF, true, "OEM_2")
        set udg_GUIKEY_OEM_3                   = InitGUIKey($C0, true, "OEM_3")
        set udg_GUIKEY_OEM_4                   = InitGUIKey($DB, true, "OEM_4")
        set udg_GUIKEY_OEM_5                   = InitGUIKey($DC, true, "OEM_5")
        set udg_GUIKEY_OEM_6                   = InitGUIKey($DD, true, "OEM_6")
        set udg_GUIKEY_OEM_7                   = InitGUIKey($DE, true, "OEM_7")
        set udg_GUIKEY_OEM_8                   = InitGUIKey($DF, true, "OEM_8")
        set udg_GUIKEY_OEM_AX                  = InitGUIKey($E1, true, "OEM_AX")
        set udg_GUIKEY_OEM_102                 = InitGUIKey($E2, true, "OEM_102")
       
        // Non "Lightweight" keys
        static if not IS_LIGHTWEIGHT_VERSION then
            set udg_GUIKEY_VOLUME_MUTE         = InitGUIKey($AD, false, "VOLUME_MUTE")
            set udg_GUIKEY_VOLUME_DOWN         = InitGUIKey($AE, false, "VOLUME_DOWN")
            set udg_GUIKEY_VOLUME_UP           = InitGUIKey($AF, false, "VOLUME_UP")
            set udg_GUIKEY_MEDIA_NEXT_TRACK    = InitGUIKey($B0, false, "MEDIA_NEXT_TRACK")
            set udg_GUIKEY_MEDIA_PREV_TRACK    = InitGUIKey($B1, false, "MEDIA_PREV_TRACK")
            set udg_GUIKEY_MEDIA_STOP          = InitGUIKey($B2, false, "MEDIA_STOP")
            set udg_GUIKEY_MEDIA_PLAY_PAUSE    = InitGUIKey($B3, false, "MEDIA_PLAY_PAUSE")
            set udg_GUIKEY_KANA                = InitGUIKey($15, false, "KANA")
            set udg_GUIKEY_HANGUL              = InitGUIKey($15, false, "HANGUL")
            set udg_GUIKEY_JUNJA               = InitGUIKey($17, false, "JUNJA")
            set udg_GUIKEY_FINAL               = InitGUIKey($18, false, "FINAL")
            set udg_GUIKEY_HANJA               = InitGUIKey($19, false, "HANJA")
            set udg_GUIKEY_KANJI               = InitGUIKey($19, false, "KANJI")
            set udg_GUIKEY_CONVERT             = InitGUIKey($1C, false, "CONVERT")
            set udg_GUIKEY_NONCONVERT          = InitGUIKey($1D, false, "NONCONVERT")
            set udg_GUIKEY_ACCEPT              = InitGUIKey($1E, false, "ACCEPT")
            set udg_GUIKEY_MODECHANGE          = InitGUIKey($1F, false, "MODECHANGE")
            set udg_GUIKEY_OEM_NEC_EQUAL       = InitGUIKey($92, false, "OEM_NEC_EQUAL")
            set udg_GUIKEY_OEM_FJ_JISHO        = InitGUIKey($92, false, "OEM_FJ_JISHO")
            set udg_GUIKEY_OEM_FJ_MASSHOU      = InitGUIKey($93, false, "OEM_FJ_MASSHOU")
            set udg_GUIKEY_OEM_FJ_TOUROKU      = InitGUIKey($94, false, "OEM_FJ_TOUROKU")
            set udg_GUIKEY_OEM_FJ_LOYA         = InitGUIKey($95, false, "OEM_FJ_LOYA")
            set udg_GUIKEY_OEM_FJ_ROYA         = InitGUIKey($96, false, "OEM_FJ_ROYA")
            set udg_GUIKEY_BROWSER_BACK        = InitGUIKey($A6, false, "BROWSER_BACK")
            set udg_GUIKEY_BROWSER_FORWARD     = InitGUIKey($A7, false, "BROWSER_FORWARD")
            set udg_GUIKEY_BROWSER_REFRESH     = InitGUIKey($A8, false, "BROWSER_REFRESH")
            set udg_GUIKEY_BROWSER_STOP        = InitGUIKey($A9, false, "BROWSER_STOP")
            set udg_GUIKEY_BROWSER_SEARCH      = InitGUIKey($AA, false, "BROWSER_SEARCH")
            set udg_GUIKEY_BROWSER_FAVORITES   = InitGUIKey($AB, false, "BROWSER_FAVORITES")
            set udg_GUIKEY_BROWSER_HOME        = InitGUIKey($AC, false, "BROWSER_HOME")
            set udg_GUIKEY_LAUNCH_MAIL         = InitGUIKey($B4, false, "LAUNCH_MAIL")
            set udg_GUIKEY_LAUNCH_MEDIA_SELECT = InitGUIKey($B5, false, "LAUNCH_MEDIA_SELECT")
            set udg_GUIKEY_LAUNCH_APP1         = InitGUIKey($B6, false, "LAUNCH_APP1")
            set udg_GUIKEY_LAUNCH_APP2         = InitGUIKey($B7, false, "LAUNCH_APP2")
            set udg_GUIKEY_ICO_HELP            = InitGUIKey($E3, false, "ICO_HELP")
            set udg_GUIKEY_ICO_00              = InitGUIKey($E4, false, "ICO_00")
            set udg_GUIKEY_PROCESSKEY          = InitGUIKey($E5, false, "PROCESSKEY")
            set udg_GUIKEY_ICO_CLEAR           = InitGUIKey($E6, false, "ICO_CLEAR")
            set udg_GUIKEY_PACKET              = InitGUIKey($E7, false, "PACKET")
            set udg_GUIKEY_OEM_RESET           = InitGUIKey($E9, false, "OEM_RESET")
            set udg_GUIKEY_OEM_JUMP            = InitGUIKey($EA, false, "OEM_JUMP")
            set udg_GUIKEY_OEM_PA1             = InitGUIKey($EB, false, "OEM_PA1")
            set udg_GUIKEY_OEM_PA2             = InitGUIKey($EC, false, "OEM_PA2")
            set udg_GUIKEY_OEM_PA3             = InitGUIKey($ED, false, "OEM_PA3")
            set udg_GUIKEY_OEM_WSCTRL          = InitGUIKey($EE, false, "OEM_WSCTRL")
            set udg_GUIKEY_OEM_CUSEL           = InitGUIKey($EF, false, "OEM_CUSEL")
            set udg_GUIKEY_OEM_ATTN            = InitGUIKey($F0, false, "OEM_ATTN")
            set udg_GUIKEY_OEM_FINISH          = InitGUIKey($F1, false, "OEM_FINISH")
            set udg_GUIKEY_OEM_COPY            = InitGUIKey($F2, false, "OEM_COPY")
            set udg_GUIKEY_OEM_AUTO            = InitGUIKey($F3, false, "OEM_AUTO")
            set udg_GUIKEY_OEM_ENLW            = InitGUIKey($F4, false, "OEM_ENLW")
            set udg_GUIKEY_OEM_BACKTAB         = InitGUIKey($F5, false, "OEM_BACKTAB")
            set udg_GUIKEY_ATTN                = InitGUIKey($F6, false, "ATTN")
            set udg_GUIKEY_CRSEL               = InitGUIKey($F7, false, "CRSEL")
            set udg_GUIKEY_EXSEL               = InitGUIKey($F8, false, "EXSEL")
            set udg_GUIKEY_EREOF               = InitGUIKey($F9, false, "EREOF")
            set udg_GUIKEY_PLAY                = InitGUIKey($FA, false, "PLAY")
            set udg_GUIKEY_ZOOM                = InitGUIKey($FB, false, "ZOOM")
            set udg_GUIKEY_NONAME              = InitGUIKey($FC, false, "NONAME")
            set udg_GUIKEY_PA1                 = InitGUIKey($FD, false, "PA1")
            set udg_GUIKEY_OEM_CLEAR           = InitGUIKey($FE, false, "OEM_CLEAR")
        endif
    endfunction
   
    public function GUIKeyEventInit takes nothing returns nothing
        local trigger t
       
        // Create the trigger that handles oskey input
        set udg_GUIKeyEvent_System = CreateTrigger()
        call TriggerAddCondition(udg_GUIKeyEvent_System, Condition(function OnGUIKeyEvent))

        // Initializes the GUIKey variables
        call InitGUIKeys()

        // Register oskey events for players
        // Run each key as a new thread to avoid op-limit (but shouldn't be an issue most likely anyway).
        // Results in ~100k operations for 24 players
        set currentKey = CONVERT_OS_KEY_TYPE_INDEX_START - 1
        set t = CreateTrigger()
        call TriggerAddCondition(t, Condition(function RegisterKey))
        call TriggerEvaluate(t)
        set t = null
    endfunction
endlibrary


Updating from 1.2a to 1.3a is recommended, but not necessary.
 
1.4a released, changes:
  • Every "GUIKEY_"-variable now have "Initial Value" set in the Trigger Editor.
  • This allow users to freely delete any "GUIKEY_" or "GUIMETAKEY_" variables they don't need, without causing syntax errors.
  • If a key is to be restored, check the "How to Restore a GUIKEY or GUIMETAKEY" in the "GUIKeyEvent Variables" folder.
  • "Lightweight" version is no longer necessary and code related to it has been removed.
  • Slight optimization in RegisterKey function by setting a player variable instead if calling Player(x) multiple times.
  • Improved overall documentation across triggers and READ ME.
  • (Demo Map Only) Added a simple demo trigger that shows how to enable/disable the system.
JASS:
//===========================================================================
//
//  GUIKeyEvent 1.4a
//  Author: lolreported
//
//  Important Note:
//  Please do not edit anything below,
//  unless you know what you're doing.
//
//===========================================================================

library GUIKeyEvent
    globals
        // For init
        private constant integer CONVERT_OS_KEY_TYPE_INDEX_START = $08
        private constant integer CONVERT_OS_KEY_TYPE_INDEX_END   = $FE
        
        // Value combined of all possible meta keys.
        // MetaKeys are "none"(0), "shift"(1), "control"(2), "alt"(4) and "META"(8) (windows key).
        private constant integer META_KEY_MAX = 15
        
        //===========================================================================
        // Only used during system setup to keep track on the current key to register
        private integer currentKey
        
        //===========================================================================
        // Used by "udg_GUIKeyEvent_S_BlockRepeat" setting
        private integer array playerPrevKey
        private boolean array playerKeyWasDown
    endglobals
    
    // This function runs whenever a registrered player presses/releases a registered event key
    private function OnGUIKeyEvent takes nothing returns boolean
        local integer playerId
        // Set globals whenever a player presses or releases a key
        // Set player that triggered the oskey event
        set udg__GUIKeyPlayer     = GetTriggerPlayer()
        // Convert oskey to handle id in order to be able to compare with GUI key variables
        set udg__GUIKeyTrigger    = GetHandleId(BlzGetTriggerPlayerKey())
        // Set boolean equal to if the key was pressed or released
        set udg__GUIKeyIsPressed  = BlzGetTriggerPlayerIsKeyDown()
        
        // ===========================================================================================
        // Ignore new events if the key is held down and udg_GUIKeyEvent_S_BlockRepeat is set to true.
        if (udg_GUIKeyEvent_S_BlockRepeat) then
            set playerId = GetPlayerId(udg__GUIKeyPlayer)
            if (playerKeyWasDown[playerId] == udg__GUIKeyIsPressed and playerPrevKey[playerId] == udg__GUIKeyTrigger) then
                return false
            endif
            set playerPrevKey[playerId]    = udg__GUIKeyTrigger
            set playerKeyWasDown[playerId] = udg__GUIKeyIsPressed
        endif
        // ===========================================================================================
        // Set meta key for pressed key
        set udg__GUIKeyMeta       = BlzGetTriggerPlayerMetaKey()
        set udg__GUIKeyIsReleased = not udg__GUIKeyIsPressed

        // Trigger event(s)
        set udg_GUIKeyEvent = 0.0
        set udg_GUIKeyEvent = 1.0
        //set udg_GUIKeyEvent = 0.0
        return false
    endfunction
    
    private function RegisterKey takes nothing returns boolean
        local integer playerId
        local player p
        local integer metaKey
        local oskeytype key
        
        //ConvertOsKeyType starts at index 8 and ends at index 254 ($08 to $FE)
        set currentKey = currentKey + 1
        if (currentKey > CONVERT_OS_KEY_TYPE_INDEX_END) then
          // No more keys to register, finish.
          call DestroyTrigger(GetTriggeringTrigger())
          return false
        endif

        // Get the current oskey and register player key events
        set key = ConvertOsKeyType(currentKey)
        set playerId = 0
        loop
            // Only register oskey events for players that are active
            // Registers both keyDown/keyUp and all metaKey combinations.
            if (udg_GUIKeyEvent_S_EnablePlayer[playerId + 1]) then
                set p = Player(playerId)
                set metaKey = 0
                loop
                    call BlzTriggerRegisterPlayerKeyEvent(udg_GUIKeyEvent_System, p, key, metaKey, false)
                    call BlzTriggerRegisterPlayerKeyEvent(udg_GUIKeyEvent_System, p, key, metaKey, true)
                    set metaKey = metaKey + 1
                    exitwhen(metaKey > META_KEY_MAX)
                endloop
            endif
            
            set playerId = playerId + 1
            exitwhen(playerId >= bj_MAX_PLAYERS)
        endloop
        
        set p   = null
        set key = null
        
        // Run this trigger again, until all keys have been registered
        call TriggerEvaluate(GetTriggeringTrigger())
        return false
    endfunction

    private function InitGUIKeyNames takes nothing returns nothing
        set udg_GUIKEY_NAME[$08] = "BACKSPACE"
        set udg_GUIKEY_NAME[$09] = "TAB"
        set udg_GUIKEY_NAME[$0C] = "CLEAR"
        set udg_GUIKEY_NAME[$0D] = "RETURN"
        set udg_GUIKEY_NAME[$10] = "SHIFT"
        set udg_GUIKEY_NAME[$11] = "CONTROL"
        set udg_GUIKEY_NAME[$12] = "ALT"
        set udg_GUIKEY_NAME[$13] = "PAUSE"
        set udg_GUIKEY_NAME[$14] = "CAPSLOCK"
        set udg_GUIKEY_NAME[$15] = "KANA"
        set udg_GUIKEY_NAME[$15] = "HANGUL"
        set udg_GUIKEY_NAME[$17] = "JUNJA"
        set udg_GUIKEY_NAME[$18] = "FINAL"
        set udg_GUIKEY_NAME[$19] = "HANJA"
        set udg_GUIKEY_NAME[$19] = "KANJI"
        set udg_GUIKEY_NAME[$1B] = "ESCAPE"
        set udg_GUIKEY_NAME[$1C] = "CONVERT"
        set udg_GUIKEY_NAME[$1D] = "NONCONVERT"
        set udg_GUIKEY_NAME[$1E] = "ACCEPT"
        set udg_GUIKEY_NAME[$1F] = "MODECHANGE"
        set udg_GUIKEY_NAME[$20] = "SPACE"
        set udg_GUIKEY_NAME[$21] = "PAGEUP"
        set udg_GUIKEY_NAME[$22] = "PAGEDOWN"
        set udg_GUIKEY_NAME[$23] = "END"
        set udg_GUIKEY_NAME[$24] = "HOME"
        set udg_GUIKEY_NAME[$25] = "LEFT"
        set udg_GUIKEY_NAME[$26] = "UP"
        set udg_GUIKEY_NAME[$27] = "RIGHT"
        set udg_GUIKEY_NAME[$28] = "DOWN"
        set udg_GUIKEY_NAME[$29] = "SELECT"
        set udg_GUIKEY_NAME[$2A] = "PRINT"
        set udg_GUIKEY_NAME[$2B] = "EXECUTE"
        set udg_GUIKEY_NAME[$2C] = "PRINTSCREEN"
        set udg_GUIKEY_NAME[$2D] = "INSERT"
        set udg_GUIKEY_NAME[$2E] = "DELETE"
        set udg_GUIKEY_NAME[$2F] = "HELP"
        set udg_GUIKEY_NAME[$30] = "0"
        set udg_GUIKEY_NAME[$31] = "1"
        set udg_GUIKEY_NAME[$32] = "2"
        set udg_GUIKEY_NAME[$33] = "3"
        set udg_GUIKEY_NAME[$34] = "4"
        set udg_GUIKEY_NAME[$35] = "5"
        set udg_GUIKEY_NAME[$36] = "6"
        set udg_GUIKEY_NAME[$37] = "7"
        set udg_GUIKEY_NAME[$38] = "8"
        set udg_GUIKEY_NAME[$39] = "9"
        set udg_GUIKEY_NAME[$41] = "A"
        set udg_GUIKEY_NAME[$42] = "B"
        set udg_GUIKEY_NAME[$43] = "C"
        set udg_GUIKEY_NAME[$44] = "D"
        set udg_GUIKEY_NAME[$45] = "E"
        set udg_GUIKEY_NAME[$46] = "F"
        set udg_GUIKEY_NAME[$47] = "G"
        set udg_GUIKEY_NAME[$48] = "H"
        set udg_GUIKEY_NAME[$49] = "I"
        set udg_GUIKEY_NAME[$4A] = "J"
        set udg_GUIKEY_NAME[$4B] = "K"
        set udg_GUIKEY_NAME[$4C] = "L"
        set udg_GUIKEY_NAME[$4D] = "M"
        set udg_GUIKEY_NAME[$4E] = "N"
        set udg_GUIKEY_NAME[$4F] = "O"
        set udg_GUIKEY_NAME[$50] = "P"
        set udg_GUIKEY_NAME[$51] = "Q"
        set udg_GUIKEY_NAME[$52] = "R"
        set udg_GUIKEY_NAME[$53] = "S"
        set udg_GUIKEY_NAME[$54] = "T"
        set udg_GUIKEY_NAME[$55] = "U"
        set udg_GUIKEY_NAME[$56] = "V"
        set udg_GUIKEY_NAME[$57] = "W"
        set udg_GUIKEY_NAME[$58] = "X"
        set udg_GUIKEY_NAME[$59] = "Y"
        set udg_GUIKEY_NAME[$5A] = "Z"
        set udg_GUIKEY_NAME[$5B] = "LMETA"
        set udg_GUIKEY_NAME[$5C] = "RMETA"
        set udg_GUIKEY_NAME[$5D] = "APPS"
        set udg_GUIKEY_NAME[$5F] = "SLEEP"
        set udg_GUIKEY_NAME[$60] = "NUMPAD0"
        set udg_GUIKEY_NAME[$61] = "NUMPAD1"
        set udg_GUIKEY_NAME[$62] = "NUMPAD2"
        set udg_GUIKEY_NAME[$63] = "NUMPAD3"
        set udg_GUIKEY_NAME[$64] = "NUMPAD4"
        set udg_GUIKEY_NAME[$65] = "NUMPAD5"
        set udg_GUIKEY_NAME[$66] = "NUMPAD6"
        set udg_GUIKEY_NAME[$67] = "NUMPAD7"
        set udg_GUIKEY_NAME[$68] = "NUMPAD8"
        set udg_GUIKEY_NAME[$69] = "NUMPAD9"
        set udg_GUIKEY_NAME[$6A] = "MULTIPLY"
        set udg_GUIKEY_NAME[$6B] = "ADD"
        set udg_GUIKEY_NAME[$6C] = "SEPARATOR "
        set udg_GUIKEY_NAME[$6D] = "SUBTRACT"
        set udg_GUIKEY_NAME[$6E] = "DECIMAL"
        set udg_GUIKEY_NAME[$6F] = "DIVIDE"
        set udg_GUIKEY_NAME[$70] = "F1"
        set udg_GUIKEY_NAME[$71] = "F2"
        set udg_GUIKEY_NAME[$72] = "F3"
        set udg_GUIKEY_NAME[$73] = "F4"
        set udg_GUIKEY_NAME[$74] = "F5"
        set udg_GUIKEY_NAME[$75] = "F6"
        set udg_GUIKEY_NAME[$76] = "F7"
        set udg_GUIKEY_NAME[$77] = "F8"
        set udg_GUIKEY_NAME[$78] = "F9"
        set udg_GUIKEY_NAME[$79] = "F10"
        set udg_GUIKEY_NAME[$7A] = "F11"
        set udg_GUIKEY_NAME[$7B] = "F12"
        set udg_GUIKEY_NAME[$7C] = "F13"
        set udg_GUIKEY_NAME[$7D] = "F14"
        set udg_GUIKEY_NAME[$7E] = "F15"
        set udg_GUIKEY_NAME[$7F] = "F16"
        set udg_GUIKEY_NAME[$80] = "F17"
        set udg_GUIKEY_NAME[$81] = "F18"
        set udg_GUIKEY_NAME[$82] = "F19"
        set udg_GUIKEY_NAME[$83] = "F20"
        set udg_GUIKEY_NAME[$84] = "F21"
        set udg_GUIKEY_NAME[$85] = "F22"
        set udg_GUIKEY_NAME[$86] = "F23"
        set udg_GUIKEY_NAME[$87] = "F24"
        set udg_GUIKEY_NAME[$90] = "NUMLOCK"
        set udg_GUIKEY_NAME[$91] = "SCROLLLOCK"
        set udg_GUIKEY_NAME[$92] = "OEM_NEC_EQUAL"
        set udg_GUIKEY_NAME[$92] = "OEM_FJ_JISHO"
        set udg_GUIKEY_NAME[$93] = "OEM_FJ_MASSHOU"
        set udg_GUIKEY_NAME[$94] = "OEM_FJ_TOUROKU"
        set udg_GUIKEY_NAME[$95] = "OEM_FJ_LOYA"
        set udg_GUIKEY_NAME[$96] = "OEM_FJ_ROYA"
        set udg_GUIKEY_NAME[$A0] = "LSHIFT"
        set udg_GUIKEY_NAME[$A1] = "RSHIFT"
        set udg_GUIKEY_NAME[$A2] = "LCONTROL"
        set udg_GUIKEY_NAME[$A3] = "RCONTROL"
        set udg_GUIKEY_NAME[$A4] = "LALT"
        set udg_GUIKEY_NAME[$A5] = "RALT"
        set udg_GUIKEY_NAME[$A6] = "BROWSER_BACK"
        set udg_GUIKEY_NAME[$A7] = "BROWSER_FORWARD"
        set udg_GUIKEY_NAME[$A8] = "BROWSER_REFRESH"
        set udg_GUIKEY_NAME[$A9] = "BROWSER_STOP"
        set udg_GUIKEY_NAME[$AA] = "BROWSER_SEARCH"
        set udg_GUIKEY_NAME[$AB] = "BROWSER_FAVORITES"
        set udg_GUIKEY_NAME[$AC] = "BROWSER_HOME"
        set udg_GUIKEY_NAME[$AD] = "VOLUME_MUTE"
        set udg_GUIKEY_NAME[$AE] = "VOLUME_DOWN"
        set udg_GUIKEY_NAME[$AF] = "VOLUME_UP"
        set udg_GUIKEY_NAME[$B0] = "MEDIA_NEXT_TRACK"
        set udg_GUIKEY_NAME[$B1] = "MEDIA_PREV_TRACK"
        set udg_GUIKEY_NAME[$B2] = "MEDIA_STOP"
        set udg_GUIKEY_NAME[$B3] = "MEDIA_PLAY_PAUSE"
        set udg_GUIKEY_NAME[$B4] = "LAUNCH_MAIL"
        set udg_GUIKEY_NAME[$B5] = "LAUNCH_MEDIA_SELECT"
        set udg_GUIKEY_NAME[$B6] = "LAUNCH_APP1"
        set udg_GUIKEY_NAME[$B7] = "LAUNCH_APP2" 
        set udg_GUIKEY_NAME[$BA] = "OEM_1"
        set udg_GUIKEY_NAME[$BB] = "OEM_PLUS"
        set udg_GUIKEY_NAME[$BC] = "OEM_COMMA"
        set udg_GUIKEY_NAME[$BD] = "OEM_MINUS"
        set udg_GUIKEY_NAME[$BE] = "OEM_PERIOD"
        set udg_GUIKEY_NAME[$BF] = "OEM_2"
        set udg_GUIKEY_NAME[$C0] = "OEM_3"
        set udg_GUIKEY_NAME[$DB] = "OEM_4"
        set udg_GUIKEY_NAME[$DC] = "OEM_5"
        set udg_GUIKEY_NAME[$DD] = "OEM_6"
        set udg_GUIKEY_NAME[$DE] = "OEM_7"
        set udg_GUIKEY_NAME[$DF] = "OEM_8"
        set udg_GUIKEY_NAME[$E1] = "OEM_AX"
        set udg_GUIKEY_NAME[$E2] = "OEM_102"
        set udg_GUIKEY_NAME[$E3] = "ICO_HELP"
        set udg_GUIKEY_NAME[$E4] = "ICO_00"
        set udg_GUIKEY_NAME[$E5] = "PROCESSKEY"
        set udg_GUIKEY_NAME[$E6] = "ICO_CLEAR"
        set udg_GUIKEY_NAME[$E7] = "PACKET"
        set udg_GUIKEY_NAME[$E9] = "OEM_RESET"
        set udg_GUIKEY_NAME[$EA] = "OEM_JUMP"
        set udg_GUIKEY_NAME[$EB] = "OEM_PA1"
        set udg_GUIKEY_NAME[$EC] = "OEM_PA2"
        set udg_GUIKEY_NAME[$ED] = "OEM_PA3"
        set udg_GUIKEY_NAME[$EE] = "OEM_WSCTRL"
        set udg_GUIKEY_NAME[$EF] = "OEM_CUSEL"
        set udg_GUIKEY_NAME[$F0] = "OEM_ATTN"
        set udg_GUIKEY_NAME[$F1] = "OEM_FINISH"
        set udg_GUIKEY_NAME[$F2] = "OEM_COPY"
        set udg_GUIKEY_NAME[$F3] = "OEM_AUTO"
        set udg_GUIKEY_NAME[$F4] = "OEM_ENLW"
        set udg_GUIKEY_NAME[$F5] = "OEM_BACKTAB"
        set udg_GUIKEY_NAME[$F6] = "ATTN"
        set udg_GUIKEY_NAME[$F7] = "CRSEL"
        set udg_GUIKEY_NAME[$F8] = "EXSEL"
        set udg_GUIKEY_NAME[$F9] = "EREOF"
        set udg_GUIKEY_NAME[$FA] = "PLAY"
        set udg_GUIKEY_NAME[$FB] = "ZOOM"
        set udg_GUIKEY_NAME[$FC] = "NONAME"
        set udg_GUIKEY_NAME[$FD] = "PA1"
        set udg_GUIKEY_NAME[$FE] = "OEM_CLEAR"
    endfunction
    
    public function GUIKeyEventInit takes nothing returns nothing
        local trigger t
        
        // Create the trigger that handles oskey input from players
        set udg_GUIKeyEvent_System = CreateTrigger()
        call TriggerAddCondition(udg_GUIKeyEvent_System, Condition(function OnGUIKeyEvent))

        // Initializes the GUIKey Name array
        call InitGUIKeyNames()

        // Register oskey events for players
        // Run each key as a new thread to avoid op-limit (but shouldn't be an issue most likely anyway).
        // Results in ~100k operations for 24 players
        set currentKey = CONVERT_OS_KEY_TYPE_INDEX_START - 1
        set t = CreateTrigger()
        call TriggerAddCondition(t, Condition(function RegisterKey))
        call TriggerEvaluate(t)
        set t = null
    endfunction
endlibrary
 
Top