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.

Author
Message
DB12222
20
Years of Service
User Offline
Joined: 20th Mar 2005
Location:
Posted: 20th Mar 2005 23:38
Hi
I would like to make a password field but I want the letters to appear as a * character. I still want to compare the users password to the correct one but i dont want to use the input command. I would like to make my own function to do this. Thanks in advance
NanoBrain
20
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Portland, OR
Posted: 21st Mar 2005 08:27 Edited at: 22nd Mar 2005 12:05
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.


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 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.


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+
DB12222
20
Years of Service
User Offline
Joined: 20th Mar 2005
Location:
Posted: 23rd Mar 2005 04:55
Thanks for all the effort, but there is a problem with that in that if you hold a key down, the number of characters in the actual do not match the number of characters in the masked representation. I am usually very good with varibles and making my functions, but this just goes over my head. Any more ideas please, and i dont need to see the actual representation, only the masked version. Please help!
NanoBrain
20
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Portland, OR
Posted: 25th Mar 2005 09:25
DB12222,

At the bottom of my post I did make mention of the fault in the program, that the number of characters do not match completely. The program works fine until a button is pressed repeatedly. Then, do the star characters begin to be more. It's a good program except for this bug. However, if someone could figure where the bug is, it would be perfect.

As an end, the 'actual' input can be taken from the screen by simply deleting the line of code which prints it out, which is at the bottom of the do loop.

text1$ = "Actual Input:" + RealWord$
text 1,10,text1$

By doing this, you are left with just the stars on the screen and a record of the actual input in a variable.

+NanoBrain+
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 26th Mar 2005 04:54 Edited at: 26th Mar 2005 04:58


that should do ya, ignore the fact that the code is displayed, you can stop that by removing the section between the rem's that tells you whats there for diagnostic purposes, thats just so that you can see it working in realtime internaly, type the code as normal and press backspace to erase, numbers, lower and uppercase letters are allowed, no spaces or symbols, press enter when you have finished, try it

Mentor.

PC1: P4 3ghz, 1gig mem, 3x160gig hd`s, Radeon 9800pro w cooler (3rd gfx card), 6 way speakers.
PC2: AMD 2ghz, 512mb ram, FX5200 ultra, 16 bit SB.
Mini ATX cases suck.

Login to post a reply

Server time is: 2025-05-31 05:39:27
Your offset time is: 2025-05-31 05:39:27