Request Focus

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();
}
Tagged:

Answers

  • I have tried requestFocusInWindow(), but it doesn't works..

  • edited February 2014 Answer ✓

    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).

    void setup()
    {
      size(512, 200);
    }
    
    void draw()
    {
    }
    
    void keyPressed()
    {
      if ( key == 's' ) println("snare");
      if ( key == 'k' ) println("kick");
    }
    

    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.

Sign In or Register to comment.