REM ***********************************************
REM Title: Last Key Press
REM Author: Phaelax
REM Downloaded from: http://dbcc.zimnox.com/
REM ***********************************************
 
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