Computer Science Canada

Input with Input.KeyDown is too fast?

Author:  NikG [ Mon May 08, 2006 11:37 pm ]
Post subject:  Input with Input.KeyDown is too fast?

Hey all, I got a little problem, demostrated by the sample code below:
code:
setscreen ("offscreenonly")
var chars : array char of boolean
var a := ""
loop
    Input.KeyDown (chars)
    if chars ('1') then
        if a = "a" then
            a := "b"
        else
            a := "a"
        end if
    end if
    put a
    View.Update
    %delay (100)
    cls
end loop

What I want is that when the user presses '1' the first time, the value of the variable should change to 'a' and after the 2nd press, it should turn to 'b'. If you uncomment that delay, this works. But I don't want to use a delay of any sort.

Although I don't want to do this, I've alse tried adding a boolean that keeps track of keypresses (i.e. if chars ('1') and keydown=false then) but that doesn't work reliably without a delay either.

I'm guessing there's no way to do this without a delay.
Any ideas?

Author:  Bobrobyn [ Tue May 09, 2006 7:38 am ]
Post subject: 

Try changing your code so that it's not using the same button for the same thing.

For example:

code:
setscreen ("offscreenonly")
var chars : array char of boolean
var a := ""
loop
    Input.KeyDown (chars)
    if chars ('1') then
        a := "b"
    end if
    if chars ('2') then
        a := "a"
    end if
    locate(1,1)
    put a
    View.Update
end loop
[/code]

Author:  jamonathin [ Tue May 09, 2006 9:12 am ]
Post subject: 

You could turn it into 2 buttons, or you ca make a fake delay.

Basically taking the time when the '1' was pressed and waiting 'x' amount of time until you are allowed to change it again.

The 'x' amount in this example is 100. I took out your View.Update because it was unneccesary and slowed things down major.

code:
var chars : array char of boolean
var a := ""
var last : real := 0
loop
    Input.KeyDown (chars)
    if chars ('1') and Time.Elapsed - last >= 100 then %if time between is 100 milliseconds or longer
        last := Time.Elapsed %last time button was pressed
        if a = "a" then
            a := "b"
        else
            a := "a"
        end if
    end if
    locate (1, 1)
    put a ..
end loop

Author:  HellblazerX [ Tue May 09, 2006 4:15 pm ]
Post subject: 

actually, I think what NikG is asking for is how do you make it so the user must release the key and press it again to change the value, so the user can't hold down the 1 key and the value will constantly change back and forth.

Your original method of using a boolean flag would work nicely in this case:

code:
setscreen ("offscreenonly")
var chars : array char of boolean
var a := ""
var pressed : boolean := false
loop
    Input.KeyDown (chars)
    if chars ('1') then
        %Checks if the user has let go of the key yet
        if pressed = false then
            if a = "a" then
                a := "b"
            else
                a := "a"
            end if
            pressed := true %Sets pressed to true
        end if
    else
        %If user is not pressing on the key, then pressed becomes false
        pressed := false
    end if
    put a
    View.Update
    cls
end loop


Note that the pressed condition is in its own separate if construct, otherwise it won't work properly.

Author:  NikG [ Tue May 09, 2006 9:37 pm ]
Post subject: 

Bobrobyn: I need it to be one key, not two.
jamonathin: I don't want a delay of any sort.

HellblazerX: Thanks, that works, although I was hoping to do it without having to create another variable.

Thanks guys.


: