We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
Perhaps use keyCode in place of key? Use cap literals if u follow this tip: Instead of 'r', use 'R'.
http://studio.processingtogether.com/sp/pad/export/ro.9bY07S95k2C2a/latest
P.S.: When posting code, highlight it and hit CTRL+K!!!
Try to click on the canvas before typing, to ensure the script has the focus.