Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Keyboard - if pressed as opposed to if down???
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xHoly-Divinity




PostPosted: Thu Nov 02, 2006 8:50 pm   Post subject: Keyboard - if pressed as opposed to if down???

Is there a command in turing that will just get when a button is pressed? E.g. if i press and hold k, using Input.KeyDown, u would get kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk. Now, what im wondering if i can do is if i press and hold k it only does it once until it is pressed again. Thanks Smile
Sponsor
Sponsor
Sponsor
sponsor
Windsurfer




PostPosted: Thu Nov 02, 2006 8:55 pm   Post subject: (No subject)

Use an array of booleans and test to see if the position of the key changed since the last time you checked.
Eg. At the start, the whole array would be false.
Start Loop.
Check all keys.
If one key is pressed down AND the corresponding boolean is false, turn the boolean to true and do a command.
If a key is not pressed AND the boolean is true, turn the boolean to false.
Loop back.
ericfourfour




PostPosted: Thu Nov 02, 2006 9:05 pm   Post subject: (No subject)

I think you are trying to ask if you can stop the program from checking if 'k' is pressed until the 'k' is released. In that case you would simply use a boolean variable. First you would check if k is down and if it is you would check if the k was down last cycle (loop) by checking the boolean variable. If it was not down the previous cycle then you would do the 'k' down stuff. If 'k' was not down you would set the boolean to say that in this cycle the 'k' was not down.

code:

boolean k_down = false
loop
     get keyboard status
     if 'k' is down then
          if not k_down then
               do k down stuff
               k_down = true
     else
          k_down = false


Or you could use Input.hasch (I think that is it). If it returns true then there is a key in the input buffer. After that call Input.getchar which returns a string that is the current input. Or you can use Input.getch which returns a string with a length of 1.

code:

var input : char
loop
     if Input.hasch then
          input = Input.getchar
     end if
end loop
xHoly-Divinity




PostPosted: Thu Nov 02, 2006 9:10 pm   Post subject: (No subject)

Hmmm.... windsurfer that doesn't seem efficient cuzz look

code:

var check : boolean := true
var chars : array char of boolean
loop
Input.KeyDown (chars)
if chars (' ') and check = true then
check := false
put "HELLO!"
end if
check := true
end loop


That still doesn't change it, it processes too quickly. Unless that's not exactly what u mean Question
xHoly-Divinity




PostPosted: Thu Nov 02, 2006 9:14 pm   Post subject: (No subject)

Ooooo, u actually brought me upon another command. Input.Flush, i think that's what im lookin for Razz
Windsurfer




PostPosted: Thu Nov 02, 2006 9:19 pm   Post subject: (No subject)

Perhaps you need to re-read my English code...
This is what i meant, sort of, in terms of your code, in Turing code:

code:
var check : boolean := true
var chars : array char of boolean
loop
    Input.KeyDown (chars)
    if chars (' ') and check = true then
        check := false
        put "HELLO!"
    elsif not chars (' ') and check = false then
        check := true
    end if
end loop
ericfourfour




PostPosted: Thu Nov 02, 2006 9:25 pm   Post subject: (No subject)

windsurfer??? I don't see anyone named windsurfer in this thread.

Anyway, I guess I didn't explain it well enough. I took the time to translate the code to Turing from pseudo.

code:

var down : boolean := false
var chars : array char of boolean
loop
    Input.KeyDown (chars)
    if chars ('k') then
        if not down then
            put "k was pressed"
            down := true
        end if
    else
        down := false
    end if
end loop


And I realized the second one wasn't made that well either. It really should look like this:

code:

var down : boolean := false
var input : char
loop
    if Input.hasch () then
        if not down then
            input := Input.getchar ()
            put "You entered ", input
            down := true
        end if
    else
        down := false
    end if
end loop
ericfourfour




PostPosted: Thu Nov 02, 2006 9:26 pm   Post subject: (No subject)

Haha. I didn't even see that windsurfer was in this thread.
Sponsor
Sponsor
Sponsor
sponsor
Windsurfer




PostPosted: Thu Nov 02, 2006 9:30 pm   Post subject: (No subject)

And yes, you got the idea in your code. Very nicely done. Except... ah never mind, i never believed in those teaching principles anyways.
xHoly-Divinity




PostPosted: Thu Nov 02, 2006 9:31 pm   Post subject: (No subject)

Nicely done boys, that was quite helpful Very Happy
bruized




PostPosted: Fri Nov 03, 2006 3:46 pm   Post subject: (No subject)

Well, I actually had the same question to ask and was going to until I saw this. The thing is I'm not sure I get the gist of what you guys are saying. Well I do but I don't understand how it works like that in the code. I was wondering if you could explain it a little more.
ericfourfour




PostPosted: Fri Nov 03, 2006 3:59 pm   Post subject: (No subject)

My second example is the easiest to implement. I'll add comments beside what is going on.

code:

var down : boolean := false %Whether there was a key down in the last frame (loop).
var input : char %The character input by the user.
loop
    if Input.hasch () then %Checks if a key is currently being pressed
        if not down then %Checks if there were no keys down last frame
            input := Input.getchar () %Get the input
            put "You entered ", input
            down := true %Now the program knows that a key was down this frame.
        end if
    else %Checks if there were no keys pressed this frame
        down := false %Now the program knows no keys were down this frame
    end if
end loop
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 12 Posts ]
Jump to:   


Style:  
Search: