Iterate until keypress

39 views
Skip to first unread message

furfante

unread,
Apr 3, 2012, 12:45:50 PM4/3/12
to xblite
Hi, Folks,

Disclaimer:
I am new to this forum, I am new to xblite, and it will be come
rapidly obvious that I am not a programmer.

Having said that, however, I have the following problem I can't seem
to solve.

I need to iterate through a function or process or whatever you want
to call it until the user presses any key or a specific key. It
doesn't really matter which key but it probably needs to be just one
key as opposed to a key and Enter. Just pressing Enter or the Space
bar would be ideal. When the key is pressed I need to preserve some
information and start iterating again.

Here is a code snippet from an old basic program I found on the web.

220 FOR D = 0 TO 36
230 IF INKEY$ = "" THEN T = T + 1: GOTO 230
240 C(D) = T: T = 0
250 NEXT D

I tried to find some way to do it in Perl but it seemed way too
complicated. I tried Perl first because I have written a couple of
programs in Perl. When I couldn't find a way to do it easily in Perl
I downloaded xblite hoping that it would be easier.

I have looked at the documentation and also the xblite Book of
Knowledge (which has been very helpful) but I can't find a substitute
for INKEY$. I tried INLINE$ but can't figure out how to evaluate it
and continue to iterate. It always stops and waits for the Enter
key. I suspect this is either very simple or damn near impossible.
Any ideas?

Regards,
Wayne

gentsky

unread,
Apr 3, 2012, 4:24:10 PM4/3/12
to xblite
Hi Wayne,

If you are writing a simple console program the you can use the
XioInkey() function. This is documented in the help file under Console
IO Library Functions (Xio).

It checks for any key press but does not wait for one. If no key is
pressed it returns 0 (False)
If any key is pressed it returns it reurns the keycode.

You can cut and paste the following short demo. to experiment

Note you need to IMPORT "xio" to use the console function library
"xsx" is also IMPORTED to use the XstSleep() function


' ####################
' ##### PROLOG #####
' ####################
'
' A console program template
'
VERSION "0.0001"
CONSOLE
'
IMPORT "xst" ' Standard library : required by most programs
IMPORT "xio"
IMPORT "xsx" ' Extended standard library


'
DECLARE FUNCTION Entry ()
'
'
' ######################
' ##### Entry () #####
' ######################
'
FUNCTION Entry ()

FOR i = 1 TO 1000
PRINT i
XstSleep(500)
key = XioInkey()
IF key THEN
PRINT "Keycode "; key; " was pressed"
EXIT FOR
ENDIF
NEXT i

a$ = INLINE$ ("Press Enter to quit >")

Hope this helps

Alan

END FUNCTION
END PROGRAM

David Szafranski

unread,
Apr 3, 2012, 4:25:19 PM4/3/12
to xbl...@googlegroups.com
There is a Xio library function called XioInkey() which should do the job.

The XioInkey function returns a character key code when a keyboard key has been pressed. It does not wait for a keyboard event. If there is no pending key in the input buffer, then program execution continues after XioInkey() has checked for a keystroke.

keyCode = XioInkey ()

Make sure you IMPORT "xio" in the PROLOG section of your program. There is
a console demo program which shows how to use this function called inkey.x.
Look in the folder \xblite\demo\console\inkey.

D.



--
You received this message because you are subscribed to the Google Groups "xblite" group.
To post to this group, send email to xbl...@googlegroups.com.
To unsubscribe from this group, send email to xblite+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/xblite?hl=en.


gentsky

unread,
Apr 3, 2012, 4:30:36 PM4/3/12
to xblite
Hi again,

Somehow the END FUNCTION and END PROGRAM statements have got
misplaced.
They need to go after the A$ = INLINE$ ..... before my signoff.

Alan

furfante

unread,
Apr 4, 2012, 8:37:42 AM4/4/12
to xbl...@googlegroups.com

Thanks, guys.  

I actually played around with  the XioInKey yesterday after looking at the inkey.x demo that is referenced in the documentation.  Unfortunately, I have another question.  The documentation says: "If there is no pending key in the input buffer, then program execution continues after XioInkey() has checked for a keystroke."  What clears the input buffer and when?  Does the buffer clear automatically after the line "key = XioInKey" so that the next time through, without a key press, IF is FALSE?  I am confused about how to control the value of "key" and manage the input buffer etc.

Thanks again for your help.

Regards,
Wayne

furfante

unread,
Apr 4, 2012, 9:03:03 AM4/4/12
to xbl...@googlegroups.com

This is possibly out to scope for this group but all of a sudden I am getting a Windows message inkeytest.exe has encountered a problem and needs to close.  Naturally, Windows is sorry for the inconvenience.  Did I break something?  

Let me know when I become annoying.

furfante

unread,
Apr 4, 2012, 2:00:44 PM4/4/12
to xbl...@googlegroups.com
Gentlemen,

Please ignore my last question.  It turns out it was an array counter thing I didn't understand.  I think I have the hard part licked.  This is a lot easier than Perl.  I would still like to know how to think about the buffer issue if you have a chance.  Also, when I get a compile error it has a number at the end, can I use the number to better understand the error or do I just have to search the program visually.

Regards,
Wayne

David Szafranski

unread,
Apr 4, 2012, 2:41:14 PM4/4/12
to xbl...@googlegroups.com
If there is no character in the input buffer, XioInkey returns zero. You don't need
to worry about the buffer at all.

But XioInkey needs to be inside a loop so that the input buffer is continuously monitored.
I guess the function description is badly worded, since your program loop will continue no
matter the return value from XioInkey.

D.

--
You received this message because you are subscribed to the Google Groups "xblite" group.
To view this discussion on the web visit https://groups.google.com/d/msg/xblite/-/LGwn4YrcvMYJ.

David Szafranski

unread,
Apr 4, 2012, 3:10:53 PM4/4/12
to xbl...@googlegroups.com
 Also, when I get a compile error it has a number at the end, can I use the number to better understand the error or do I just have to search the program visually.

Often, the compiling errors will give some kind of code, an explanation, and
a line number where the error occurred. In the code editor, the output console at the bottom shows the results/errors of the compilation process:

D:\temp\Untitled1.x(35): Too Few Arguments(7)

You can double click this line to go to the offending line in your program, eg, line 7.

The above error was caused by this line:

a[0] =

Hope this helps.

D.

D.

unread,
Apr 4, 2012, 3:41:29 PM4/4/12
to xbl...@googlegroups.com
A correction, the compiler output displays the line number (35) and the character position (7) for the error:


D:\temp\Untitled1.x(35): Too Few Arguments(7)

D.

furfante

unread,
Apr 4, 2012, 6:56:00 PM4/4/12
to xbl...@googlegroups.com
Thanks - that was extremely helpful.

furfante

unread,
Apr 7, 2012, 8:33:50 AM4/7/12
to xbl...@googlegroups.com
Gentlemen,

Sorry to bother you again but a last difficult problem remains.  The good news is that, with your kind assistance, I was able to get everything in my program to work.  I struggled with some simple arithmetic for quite a while but now it is working fine.  The last step is to write out 2 files.  One needs to be suitable for reading on the screen  or printing.  The other needs to be a csv file for importing into XL.  I have only been trying to get the txt file to write so far.  I end up with an output file but I can't get the numbers to appear as numbers and I can 't get new lines.  I'm convinced the number problem is my not understanding how/where/why to use Type Suffixes.  For the new line problem I fooled around with \n and char(13) with no success.  If the Demo programs showed this there were just too many things going on for me to fathom the parts I was interested in.

Here is the part of the program where I try and write the lines to the output file:

'NOW I NEED TO WRITE OUT THE DEVIATION TABLE IN PRINTABLE FORM
PrintFile$ = "printtable.txt" ' A PRINTABLE DEVIATION TABLE
PrintFile = OPEN (PrintFile$, $$WRNEW)

FOR i=1 TO 4
c=i*90  
d=c    +DevTab [i]
String$=" on the compass is actually "
PRINT "c ="c "  String ="String$ "  d ="d      'THIS IS FOR DEBUGGING PURPOSES ONLY
WRITE [PrintFile], c, String$, d
NEXT i
CLOSE (PrintFile) 

As I say, I am able to get a new printfile.txt each run but the numbers are not numbers and no line breaks.  Any assistance would be appreciated.

One last thing.  Can you point me to a primmer designed for non-technical types that will help me understand this Type Suffix business.

gentsky

unread,
Apr 7, 2012, 4:13:55 PM4/7/12
to xblite
Hi Wayne,

The WRITE intrinsic sends the internal representation of a numeric
argument which is binary.
If you want to send a string in the same way as you would PRINT it
then you need to produce this string yourself.
This can be done with the STRING() function. You can also set up a
newline as a string and then WRITE this

c$ = STRING(c)
d$ = STRING(d)
newline$ = "\n"


Your output would then be:

WRITE [PrintFile], c$, String$, d$, newline$




When you want to output CSV try this

comma$ = ","

WRITE [PrintFile], c$, comma$, String$, comma$, d$, newline$

Hope this helps,


Alan

Ken

unread,
Apr 8, 2012, 9:50:02 AM4/8/12
to xblite
Or, you can just use the PRINT statement:

PRINT [PrintFile], c, String$, d

Ken

furfante

unread,
Apr 8, 2012, 9:09:33 PM4/8/12
to xbl...@googlegroups.com
Thanks.

I tried both solutions and they both work equally well.  I get the numbers printed in readable form.  However, I still can't get the line returns to work in the .txt file.  If I copy the text into a Word doc I get paragraph breaks.  Any last suggestions?

Here is the code I am running:

FOR i=1 TO 4
c=i*90  'changed this from 10 to 90

d=c    +DevTab [i]
String$=" on the compass is actually  "
c$=STRING (c)
d$ = STRING (d)
newline$ = "\n" 
PRINT "c ="c$ "  String ="String$ "  d ="d$
WRITE [PrintFile], c$, String$, d$, newline$
'PRINT [PrintFile], c, String$, d
NEXT i
CLOSE (PrintFile)


I also have a general question.  In the statement c$=STRING (c) what makes "c" a string?  Is it the STRING(c) or the c$.  The way I am thinking about this is that whatever is on the left off = is the name and it "means" whatever is on the right of the =.  When the name is c$ does it make anything it refers to a string?  If you make something a string with STRING(c) doesn't it remain a string when named simply "c"?  I find this stuff very confusing.  Can you suggest a reference - something for non programming types?

Regards,
Wayne

David Szafranski

unread,
Apr 8, 2012, 10:29:17 PM4/8/12
to xbl...@googlegroups.com
I would suggest using

newline$ = "\r\n"

And I would place this line of code outside of your loop since
you only need to calculate it once.

You could also make this into a constant by placing this into your
prolog part of your code:

$$CRLF = "\r\n"

As far as the line c$=STRING (c) goes, the variable c is already
defined in your program and has a value. This statement does not
change anything about c.

The conversion instrinsic function STRING is used to convert the contents
of the integer variable c into a string and assign it to the string variable c$.

For all variables, whether they are numbers or strings, you need to
assign a value to them. For strings, you can assign a value to it
by using the enclosing quote characters, or by using a string conversion
intrinsic function:

c = 100
a$ = "my string"
a$ = STRING(100)
a$ = STRING(c)

D.



--

Guy

unread,
Apr 9, 2012, 5:02:09 AM4/9/12
to xbl...@googlegroups.com
Hi Xbliter,

In "Windowsland", a newline flag is a sequence of "carriage return" +
"line feed".
For this purpose, D. has recently defined the global constant $$CRLF$,
which is replaced at compile trime by the string "\r\n".

I prefer PRINT over WRITE because it nicely formats numeric variables,
with the only 2 (small) problems that:
1. when PRINTing a positive number, it replaces a + sign by a space,
2. when PRINTing a newline, it sends only \n as a newline instead of the
Windows sequence "\r\n".

My solutions are:
1. PRINT STRING$(i) instead of PRINT i,
2. PRINT st$;"\r" instead of PRINT st$, the \n being appended by PRINT.

bye! Guy

David Szafranski

unread,
Apr 10, 2012, 12:14:15 AM4/10/12
to xbl...@googlegroups.com
Thanks the tip on $$CRLF$. Also note that you need to use IMPORT "xsx"
in the PROLOG.

D.

furfante

unread,
Apr 10, 2012, 6:49:44 PM4/10/12
to xbl...@googlegroups.com
So in the following bit of code

c = 100
a$ = "my string"
a$ = STRING(100)
a$ = STRING(c) 

The line a$=STRING(100) says that "a" is a variable that is defined to hold a string and the STRING(100) prepares 100 as a string.  Is that a fair way to look at it?  If you wrote a$="100" I assume the quotes do the same thing.  In the case of "c" it is defined as a variable that holds integers so it needs to be converted by STRING(c) so that a$ can hold it.  Am I getting this?    After the last program statement does c become c$?  Is c now a string variable or does the statement merely convert the contents of c as it moves it into a$? 

BTW - I want to thank all the folks who responded so patiently to my questions.  I actually finished my program and it seems to run and even do what it was intended to do.  Thanks again you have all been a great help.

Regards,
Wayne

David Szafranski

unread,
Apr 11, 2012, 6:00:33 AM4/11/12
to xbl...@googlegroups.com
So in the following bit of code

c = 100
a$ = "my string"
a$ = STRING(100)
a$ = STRING(c) 

The line a$=STRING(100) says that "a" is a variable that is defined to hold a string and the STRING(100) prepares 100 as a string.  Is that a fair way to look at it?  If you wrote a$="100" I assume the quotes do the same thing.  In the case of "c" it is defined as a variable that holds integers so it needs to be converted by STRING(c) so that a$ can hold it.  Am I getting this?

So far, you got it.
 
   After the last program statement does c become c$?

No,  in a$ = STRING(c), the value assigned to the variable c remains unchanged.
Think of the = sign as an operator which assigns a value to the variable on the left side with the contents of the right side. The only thing that changes in the above statement is a$.
 
 Is c now a string variable or does the statement merely convert the contents of c as it moves it into a$?

c always remains an integer variable. The only way to change c is to assign it
another value

c = c + 10

So now c has a value of 110.

D.

Guy

unread,
Apr 11, 2012, 10:20:41 AM4/11/12
to xbl...@googlegroups.com
--
You received this message because you are subscribed to the Google Groups "xblite" group.
To view this discussion on the web visit https://groups.google.com/d/msg/xblite/-/1vnzUyPNZ08J.

To post to this group, send email to xbl...@googlegroups.com.
To unsubscribe from this group, send email to xblite+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/xblite?hl=en.

Perfect Wayne, you got it all right!

Just an additionnal piece of advice:
Since you want to manipulate strings, forget the STRING operator and use STRING$ instead. (Please, note the final $)

Why? Because the STRING$ operator returns EXPLICITELY a string, which is exactly your intend.
(By no means a big deal! However, I don't mind the extra typing of the final $, and produce a code more to the point ;-) )

This is an illustration of the WYSIWYG principle in my programming rule book:
Say what you do and do as you say.

Programming is a peculiar intellectual activity in that it requires attention to even small details.
Actually, in a code review, your balanced attention to small and big details is an indication of your coding maturity.

Happy xbliting!
Bye! Guy
Reply all
Reply to author
Forward
0 new messages