topical media & game development

talk show tell print

#javascript-processing-example-basic-inputs-keyboard.htm / htm



  <!DOCTYPE html>
  <html><head>
  <script src="javascript-processing-example-processing.js"></script>
  <script src="javascript-processing-example-init.js"></script>
  <link rel="stylesheet" href="javascript-processing-example-style.css">
  </head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
  <h2>Keyboard</h2>
  
  <p>Click on the image to give it focus and press the letter keys 
  to create forms in time and space. Each key has a unique identifying 
  number called it's ASCII value. These numbers can be used to position 
  shapes in space.</p>
  
  <p><a href="http://processing.org/learning/basics/keyboard.html"><b>Original Processing.org Example:</b> Keyboard</a><br>
  <script type="application/processing">
  int numChars = 26;
  color[] colors = new color[numChars];
  int keyIndex;
  float keyScale;
  int rectWidth;
  
      
  void setup()
  {
    size(200, 200);
    noStroke();
    background(0);
    keyScale = 200/numChars-1.0;
    rectWidth = width/4;
  }
  
  void draw()
  { 
    if(keyPressed) {
      if(key >= 'A' && key <= 'z') {
        if(key <= 'Z') {
          keyIndex = key-'A';
        } else {
          keyIndex = key-'a';
        }
        fill(millis()%255);
        float beginRect = rectWidth/2 + keyIndex*keyScale-rectWidth/2;
        rect(beginRect, 0.0, rectWidth, height);
      }
    }
  }
  </script><canvas width="200" height="200"></canvas></p>
  <div style="overflow: hidden; height: 0px; width: 0px;"></div>
  
  <pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
  // unless otherwise stated.</b>
  int numChars = 26;
  color[] colors = new color[numChars];
  int keyIndex;
  float keyScale;
  int rectWidth;
  
      
  void setup()
  {
    size(200, 200);
    noStroke();
    background(0);
    keyScale = 200/numChars-1.0;
    rectWidth = width/4;
  }
  
  void draw()
  { 
    if(keyPressed) {
      if(key &gt;= 'A' &amp;&amp; key &lt;= 'z') {
        if(key &lt;= 'Z') {
          keyIndex = key-'A';
        } else {
          keyIndex = key-'a';
        }
        fill(millis()%255);
        float beginRect = rectWidth/2 + keyIndex*keyScale-rectWidth/2;
        rect(beginRect, 0.0, rectWidth, height);
      }
    }
  }</pre>
  </body></html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.