Scratch for Java

Stage::whenKeyPressed(keyCode)

This method is called when a key is pressed. The method will receive the key code as a paramter.

Examples

import org.openpatch.scratch.*;

public class StageWhenKeyPressed {

  class CustomStage extends Stage {

    public CustomStage() {
      super(600, 240);
      while (this.getTimer().forMillis(3000))
        ;
      this.exit();
    }

    @Override
    public void whenKeyPressed(int keyCode) {
      this.display("Key Pressed: " + keyCode);
    }
  }

  public StageWhenKeyPressed() {
    new CustomStage();
  }

  public static void main(String[] args) {
    new StageWhenKeyPressed();
  }
}

View on GitHub

Syntax

Java

void whenKeyPressed(keyCode)

Scratch

Parameters

NameData TypeDescription
keyCodeintA key code. You can use the enum KeyCode. For example KeyCode.VK_SPACE.

Return

void