# Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

### DarkBASIC Professional Discussion / HOW do you clear the keystate buffer ?

Message
Posted: 23rd Nov 2016 23:23 Edited at: 24th Nov 2016 12:00
Have a program that when it first runs needs to know 1 of 3 conditions

e=500:h=150:i=3

rem conditions
Q\$(1)="New"
Q\$(2)="Continue"
Q\$(3)="End Program"

program goes to _menu1

_menu1:
for x = 1 to i
ink rgb(110,150,240),0:set cursor e,19:print "( )";
ink rgb(150,0,200),0:set cursor (e+21),19:print str\$(x);
ink rgb(150,150,150),0:set cursor (e+58),19:print Q\$(x);
e=(e+h)
next x
_menu01:
if keystate(79) = 1 then r=1:goto _endm1
if keystate(80) = 1 then r=2:goto _endm1
if i = 2 then goto _menu01
if keystate(81) = 1 then r=3:goto _endm1
if i = 3 then goto _menu01
if keystate(75) = 1 then r=4:goto _endm1
if i = 4 then goto _menu01
if keystate(76) = 1 then r=5:goto _endm1
if i = 5 then goto _menu01
if keystate(77) = 1 then r=6:goto _endm1
goto _menu01
_endm1:
ink rgb(0,0,0),0:box 0,16,1920,50

after I get the condition from _menu1

program goes to _menu2 to get condition number TWO

e=500:h=150:i=3

say keystate 79 was pressed in _menu1 - then r=1 , tells the program to create a NEW file
then program goes to _menu2

rem conditions
Q\$(1)="Red file"
Q\$(2)="Blue file"
Q\$(3)="Main Menu"

_menu2:
for x = 1 to i
ink rgb(110,150,240),0:set cursor e,1040:print "( )";
ink rgb(150,0,200),0:set cursor (e+21),1040:print str\$(x);
ink rgb(80,40,20),0:set cursor (e+58),1040:print Q\$(x);
e=(e+h)
next x
_menu02:
if keystate(79) = 1 then r=1:goto _endm2
if keystate(80) = 1 then r=2:goto _endm2
if i = 2 then goto _menu02
if keystate(81) = 1 then r=3:goto _endm2
if i = 3 then goto _menu02
if keystate(75) = 1 then r=4:goto _endm2
if i = 4 then goto _menu02
if keystate(76) = 1 then r=5:goto _endm2
if i = 5 then goto _menu02
if keystate(77) = 1 then r=6:goto _endm2
_endm2:
ink rgb(0,0,0),0:box 0,1040,1920,1070

here in lies my problem: in _menu1 keystate(79) was pressed so now keystate(79) = 1
when the program gets to _menu2 , keystate(79) is already at 1 so the program automatically passes program control to keystate(79) even though I have not pressed "any" keys yet
HOW do you clear keystate() buffers so that they return to 0 ?

using inkey\$ does not work either - SAME problem , what ever was last pressed , STAYS in the buffer , How do you clear the inkey\$ buffers ?

The program works fine if I use input R
but this method requires me to press the condition number I want ( 1-2-3 ) and press the [enter] key.
My goal is to make the program run by pressing only one key ( 1-2-3 / a-b-c )

suggestions ???

-majic-
Posted: 24th Nov 2016 01:35 Edited at: 24th Nov 2016 01:37
Hi there
Immediately you get into the new label , you could reset variable ....something like this:

Here an example

I'm not a grumpy grandpa
Posted: 24th Nov 2016 10:25 Edited at: 24th Nov 2016 13:18
using scancode() is no good either - has same problem as keystate() and inkey\$ - once either of them is used the , the last data put into their buffer stays and it does not reset

the issue is not resetting the var r

the issue is resetting the keystate(79) back to zero

having scancode() = 0 , keystate() = 0 , inkey\$ = "" , does not clear the buffer

When you use successive 'menus' as I am above , using scancode() , keystate() or inkey\$ , ONLY works for the first time because the buffers are at zero , but once used , buffer changes from 0 to 1 and it stays at 1 , it does not reset itself for the next occurrence of the keystate() check
run this program with [num lock] ON , (scancode 156 is the [enter] key on the numeric keypad and keystate 79 is the number 1 of the numeric keypad
may need to adjust ( set cursor commands) depending on your screen resolution , mine is at 1920,1080,32

scancode()=0
keystate(79)=0
inkey\$()=""

_001:
set cursor 500,400:print "at label 001";
_011:
if scancode()=156 then scancode()=0:goto _002
rem press this key or
if keystate(79)=1 then keystate(79)=0:goto _002
rem press this key
goto _011

_002:
set cursor 500,450:print "at label 002";
_012:
if scancode()=156 then scancode()=0:goto _003
if keystate(79)=1 then keystate(79)=0:goto _003
rem the program should STOP here at _002 and wait
rem for you to press another key either 1 or enter
rem but it does NOT , it goes straight thru to label _005
goto _012

_003:
set cursor 500,500:print "at label 003";
_013:
if inkey\$()<>"" then inkey\$()="":goto _004
goto _013
rem once 1 or enter is pressed , the buffers are filled with the 1 or enter data and it
rem just keeps passing the buffer data right on thru - never resetting itself to 0
rem that is the problem, how to reset buffers to zero state so that it stops at each
rem of the labels and WAITS for a key press , without having to use the input command

_004:
set cursor 500,550:print "at label 004";
_014:
if inkey\$()<>"" then inkey\$()="":goto _005
goto _014

_005:
set cursor 500,600:print "at label 005";

_wk:
wait key

_end:
end

so basically : HOW does one clear the keyboard buffer
if the keyboard buffer is cleared , then all scancodes for scancode() , keystate() , inkey\$() , etc. should all revert to zero state

-majic-
Posted: 24th Nov 2016 14:29 Edited at: 24th Nov 2016 14:45
< < < < < < < < < < < EUREKA > > > > > > > > > >

I THINK I FOUND THE SOLUTION !

call the function

clearbuffer()

create the function

function clearbuffer()
while inkey\$() <> ""
az=0
endwhile
endfunction
-majic-
Posted: 24th Nov 2016 14:36 Edited at: 25th Nov 2016 10:05
Here is my new program thus far and it WORKS !

-majic-
Posted: 25th Nov 2016 12:27 Edited at: 25th Nov 2016 13:47

Consider using common input functions and organizing input processing into central locations within your program.
Peppering you program with inputs everywhere is building you own nightmare.

Also if you're going to use Keystates like that you should consider programming it to activate when the button is released,
This problem is called Input Flooding.
Posted: 25th Nov 2016 13:39
input flooding

Thank You for your input , something to consider later on

but your code is above this nubbys' skills at this time

my five lines of function code vs your 17 is enough for me

Thanks again

-majic-
Posted: 25th Nov 2016 13:55
Quote: "my five lines of function code vs your 17 is enough for me"

The point is my 17 lines only needs to be written once, and when I improve them the whole program sees the benefit.

The code I posted also lets you type a complete sentence. I thought it might be helpful to someone.

Thanks for the reply.

Login to post a reply

Server time is: 2023-09-29 01:43:53
Your offset time is: 2023-09-29 01:43:53