- Joined
- Aug 3, 2008
- Messages
- 2,345
This is my Arrow Key Detection system I have created as part of an arrow key movement system. I thought other people may have a use for it, so here it is!
It basically extends on Blizzard's arrow key down and up events, allowing you to query if a player is pressing an arrow key. A known bug is that it can't detect when a key is lifted while in any menu, but I can't see any way around this so it'll have to do
This system works around one function:
I don't believe there are any leaks as I use no local handles and my only Global variable is an array whose elements are only ever set once.
Anyway, the code:
PS: I will convert it to normal JASS if you want, but I figured using a library with private functions was easier and most systems and spells which implement this system will probably use vJASS anyway.
It basically extends on Blizzard's arrow key down and up events, allowing you to query if a player is pressing an arrow key. A known bug is that it can't detect when a key is lifted while in any menu, but I can't see any way around this so it'll have to do

This system works around one function:
function IsKeyDown takes integer key, player id returns boolean
, and I have made constant variables to support it:
JASS:
constant integer ARROW_LEFT = 0
constant integer ARROW_RIGHT = 1
constant integer ARROW_DOWN = 2
constant integer ARROW_UP = 3
I don't believe there are any leaks as I use no local handles and my only Global variable is an array whose elements are only ever set once.
v1.0.1
This version actually works.
This version actually works.
Anyway, the code:
JASS:
library KeyDetection initializer Init
globals
private trigger array ArrowDetectors
private boolean array KeyDown
private constant integer MAX_NUMBER_OF_PLAYERS = 4
constant integer ARROW_LEFT = 0
constant integer ARROW_RIGHT = 1
constant integer ARROW_DOWN = 2
constant integer ARROW_UP = 3
endglobals
function IsKeyDown takes integer key, player id returns boolean
return KeyDown[MAX_NUMBER_OF_PLAYERS*key+GetPlayerId(id)]
endfunction
private function GetKeyEventDown takes integer i returns playerevent
if i == 0 then
return EVENT_PLAYER_ARROW_LEFT_DOWN
elseif i == 1 then
return EVENT_PLAYER_ARROW_RIGHT_DOWN
elseif i == 2 then
return EVENT_PLAYER_ARROW_DOWN_DOWN
elseif i == 3 then
return EVENT_PLAYER_ARROW_UP_DOWN
endif
return null
endfunction
private function GetKeyEventUp takes integer i returns playerevent
if i == 0 then
return EVENT_PLAYER_ARROW_LEFT_UP
elseif i == 1 then
return EVENT_PLAYER_ARROW_RIGHT_UP
elseif i == 2 then
return EVENT_PLAYER_ARROW_DOWN_UP
elseif i == 3 then
return EVENT_PLAYER_ARROW_UP_UP
endif
return null
endfunction
private function LeftDown takes nothing returns nothing
set KeyDown[MAX_NUMBER_OF_PLAYERS*0+GetPlayerId(GetTriggerPlayer())] = true
endfunction
private function LeftUp takes nothing returns nothing
set KeyDown[MAX_NUMBER_OF_PLAYERS*0+GetPlayerId(GetTriggerPlayer())] = false
endfunction
private function RightDown takes nothing returns nothing
set KeyDown[MAX_NUMBER_OF_PLAYERS*1+GetPlayerId(GetTriggerPlayer())] = true
endfunction
private function RightUp takes nothing returns nothing
set KeyDown[MAX_NUMBER_OF_PLAYERS*1+GetPlayerId(GetTriggerPlayer())] = false
endfunction
private function DownDown takes nothing returns nothing
set KeyDown[MAX_NUMBER_OF_PLAYERS*2+GetPlayerId(GetTriggerPlayer())] = true
endfunction
private function DownUp takes nothing returns nothing
set KeyDown[MAX_NUMBER_OF_PLAYERS*2+GetPlayerId(GetTriggerPlayer())] = false
endfunction
private function UpDown takes nothing returns nothing
set KeyDown[MAX_NUMBER_OF_PLAYERS*3+GetPlayerId(GetTriggerPlayer())] = true
endfunction
private function UpUp takes nothing returns nothing
set KeyDown[MAX_NUMBER_OF_PLAYERS*3+GetPlayerId(GetTriggerPlayer())] = false
endfunction
private function GetActionFunc takes integer i returns code
if i == 0 then
return function LeftDown
elseif i == 1 then
return function RightDown
elseif i == 2 then
return function DownDown
elseif i == 3 then
return function UpDown
elseif i == 4 then
return function LeftUp
elseif i == 5 then
return function RightUp
elseif i == 6 then
return function DownUp
elseif i == 7 then
return function UpUp
endif
return null
endfunction
//===========================================================================
private function Init takes nothing returns nothing
local integer i = 0
local integer i2 = 0
loop
exitwhen i >= 4
set ArrowDetectors[i] = CreateTrigger()
set ArrowDetectors[i+4] = CreateTrigger()
loop
exitwhen i2 >= MAX_NUMBER_OF_PLAYERS
call TriggerRegisterPlayerEvent(ArrowDetectors[i], Player(i2), GetKeyEventDown(i))
call TriggerRegisterPlayerEvent(ArrowDetectors[i+4], Player(i2), GetKeyEventUp(i))
set i2 = i2 + 1
endloop
set i2 = 0
call TriggerAddAction(ArrowDetectors[i], GetActionFunc(i))
call TriggerAddAction(ArrowDetectors[i+4], GetActionFunc(i+4))
set i = i + 1
endloop
endfunction
endlibrary
Last edited: