How does one pause a program using KEY ?

If you have any questions, remarks, ... if you need help... its here...

How does one pause a program using KEY ?

Postby bobgillies » 08 Apr 2017 09:52

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?

Example:
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.
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26

Re: How does one pause a program using KEY ?

Postby Franck » 09 Apr 2017 10:57

Hi,

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 ] ;


Bye
Franck
Franck
 
Posts: 140
Joined: 29 Oct 2014 19:01

Re: How does one pause a program using KEY ?

Postby bobgillies » 13 Apr 2017 04:37

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.

Cheers,
Bob
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron