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

when [name v] key pressed

Parameters

Name Data Type Description
keyCode int A key code. You can use the enum KeyCode. For example KeyCode.VK_SPACE.

Return

void

whenKeyPressed(keyCode)