typed input - leaving out spaces

edited January 2014 in JavaScript Mode

I've setup a processing sketch that runs on a web server communicating with a php script.

Part of it involves typing input directly into the browser window - no forms.

It works, but my problem is - I cannot find a way to stop the typed input accepting spaces (which I do not want).

Here's the relevant bit of code for what I am trying to do:

void keyReleased() {
  if (key == CODED) {
  }
  if (key != CODED) {
    switch(key) {
    default:
      if (key== " "){
        return;
      }
      if (key!=" ") {
        typedEntry += str(key);
        //up=false;
        //down=false;
      }
    }  
  }
}

Answers

  • Answer ✓

    You're getting your control structures all mixed up. Do you want an if statement or a switch? I suggest you use an if statement:

    void keyReleased() { if (key != CODED && key!=" ") { typedEntry += key; } }

  • edited January 2014

    Thanks!

    Just tried that. (Also used str(key) so I don't get the ascii)

    Maybe the problem is javascript, because I can still type a space with that....

  • edited January 2014

    Did it!

    Here's the code that worked:

    void keyReleased() {
      if (key != CODED && key!="32") { typedEntry += str(key); } 
    }
    

    You made me realize the space was seen as ascii by the javascript :)

  • edited January 2014

    Oh man, my bad. I've been working in python a lot lately, and company policy is that all quotes are double quotes.

    The trouble you were having is because " " is a string and ' ' is a space character.

    void keyReleased() { if (key != CODED && key!=' ') { typedEntry += key; } }

  • edited January 2014

    There's no need to test for CODED! Just a redundant and visual pollution! :P
    Also, w/ keyCode, we don't need to test for lowercase letters! o->

  • edited January 2014

    Thanks Guys - great input

    The final code that works is this:

    void keyReleased() {
      if (key!=' ') { typedEntry += str(key); } 
    }
    

    I have to use the str cast as otherwise the type comes up as ascii in the web browser (javascript).

    I've not seen this in many references, and it had me stumped for a bit, so I include it here for people....

    Cheers!

  • edited January 2014

    Actually, i found I need to keep the CODED, otherwise it messes up the deleting of characters (a sort of double pipe appears instead). Here's relevant bits of code....

     void draw(){
        entrydisplay();
    }
    

    //blinking cursor, typedEntry display

    void entryDisplay(){ 
          text((frameCount/4 % 2 == 0 ? "▌" : ""),width/4+20,100);
          text(typedEntry,width/4,100); 
     } 
    

    //code to delete typed input

    void keyPressed(){
            if (keyCode == LEFT) {
                typedEntry = typedEntry.substring(0,max(0,typedEntry.length()-1));
                left=true;
             }    
     }
    

    //code to block space input

     void keyReleased() {
          if (key != CODED) {
              if (key!=' ') {
                typedEntry += str(key);
              }
           }
        }
    
  • edited January 2014 Answer ✓

    Indeed, since it's a text input program, testing for CODED is just an extra way to know whether we can actually use key variable.
    Most of the time, I just use keyCode exclusively rather than key, b/c I'm usually not interested in lowercase letters!

    I've got an online example which uses both keyTyped() & keyPressed().
    Of course, I coulda used keyReleased() instead of keyPressed() too.

    Inside keyTyped(), I'm not interested to continue if key = CODED. So I check for it to issue an early quit if it's so:

    if (key == CODED)  return;
    

    While within keyPressed(), it's the opposite:

    if (key != CODED) return;
    

    In short, in some situations, testing for CODED is more like a shortcut; but not exactly essential: :-B

    http://studio.processingtogether.com/sp/pad/export/ro.9Zo$UbIWYZEDR/latest

  • Wow, that's really helpful.

    I was just staggering around and testing what worked, that will help me immensely when I refine this - thank you sir.

Sign In or Register to comment.