/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * AndroidWorld Library, Copyright 2011 Bryan Chadwick * * * * FILE: ./android/world/test/KeyInput.java * * * * This file is part of AndroidWorld. * * * * AndroidWorld is free software: you can redistribute it and/or * * modify it under the terms of the GNU General Public License * * as published by the Free Software Foundation, either version * * 3 of the License, or (at your option) any later version. * * * * AndroidWorld is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with AndroidWorld. If not, see . * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ package android.world.test; import android.app.Activity; import android.os.Bundle; import android.view.Display; import android.image.*; import android.world.VoidWorld; public class KeyInput extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Display dis = getWindowManager().getDefaultDisplay(); new KeyWorld(dis.getWidth(), dis.getHeight()-50).bigBang(this); } } class KeyWorld extends VoidWorld{ int WIDTH, HEIGHT; String str = ""; String event = ""; KeyWorld(int w, int h){ this.WIDTH = w; this.HEIGHT = h; } public Scene onDraw(){ return new EmptyScene(this.WIDTH, this.HEIGHT, "black") .placeImage(new Text(str, 20, "blue"), this.WIDTH/2, 50) .placeImage(new Text(event, 20, "red"), this.WIDTH/2, 100); } public void onKey(String key){ if(key.length() == 1){ if(key.equals("\b")){ str = str.substring(0, Math.max(str.length()-1, 0)); }else str += key; } event = "Key-down: '"+escape(key)+"'"; } public void onRelease(String key){ event = "Key-up: '"+escape(key)+"'"; } public boolean stopWhen(){ return str.equals("stop\n"); } public Scene lastScene(){ return onDraw().placeImage(new Text("FINISHED", 30, "#CCFF0000").rotate(45), this.WIDTH/2, 75); } public static String escape(String s){ char str[] = s.toCharArray(); StringBuffer ret = new StringBuffer(""); for(char c:str)ret.append(escape(c)); return ret.toString(); } public static String escape(char c){ switch(c){ case '\n':return "\\n"; case '\t':return "\\t"; case '\b':return "\\b"; case '\r':return "\\r"; case '\f':return "\\f"; case '\\':return "\\\\"; case '\'':return "\\'"; case '\"':return "\\\""; default: return ""+c; } } }