From 3fc64a8066d4996743aef4bcc3c89a44e4459ea1 Mon Sep 17 00:00:00 2001 From: Walter Korman Date: Sat, 12 Jan 2002 02:31:16 +0000 Subject: [PATCH] Simple test applications for examining keyboard events and testing the keyboard manager functionality. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@852 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../java/com/threerings/util/KeyTimerApp.java | 79 ++++++++++++++++++ .../threerings/util/KeyboardManagerApp.java | 80 +++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 tests/src/java/com/threerings/util/KeyTimerApp.java create mode 100644 tests/src/java/com/threerings/util/KeyboardManagerApp.java diff --git a/tests/src/java/com/threerings/util/KeyTimerApp.java b/tests/src/java/com/threerings/util/KeyTimerApp.java new file mode 100644 index 000000000..6b9b8c8cb --- /dev/null +++ b/tests/src/java/com/threerings/util/KeyTimerApp.java @@ -0,0 +1,79 @@ +// +// $Id: KeyTimerApp.java,v 1.1 2002/01/12 02:31:16 shaper Exp $ + +package com.threerings.yohoho.puzzle.util; + +import java.awt.Frame; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +import com.threerings.yohoho.Log; + +public class KeyTimerApp +{ + public KeyTimerApp () + { + _frame = new TestFrame(); + _frame.setSize(400, 300); + } + + public void run () + { + _frame.show(); + } + + public static void main (String[] args) + { + KeyTimerApp app = new KeyTimerApp(); + app.run(); + } + + protected class TestFrame extends Frame implements KeyListener + { + public TestFrame () + { + addKeyListener(this); + _prStart = _rpStart = -1; + } + + public void keyPressed (KeyEvent e) + { + long now = System.currentTimeMillis(); + _prStart = now; + + if (_rpStart != -1) { + Log.info("RP\t" + (now - _rpStart)); + } + + logKey("keyPressed", e); + } + + public void keyReleased (KeyEvent e) + { + long now = System.currentTimeMillis(); + _rpStart = now; + + Log.info("PR\t" + (now - _prStart)); + + logKey("keyReleased", e); + } + + public void keyTyped (KeyEvent e) + { + logKey("keyTyped", e); + } + + /** + * Logs the given message and key. + */ + protected void logKey (String msg, KeyEvent e) + { + int keyCode = e.getKeyCode(); + Log.info(msg + " [key=" + KeyEvent.getKeyText(keyCode) + "]."); + } + + protected long _prStart, _rpStart; + } + + protected Frame _frame; +} diff --git a/tests/src/java/com/threerings/util/KeyboardManagerApp.java b/tests/src/java/com/threerings/util/KeyboardManagerApp.java new file mode 100644 index 000000000..44bffe2ab --- /dev/null +++ b/tests/src/java/com/threerings/util/KeyboardManagerApp.java @@ -0,0 +1,80 @@ +// +// $Id: KeyboardManagerApp.java,v 1.1 2002/01/12 02:31:16 shaper Exp $ + +package com.threerings.yohoho.puzzle.util; + +import java.awt.Frame; +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; + +import com.samskivert.swing.Controller; +import com.samskivert.swing.ControllerProvider; + +import com.threerings.yohoho.Log; + +public class KeyboardManagerApp +{ + public KeyboardManagerApp () + { + _frame = new TestFrame(); + _frame.setSize(400, 300); + } + + public void run () + { + _frame.show(); + } + + public static void main (String[] args) + { + KeyboardManagerApp app = new KeyboardManagerApp(); + app.run(); + } + + protected class TestFrame extends Frame implements ControllerProvider + { + public TestFrame () + { + // create the test controller + _ctrl = new TestController(); + + // add some sample key mappings + KeyTranslatorImpl xlate = new KeyTranslatorImpl(); + xlate.addPressCommand(KeyEvent.VK_LEFT, TestController.MOVE_LEFT); + xlate.addPressCommand( + KeyEvent.VK_RIGHT, TestController.MOVE_RIGHT); + xlate.addPressCommand(KeyEvent.VK_SPACE, TestController.DROP); + + // create the keyboard manager + KeyboardManager keymgr = new KeyboardManager(this, xlate); + } + + // documentation inherited + public Controller getController () + { + return _ctrl; + } + + /** The test controller. */ + protected Controller _ctrl; + } + + protected class TestController extends Controller + { + public static final String MOVE_LEFT = "move_left"; + public static final String MOVE_RIGHT = "move_right"; + public static final String DROP = "drop"; + + // documentation inherited + public boolean handleAction (ActionEvent action) + { + String cmd = action.getActionCommand(); + Log.info("handleAction [cmd=" + cmd + "]."); + return true; + } + } + + /** The test frame. */ + protected Frame _frame; +}