Keyboard input without INPUT with key filters

SYS 64738
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Keyboard input without INPUT with key filters

Post by Shaun_B »

Here's a little routine I remember writing during my pre-teen years, though this is definitely a bit cleaner.

Code: Select all

0 dim a$, b$, e$, i
10 e$="!;:()@[]'0123456789., "+chr$(13)
20 print">{reverse on} {reverse off}{left}";
30 gosub 250
40 print "final string: "; a$
50 end
250 get b$: if b$="" then goto 250
255 for i=1 to len(e$)
260 if b$=mid$(e$, i, 1) then goto 280
265 next i
270 if b$=chr$(20) and len(a$)>0 then a$=left$(a$, (len(a$)-1) ): goto 290
275 if b$<"a" or b$>"z" then goto 250
280 if b$=chr$(13) then print" ": return
290 print b$;"{reverse on} {reverse off}{left}";:if len(a$)<254 and b$<>chr$(20) then a$=a$+b$
300 goto 250
The variable A$ is used as the final input, each key event is stored in B$ and E$ is used as a filter. Here's some more specifics:

Lines 255 - 265 will check for the exceptions stored in E$ by checking the contents of B$ against each character.

Line 270 checks for the delete key and will work if there is something already in the variable A$, if so it will remove the last character from the string and goto 290.

Line 275 checks the keys A - Z by default.

Line 280 checks for the RETURN key to be pressed - if so, the last character is cleared and the sub-routine is returned.

Line 290 will output the contents of B$ as long as it's a 'legal entry', followed by an reversed space character. There is also a check that A$ is not exceeded the maximum length of a string in Commodore BASIC (ie, 255 characters).

I think that's it.

Regards,

Shaun.


BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
User avatar
Heart
Member
Member
Posts: 38
Joined: Fri Aug 22, 2014 3:52 pm
Location: Minnesota
Contact:

Re: Keyboard input without INPUT with key filters

Post by Heart »

:arrow:
Last edited by Heart on Thu Oct 23, 2014 8:46 am, edited 1 time in total.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Keyboard input without INPUT with key filters

Post by Shaun_B »

Heart wrote:It's hard to follow--lines 260 and 270 seem to leapfrog over one another into 280 and 290.

I think if it had been me I would have left out e$ and just had a series of IF statements.
You could do it with IF statements but then you'd need a way to handle " - you can see the problem if you add CHR$(34) to the E$ string. So, just using IF statements, we need to have a condition for the double quotes and return. Here's a way to do this:

Code: Select all

0 dim a$, b$, i
10 print">{reverse on} {reverse off}{left}";
20 gosub 250
30 print "final string: "; a$
40 end
250 get b$: if b$="" then goto 250
260 if b$=chr$(13) then print" ":return
270 if b$=chr$(20) and len(a$)>0 then let a$=left$(a$, (len(a$)-1) ): goto 300
280 if b$<" " or b$>"]" then goto 250
290 if b$=chr$(34) then print chr$(34);chr$(20);
300 print b$;"{reverse on} {reverse off}{left}";:if len(a$)<254 and b$<>chr$(20) then let a$=a$+b$
310 goto 250
Line 290 now handles the double quotes problem by opening the quote and deleting the opening character right away.

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
User avatar
Heart
Member
Member
Posts: 38
Joined: Fri Aug 22, 2014 3:52 pm
Location: Minnesota
Contact:

Re: Keyboard input without INPUT with key filters

Post by Heart »

:arrow:
Last edited by Heart on Thu Oct 23, 2014 8:47 am, edited 1 time in total.
User avatar
Shaun_B
Member
Member
Posts: 179
Joined: Tue May 06, 2014 12:12 pm
Location: UK
Contact:

Re: Keyboard input without INPUT with key filters

Post by Shaun_B »

Heart wrote:Oh, I see. I was wondering why you had all those characters in e$.

I suppose one could also disable quote mode after every printed character directly--poke 214,0 on the C64 or poke 244,0 on the C128.
Thanks for the tip :-)

Regards,

Shaun.
BASIC Programming - making the mistakes so that you don't have to.
Circles and Squares.
Nothing I post here will stand up in a court of law.
MadModder
Member
Member
Posts: 2
Joined: Wed Jul 13, 2016 1:57 pm
Contact:

Re: Keyboard input without INPUT with key filters

Post by MadModder »

Don't use

Code: Select all

10 get a$: if a$="" then 10
because it chews up memory every time a$ is assigned nothing or anything. Makes a difference in big programs with heavy variable use.
Use this instead

Code: Select all

10 poke198,0:wait198,1:geta$
Then it'll just sit there and wait and really do nothing until you press a key.
198 is the current keyboard buffer size on a C64.

Or if you have a loop doing something, animations or whatever, use

Code: Select all

10 if peek(198) then get a$ : poke 198,0
20 here goes the looping stuff
.
.
90 if a$<>"" then a$=""
100 goto 10
Then it'll assign a$ only if something is pressed, and ignore consecutive key presses (to not have the prorgam go havoc if someone bangs the whole hand on the keyboard...)
:)
User avatar
Gyro Gearloose
Member
Member
Posts: 471
Joined: Sun Nov 15, 2015 5:20 am
Contact:

Re: Keyboard input without INPUT with key filters

Post by Gyro Gearloose »

Could you explain the purpose of line 0, and what does DIM accomplish with no index? I feel I'm missing something here, because I don't recall BASIC ever needing to declare variables?
The price one pays for pursuing any profession, or calling, is an intimate knowledge of its ugly side.
merman
Member
Member
Posts: 183
Joined: Mon Apr 14, 2014 2:50 pm
Location: Skegness
Contact:

Re: Keyboard input without INPUT with key filters

Post by merman »

Gyro Gearloose wrote:Could you explain the purpose of line 0, and what does DIM accomplish with no index? I feel I'm missing something here, because I don't recall BASIC ever needing to declare variables?
It's a memory allocation trick.

By DIMming a string variable like that, it allocates the memory space immediately.

Placing this DIM statement at the start of the program is a way to keep important variables near the start of variable memory, speeding up access.
User avatar
Gyro Gearloose
Member
Member
Posts: 471
Joined: Sun Nov 15, 2015 5:20 am
Contact:

Re: Keyboard input without INPUT with key filters

Post by Gyro Gearloose »

Well wouldn't that depend on the size of the string and wouldn't it cause "garbage collection" thus slowing it down?
Wouldn't declaring i for example as an integer variable like this: i% save more memory and make things faster without invoking garbage collection?
The price one pays for pursuing any profession, or calling, is an intimate knowledge of its ugly side.
User avatar
Zippy Zapp
Member
Member
Posts: 206
Joined: Tue Jul 21, 2015 6:52 pm
Location: USA
Contact:

Re: Keyboard input without INPUT with key filters

Post by Zippy Zapp »

Thanks for the snippet. Back in the days of glory I use to find it fascinating all the different ways over the years people came up with non-input input.
Heart wrote: :arrow:
Sorry if this is obvious to everyone else but I don't know what the arrow means? What is the purpose of a post with just an arrow? Is it because you didn't like what you posted and deleted that and changed the text to an arrow?
Commodore - Changing the world 8 bits at a time.
Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 4 guests