Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 08:40 28 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MM - building a string from keypresses...

Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 08:27pm 11 Sep 2013
Copy link to clipboard 
Print this post

Hello folks.

The existing code I have, uses LINE INPUT, but the problem with this, is that the MM waits there forever for said line input, and the program could get stuck in one of those menus.

In the other menus where only a single key-press is needed, I use a DO/LOOP and TIMER to auto-exit back to the main screen, if you don't press any keys in a given time period:

[code]
timer=0
Do
key$=Inkey$
Loop Until key$<>"" or Timer>=10000
If Asc(key$)=145 Then GoTo SendTestMsg
GoTo start
[/code]

If you go into a menu that has this routine, and you DON'T select something within ten seconds, the code auto-exits back to the main menu.

This works beautifully, and there are no problems with the above, but I am having a hard time trying to work out how I can do the equivalent of LINE INPUT like this.

My first thought was just a variation of the above loop, such that it will only exit back to the main menu, if the value of the key-press is 13(CR or ENTER) or the TIMER exceeds whatever I set it to.

The problem is, that for characters that are NOT enter or CR, how can I put them into a string?

I need a way to be able to put the characters into a string one by one, until ENTER is pressed, then it leaves that routine, with the string of characters.

X=X+1 kind of thing can be used to increment the position in the string, but I can't find any command that will allow me to put the key-presses into the string at a given position.

Can anyone prod me in the right direction?
Smoke makes things work. When the smoke gets out, it stops!
 
kiiid

Guru

Joined: 11/05/2013
Location: United Kingdom
Posts: 671
Posted: 08:57pm 11 Sep 2013
Copy link to clipboard 
Print this post

It probably should be something like this. In most of the cases you can get away with BackSpace support only, but if you need to be able to walk through the text, insert and delete, then it will be a bit more complex code.

(writing it at the moment so errors ARE possible):


s$="" ' this will be the output string
k0$="" ' this is to catch the moment of pressing the key only
Timer=0
do
k$=inkey$
if k0$="" and k$<>"" then
if asc(k$)>=32 then
s$=s$+k$
print k$;
endif
if asc(k$)=8 and s$<>"" then
s$=left$(s$,len(s$)-1)
print chr$(8);" ";chr$(8);
endif
Timer=0
endif
k0$=k$
loop until k$=chr$(13) or Timer>=1000
if (Timer>=1000) then s$="" ' timeout will return a null string

Edited by kiiid 2013-09-13
http://rittle.org

--------------
 
MicroBlocks

Guru

Joined: 12/05/2012
Location: Thailand
Posts: 2209
Posted: 09:01pm 11 Sep 2013
Copy link to clipboard 
Print this post

You have to add it to a string:
[code]
txt$ = txt$ + key$
[/code]
You do this after you check the key$ for a enter or backspace.
A backspace should check for the length of the string and when it is higher then 1 you can then take the left part of it (length - 1) with the left$ function.
You can go very fancy on this with support for home,end,left arrow,right arrow, delete etc. In the past i have made many variations of this depending on what kind of values were entered. Support for a tab, up and down arrow could allow the user to move from one input field to another.
You can even add support with input masks etc.

As a start it should in my opinion at least support the backspace to enable the user to correct the input.
Best is to make a function for this that accepts parameters like maximum length, and timeout if needed. You can then easily expand on it when the need arises.


Microblocks. Build with logic.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 10:09pm 11 Sep 2013
Copy link to clipboard 
Print this post

Thanks chaps for the pointers, especially kiiid for the code sample - I will study it.
Smoke makes things work. When the smoke gets out, it stops!
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024