Added support for specifying the delay in milliseconds before a pressed

key begins to auto-repeat.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2170 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Walter Korman
2003-01-17 01:33:34 +00:00
parent 91c173b6e5
commit a5c348a775
3 changed files with 73 additions and 11 deletions
@@ -1,5 +1,5 @@
//
// $Id: KeyTranslator.java,v 1.4 2003/01/14 00:53:38 shaper Exp $
// $Id: KeyTranslator.java,v 1.5 2003/01/17 01:33:34 shaper Exp $
package com.threerings.util;
@@ -36,10 +36,17 @@ public interface KeyTranslator
/**
* Returns the number of times each second that key presses are to be
* automatically repeated while the key is held down.
* automatically repeated while the key is held down, or
* <code>0</code> to disable auto-repeat for the key.
*/
public int getRepeatRate (int keyCode);
/**
* Returns the delay in milliseconds before generating auto-repeated
* key press events for the specified key.
*/
public long getRepeatDelay (int keyCode);
/**
* Returns an iterator that iterates over the available press
* commands.
@@ -1,5 +1,5 @@
//
// $Id: KeyTranslatorImpl.java,v 1.4 2003/01/14 00:53:38 shaper Exp $
// $Id: KeyTranslatorImpl.java,v 1.5 2003/01/17 01:33:34 shaper Exp $
package com.threerings.util;
@@ -9,7 +9,7 @@ import java.util.Iterator;
import com.samskivert.util.HashIntMap;
/**
* A simple implementation of the {@link KeyTranslator} interface that
* A basic implementation of the {@link KeyTranslator} interface that
* provides facilities for mapping key codes to action command strings for
* use by the {@link KeyboardManager}.
*/
@@ -31,14 +31,33 @@ public class KeyTranslatorImpl implements KeyTranslator
* registered.
*
* @param rate the number of times each second that the key press
* should be repeated while the key is down; passing <code>0</code>
* will result in no repeating.
* should be repeated while the key is down, or <code>0</code> to
* disable auto-repeat for the key.
*/
public void addPressCommand (int keyCode, String command, int rate)
{
addPressCommand(keyCode, command, rate, DEFAULT_REPEAT_DELAY);
}
/**
* Adds a mapping from a key press to an action command string that
* will auto-repeat at the specified repeat rate after the specified
* auto-repeat delay has expired. Overwrites any existing mapping for
* the specified key code that may have already been registered.
*
* @param rate the number of times each second that the key press
* should be repeated while the key is down; passing <code>0</code>
* will result in no repeating.
* @param repeatDelay the delay in milliseconds before auto-repeating
* key press events will be generated for the key.
*/
public void addPressCommand (
int keyCode, String command, int rate, long repeatDelay)
{
KeyRecord krec = getKeyRecord(keyCode);
krec.pressCommand = command;
krec.repeatRate = rate;
krec.repeatDelay = repeatDelay;
}
/**
@@ -90,7 +109,14 @@ public class KeyTranslatorImpl implements KeyTranslator
public int getRepeatRate (int keyCode)
{
KeyRecord krec = (KeyRecord)_keys.get(keyCode);
return (krec == null) ? 0 : krec.repeatRate;
return (krec == null) ? DEFAULT_REPEAT_RATE : krec.repeatRate;
}
// documentation inherited
public long getRepeatDelay (int keyCode)
{
KeyRecord krec = (KeyRecord)_keys.get(keyCode);
return (krec == null) ? DEFAULT_REPEAT_DELAY : krec.repeatDelay;
}
// documentation inherited
@@ -128,11 +154,19 @@ public class KeyTranslatorImpl implements KeyTranslator
/** The rate in presses per second at which the key is to be
* auto-repeated. */
public int repeatRate;
/** The delay in milliseconds that must expire with the key still
* pressed before auto-repeated key presses will begin. */
public long repeatDelay;
}
/** The keys for which commands are registered. */
protected HashIntMap _keys = new HashIntMap();
/** The default key press repeat rate. */
protected static final int DEFAULT_REPEAT_RATE = 3;
protected static final int DEFAULT_REPEAT_RATE = 5;
/** The default delay in milliseconds before auto-repeated key presses
* will begin. */
protected static final long DEFAULT_REPEAT_DELAY = 150L;
}
@@ -1,5 +1,5 @@
//
// $Id: KeyboardManager.java,v 1.17 2003/01/16 21:18:02 shaper Exp $
// $Id: KeyboardManager.java,v 1.18 2003/01/17 01:33:34 shaper Exp $
package com.threerings.util;
@@ -397,6 +397,7 @@ public class KeyboardManager
_releaseCommand = _xlate.getReleaseCommand(_keyCode);
int rate = _xlate.getRepeatRate(_keyCode);
_pressDelay = (rate == 0) ? 0 : (1000L / rate);
_repeatDelay = _xlate.getRepeatDelay(_keyCode);
}
/**
@@ -412,8 +413,15 @@ public class KeyboardManager
if (_iid == -1 && _pressDelay > 0) {
// register an interval to post the key press command
// until the key is decidedly released
_iid = IntervalManager.register(
this, _pressDelay, null, true);
if (_repeatDelay > 0) {
_iid = IntervalManager.register(
this, _repeatDelay, this, false);
} else {
_iid = IntervalManager.register(
this, _pressDelay, null, true);
}
if (DEBUG_EVENTS) {
Log.info("Pressing key [key=" + _keyText + "].");
}
@@ -533,6 +541,16 @@ public class KeyboardManager
} else if (_lastPress != 0 && _pressCommand != null) {
// post the key press command again
postPress(now);
if (arg == this) {
// this key had a specific repeat delay interval
// that may differ from the once-started repeat
// rate, and so we need to re-register the
// interval to make use of the proper time and to
// continue repeating henceforth
_iid = IntervalManager.register(
this, _pressDelay, null, true);
}
}
} else if (id == _siid) {
@@ -621,6 +639,9 @@ public class KeyboardManager
/** The milliseconds to sleep between sending repeat key commands. */
protected long _pressDelay;
/** The delay in milliseconds before auto-repeating the key press. */
protected long _repeatDelay;
}
/** An observer operation to notify observers of a key event. */