Computer Science Canada

getch & hasch

Author:  appling [ Sat Nov 22, 2003 4:12 pm ]
Post subject:  getch & hasch

i am worried about my homework. i don't know getch & hasch clearly.So is there anyone can help me??
how to use getch & hasch.

Author:  Tony [ Sat Nov 22, 2003 4:31 pm ]
Post subject: 

getch grabs a single character from the input buffer.
code:

var c:string(1)
getch(c)
put c


hasch returns true if there's anything in input buffer waiting to be processed. It is generally used to skip getch so that program continues even without user's input.

code:

var c:string(1)

loop
if hasch then
    getch(c)
    locate(1,1)
    put "you've pressed ",c
    delay(1000)
end if
locate(1,1)
put "waiting..."
end loop

Author:  Mazer [ Sat Nov 22, 2003 4:33 pm ]
Post subject: 

use getch to get a character. like this:
code:

var character : string (1) %<- the (1) is important

getch (character)


use hasch to see if a character has been pressed. like this:
code:

loop
    exit when hasch
    put "wheeeee loop!"
end loop

put "aack! you pressed a character!"

Author:  appling [ Sat Nov 22, 2003 4:44 pm ]
Post subject: 

so,if i have a graphics program that have 2 scene, and i want to change the scene A to scene B immediately by pressing the keyboard, also later change the scene B to scene A immediately by pressing the keyboard.

what can i do??

Author:  Tony [ Sat Nov 22, 2003 6:09 pm ]
Post subject: 

you can do
code:

var c:string(1)

loop
if hasch
%key has been pressed
getch(c) %clears the buffer

drawScene2
end if
end loop

Author:  appling [ Sat Nov 22, 2003 9:37 pm ]
Post subject: 

but how to change 2 to 1 then

Author:  Tony [ Sat Nov 22, 2003 9:46 pm ]
Post subject: 

you exit your loop and start another one, this time waiting to draw scene1

Author:  Blade [ Sat Nov 22, 2003 10:23 pm ]
Post subject: 

you could also just use one loop and use a boolean var..

code:
var switch:boolean:=false
var c:string(1)

loop
  if hasch
    %key has been pressed
    getch(c) %clears the buffer

    if switch then
      drawScene2
      switch:=false
    else
       drawScene1
      switch:=true
    end if
   
  end if
end loop

Author:  appling [ Sat Nov 22, 2003 11:15 pm ]
Post subject: 

thanks i get it


: