Responding to keyboard key's

edited May 2014 in JavaScript Mode

In the Processing IDE I have made a very simple program in witch the color of the background changes whenever you press one of the key's r ( red), g (green) or b ( blue). The code looks like this:

int r = 100;
int g = 0;
int b = 0;

void setup() 
{
  size( 400, 400);
}

void draw()
{
  background( r, g, b);
}

void keyPressed()
{
 if ( key == 'r') {
    r=255; g = 0; b = 0;
 } 
 if ( key == 'g') {
    r=0; g=255; b=0;
 } 
 if ( key == 'b') {
    r=0; g=0; b=255;
 } 
}

In Processing the code works fine, but when I put the code to work on a HTML5 webpage with Processing.JS, the program do not respond to the keyboard keys. This is my code to incorporate de above PDE file in a HTML web page:

(<)script src="processing-1.4.8.min.js"></script>
(<)canvas data-processing-sources="mysketch.pde"></canvas>

Can anyone explain how you can respond to keyboard keys from HTML/javascript?

Many thanks in advance.

Pythia

Answers

Sign In or Register to comment.