Not sure if this is the most efficient code in the world, but it gets the job done. If you press two or more keys and then release one, getRawLastKey() will still only return the last one 'pressed', even if its been released and other keys are still being pressed down. This aims to correct that, and if no keys are currently pressed will return 0.
If you press
A then
B, the function will return the key code for
B. If you release
B while still holding
A, then it will return the code for
A instead rather keep returning
B like getRawLastKey() would.
This will remember the last 4 keys pressed, but can easily be changed to support more if needed.
global stack_index = 0
dim keyStack[4]
do
//right
if GetRawKeyState(39) = 1 and getLastKeyPress() = 39
print("Right")
endif
//left
if GetRawKeyState(37) = 1 and getLastKeyPress() = 37
print("Left")
endif
//up
if GetRawKeyState(38) = 1 and getLastKeyPress() = 38
print("Up")
endif
//down
if GetRawKeyState(40) = 1 and getLastKeyPress() = 40
print("Down")
endif
print("+++++++++++++++++")
k = getrawlastkey()
if k <> oldK and getRawKeyState(k) = 1
oldK = k
addToKeyStack(k)
endif
if getRawKeyReleased(k) = 1
oldK = 0
endif
// monitor key releases
for i = 1 to 4
if getRawKeyReleased(keyStack[i]) = 1
removeFromKeystack(i)
dec i
endif
next i
print("Last Key Press: "+str(getLastKeyPress()))
sync()
loop
function getLastKeyPress()
k = keyStack[stack_index]
endfunction k
function removeFromKeystack(i)
for j = i to 3
keyStack[j] = keyStack[j+1]
next j
keyStack[4] = 0
dec stack_index
endfunction
function addToKeyStack(k)
inc stack_index
if stack_index > 4
for i = 1 to 3
keyStack[i] = keyStack[i+1]
next i
stack_index = 4
else
keyStack[stack_index] = k
endif
endfunction
"You're all wrong. You're all idiots." ~Fluffy Rabbit