Hello DB12222,
To make this function is difficult, but possible. Below, is a tutorial I have typed up for you to understand the answer to your question.
--------------
First, let me show the code in full. This is so you can get an overview of it and, maybe better understand it in the end.
sync on
sync rate 0
do
if lastkey = scancode()
inc counter1
else
counter1 = 0
counter2 = 0
lastkey = scancode()
if scancode() = 14 and len(RealWord$) > 0
RealWord$ = left$(Realword$, len(Realword$) - 1)
FakeWord$ = left$(FakeWord$, len(FakeWord$) - 1)
else
if scancode() > 0 and scancode() <> 14
RealWord$ = Realword$ + inkey$()
FakeWord$ = FakeWord$ + chr$(42)
endif
endif
endif
if counter1 > 20
inc counter2
if counter2 = 5
counter2 = 0
if scancode() = 14 and len(RealWord$) > 0
Realword$ = left$(Realword$, len(Realword$) - 1)
FakeWord$ = left$(FakeWord$, len(FakeWord$) - 1)
else
if scancode() > 0 and scancode() <> 14
Realword$ = Realword$ + inkey$()
FakeWord$ = FakeWord$ + chr$(42)
endif
endif
endif
endif
text1$ = "Actual Input:" + RealWord$
text 1,10,text1$
text2$ = "On-Screen Representation:" + FakeWord$
text 1,30,text2$
sync
cls
loop
Seven variables are needed to make this piece work properly. These variables I have named,
lastkey, counter1, RealWord$, FakeWord$, counter2, text1$ and
text2$.
lastkey - records the key pressed by the user and then compares it to the key pressed in the next program loop
counter1 - used to create the typing 'pause' effect when a single key is held in the 'pressed' state
RealWord$ - contains the actual characters that the user has typed
FakeWord$ - contains star characters in place of the amount of characters the user has typed
text1$ - prints user's 'RealWord' onto the screen
text2 - prints user's 'FakeWord' onto the screen
There are two main sections in this code. The first section tests if the key currently being pressed is equal to the last key pressed(initialy, the key pressed on the last program loop). The second section creates the 'pause' effect, if so-be that the current key has been held down for more than 20 program loops.
First, let's take a look at the first section.
if lastkey = scancode()
inc counter1
else
counter1 = 0
counter2 = 0
lastkey = scancode()
if scancode() = 14 and len(RealWord$) > 0
RealWord$ = left$(Realword$, len(Realword$) - 1)
FakeWord$ = left$(FakeWord$, len(FakeWord$) - 1)
else
if scancode() > 0 and scancode() <> 14
RealWord$ = Realword$ + inkey$()
FakeWord$ = FakeWord$ + chr$(42)
endif
endif
endif
if lastkey() = scancode compares if the last key pressed is equal to the current key being pressed. If it is equal, counter1 is incremented by 1 and this whole section of code is then skipped. Next, the
else statement precedes that the key being pressed is not equal to that of the last key pressed. So,
counter1 is set to zero, representing that the keys pressed were not equal. Then, since the keys were different, the key currently being pressed is recorded into
lastkey, in the statement
lastkey = scancode().
I have included, of course, a rule to use when the user presses the
backspace key. In adding a character to a variable, backspace does not act as it would normally. Instead, you must code it to act as it should. When the backspace key is pressed, it should delete the last character in the
RealWord$ and
FakeWord$ variables. First, you need to know that the backspace key's
scancode() is 14, and as well, 0 is the
scancode() for when no key is being pressed. You can test this with the simple line of code:
print scancode()
if scancode() = 14 and len(RealWord$) > 0 tests if the backspace key is being pressed and tests if there are characters in the variable
RealWord$. If the statement is true, then the very last character within the
RealWord$ and
FakeWord$ variables are subtracted from them. This is accomplished by finding the amount of characters within the variables, via the
len(string$) command, and then decrementing that amount by 1. Then, using the
left$(string$) command, the last character is subtracted. For example, if there are 5 characters within the variable,
len(string$) will return the value of 5. 1 is subtracted from 5, making the value to be 4. So, using the
left$(string$) command, the first four characters are kept and any characters beyond that are deleted. As you can see, the same method is used on the
FakeWord$ variable. In this, whenever the backspace key is pressed, a character is deleted from each variable.
Next, the
else statement precedes that a key is being pressed which is not the backspace key. Here is where we will add the character being pressed to each variable. This is done by using simple 'string concatenation'. First, let's look at the
inkey$() command. It returns the actual string character of the key that is being pressed. So, we simply add the character being pressed to the variable
RealWord$, in the statement
RealWord$ = RealWord$ + inkey$(). Since we wish to display stars on-screen, in place of the actual characters typed by the user, we will add the star character to the
FakeWord$ variable, in like manner.
chr$(42) is just as if the user was to press the star(*) key. 42 is the ASCII code number for the star character. This can be found with the line of code:
print asc(inkey$())
This concludes the first section of our code. Now on to the last section.Let's first take a look at it.
if counter1 > 20
inc counter2
if counter2 = 5
counter2 = 0
if scancode() = 14 and len(RealWord$) > 0
Realword$ = left$(Realword$, len(Realword$) - 1)
FakeWord$ = left$(FakeWord$, len(FakeWord$) - 1)
else
if scancode() > 0 and scancode() <> 14
Realword$ = Realword$ + inkey$()
FakeWord$ = FakeWord$ + chr$(42)
endif
endif
endif
endif
Remember the
counter1 variable? That, if the last key pressed equaled the current key being pressed, it would be incremented by 1, and the whole first section of the code would be skipped. This variable is used to create the effect, that when a key is pressed and held down, there is a short pause, and then the character of the key pressed is rapidly copied to the screen one after the other, until the key is unpressed. So, when a key is held down,
counter1 is incremented by 1 until the key is let go. Not only does this create the 'pause effect', but it keeps characters from being printed uncontrolably to the screen. If this piece of code was not here, each time you pressed a key once, many more than just 1 character would be added to the screen.
Now, when the key is let off of,
counter1 is reset to 0. This resets the code to accept another key that will be held down later. If
counter1 is incremented over 20, then the code below it is executed. The variable
counter2 is to create a delay effect on the key which is being held down. That only every 5 program loops, the code below it, which is the same code explained in section one of the program, is executed. Now, take a quick look back at section one of the code, and see how that if the current key being pressed is not the same as the last key pressed,
counter2 is then reset to zero. This variable must be reset for the second section of code to work properly the next time around.
This concludes my tutorial. Good luck.
----------------------------------
To those who are more skilled than me in this area, I need help fixing a bug in this program above. Try the code out and you will see what I mean, in that when random keys are pressed at random times, and all rapidly, sometimes more stars are printed to the screen then actual characters input by the user. I've looked through my code a million, and studied and never came to a conclusion as why. Everything else in the code works fine. Thank you.
+NanoBrain+