Page 1 of 1

How does one pause a program using KEY ?

PostPosted: 08 Apr 2017 09:52
by bobgillies
Hi Franck, it's been awhile since I was last here and I'm stuck trying to pause a loop and wait for a key to be pressed.

I used import: console to import the #key. I see it's a method of the console class and I'm still a little fuzzy on how it can be used within my class of objects.

As you well know, in ANSI FORTH the word "KEY" waits for a key press and returns it's ascii value. How would you setup oforth's #key to work the same way?

Code: Select all
import: console

: wait1s \ -- x | null   1000000 System.Console receiveTimeout ;

: foo  doWhile: [ 7 emit  wait1s dup key 27 == if drop false else drop true then ] ;

I always get an integer 27 does not understand #key.

Re: How does one pause a program using KEY ?

PostPosted: 09 Apr 2017 10:57
by Franck

How are you ?

If you want to wait in a blocking way for a key, just use :

Code: Select all
System.Console key

If you want the word KEY as in Forth :

Code: Select all
: KEY    System.Console key ;

To wait a key for 1 second, then do something else if no key is pressed :

Code: Select all
1000000 Syste.Console receiveTimeout

This will return the key or null if no key is pressed during 1 second.

Now if you just want to wait for 1 second, no matter a key is pressed or not :

Code: Select all
1000 sleep

To wait until the escape key is pushed (with KEY defined) :

Code: Select all
: foo
   doWhile: [ KEY 27 == not ] ;


Re: How does one pause a program using KEY ?

PostPosted: 13 Apr 2017 04:37
by bobgillies
Greetings Franck, I'm good. Thank you for providing me with #key function usage. I needed to single step a section of the 8 Queens Solver code where I'm getting incorrect backtracking results causing an incorrect tally of solutions.