We are about to switch to a new forum software. Until then we have removed the registration on this forum.
How to avoid mouse click on applet area (to set focus) and allow keypress event? It works in processing 1.5.1, but not in processing 2.0b8
import ddf.minim.*;
Minim minim;
AudioSample kick;
AudioSample snare;
void setup()
{
size(512, 200, P3D);
minim = new Minim(this);
kick = minim.loadSample( "BD.mp3", 512);
snare = minim.loadSample("SD.wav", 512);
}
void draw()
{
background(0);
stroke(255);
for (int i = 0; i < kick.bufferSize() - 1; i++)
{
float x1 = map(i, 0, kick.bufferSize(), 0, width);
float x2 = map(i+1, 0, kick.bufferSize(), 0, width);
line(x1, 50 - kick.mix.get(i)*50, x2, 50 - kick.mix.get(i+1)*50);
line(x1, 150 - snare.mix.get(i)*50, x2, 150 - snare.mix.get(i+1)*50);
}
}
void keyPressed()
{
if ( key == 's' ) snare.trigger();
if ( key == 'k' ) kick.trigger();
}
void stop()
{
kick.close();
snare.close();
minim.stop();
super.stop();
}
Answers
I have tried requestFocusInWindow(), but it doesn't works..
Tried a simple variant in Processing 2.1.1, worked fine on Windows 7 (ie. it displays the string without having to click in the window).
Note that I dropped the P3D, since you don't use 3D at all, and my computer is slow to start OpenGL sketches. Perhaps the P3D was the source of your troubles in your sketch.