How to reset the value when using input

New to FreeBASIC? Post your questions here.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: How to reset the value when using input

Post by Tourist Trap »

dodicat wrote:Another possible method to clear the keyboard buffer.

Code: Select all



    dim as function() as string clearbuffer=@inkey()
   


print "Press a key to continue .."

sleep
clearbuffer()

print "len inkey = ";len(inkey)

print "Press a key to end .."

sleep
 
Nice one, I was trying with @chr(getkey()) with no result :)
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to reset the value when using input

Post by fxm »

@dodicat
:-)

But I prefer that following version:

Code: Select all

#macro clearbuffer()
  While Inkey <> ""
  Wend
#endmacro

print "Press a key to continue .."

sleep
clearbuffer()

print "len inkey = ";len(inkey)

print "Press a key to end .."

sleep
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to reset the value when using input

Post by dodicat »

That way is standard (I think because of habit more than reason IMO)
But inkey clears the buffer itself by definition anyway.
...
Peeks into the keyboard buffer and returns a String representation of the first character, if any, found. The key is then removed from the buffer ..

Code: Select all

 
print "Press a key to continue .."

sleep:inkey


print "len inkey = ";len(inkey)

print "Press a key to end .."

sleep
 
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to reset the value when using input

Post by fxm »

fxm wrote:'While InKey <> "" : Wend' instead of a simple 'InKey', is a security against eventual multi key-pressed during the waiting if the CPU is hogged.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to reset the value when using input

Post by dodicat »

Another method(windows)
Linux is read I think

Code: Select all

 

#macro clearbuffer()
  While Inkey <> ""
  Wend
#endmacro

#define emptybuffer shell("pause >nul")

print "Press a few keys (you have five seconds) .."
sleep 5000,1
print "Now press a key to continue . . ."

emptybuffer



print "len inkey = ";len(inkey)

print "Press a key to end .."

sleep
  
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to reset the value when using input

Post by fxm »

fxm wrote:Restricting to the only SLEEP documentation page to be improved, what do users think of my proposal (a second example has been added)?
MrSwiss wrote:Leave it, as is ... (aka: no need for change)

Reason: the only person here, that has mastered the Art of complexifying everything,
instead of keeping simple things the way they are, should not be the "yardstick" ...
Right, as long as I do not have enough positive echoes, I do not touch the SLEEP documentation page.
(do nothing, everyone can do it!)
(me it does not bother me, I know how it works!)

But the next time a user is caught by this behavior, I will complement the explanation in the SLEEP documentation (as suggested above).
Last edited by fxm on Feb 22, 2019 9:42, edited 2 times in total.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to reset the value when using input

Post by dodicat »

The inkey page needs fixing for the coming version 1.06
They are all inkey$ at the moment.
(I cannot do it, I am not on the long list).
My swansong here, clearbuffer+

Code: Select all

  



function clearbuffer() as string
    dim as string tot,char=chr(0)
    while char<>""
        char=inkey
        tot+=char
    wend
    return tot
    end function
        

print "Press a few keys (you have five seconds) .."
sleep 5000,1
print "Now press a key to continue . . ."

var contents=clearbuffer
sleep
clearbuffer



print "Buffer contained ";contents
print "len inkey = ";len(inkey)

print "Press a key to end .."

sleep
   
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: How to reset the value when using input

Post by fxm »

dodicat wrote:The inkey page needs fixing for the coming version 1.06
They are all inkey$ at the moment.
Already done, and also for other relevant keywords.
See paragraph "Dialect Differences" at pages:
Chr[$], Command[$], Hex[$], Inkey[$], Lcase[$], Left[$], Ltrim[$], Mid[$] (statement), Mkd[$], Mki[$], Mkl[$], Mks[$], Oct[$], Right[$], Rtrim[$], Space[$], Str[$], String[$] (function), Trim[$], Ucase[$].
Post Reply