//===========================================================================
//
// 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