Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prevent double space from On-Screen Keyboard

Status
Not open for further replies.

Sammybobo

Programmer
Apr 4, 2003
87
US
I am making a screen keyboard with a textfield that captures and writes the equivalent of the pressed key. I do not want the space or " " to be captured in the textfield more than once. How could I do this? Thanks.
 
create a variable to hold what the previous key pressed is, then on the next key press, compare it to the last key pressed and if its another space, dont accept the input.

example:
Code:
//start off with a null var
var lastKey = null

//when a key is pressed... (in this example, the spacebar)
//note: "key value" is just psuedo code
if (keyPress(keyDown(SPACE))) {
    //if new key value is NOT equal to the last key value
    //(if this is a space and the last input isnt a space...
    //then display the input)
    if ("key value" != lastKey) {
        sometext.text = "key value";
        lastKey = "key value";
    }
    //otherwise do nothing
}

something like that

--
Matt Milburn, Wave Motion Studios
matt@wavemotionstudios.com
 
perhaps simpler with a boolean value

spacePressed = false;
listen = new Object();
listen.onKeyDown = function() {
if (Key.getCode() == Key.SPACE){
if(!spacePressed){
spacePressed = true;
my_text.text += chr(Key.getAscii());
}
} else {
my_text.text += chr(Key.getAscii());
}
};
Key.addListener(listen);
 
Thanks very much gentlemen, for your help.

Bill, "my_text.text += chr(Key.getAscii());" seems to be the same for both IF and ELSE. Would that be the case if I am trying to exclude SPACE after the first occurence? Thanks again.
 
code as posted should only allow for 1 space

after first space click all else are ignored. is that not what you wanted?
 
if i misunderstood the question then reset the boolean

spacePressed = false;
listen = new Object();
listen.onKeyDown = function() {
if (Key.getCode() == Key.SPACE) {
if (!spacePressed) {
spacePressed = true;
my_text.text += chr(Key.getAscii());
}
} else {
my_text.text += chr(Key.getAscii());
spacePressed = false;
}
};
Key.addListener(listen);



first version just says 1 space and 1 space only all other text a continuous string

text texttexttext


second version says only 1 space between strings

text text text
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top