Import from http://drts.cvs.sourceforge.net/viewvc/drts/projects/jxinput/ tarball
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
//**********************************************************************************************
|
||||
// (C) Copyright 2002 by Dipl. Phys. Joerg Plewe, HARDCODE Development
|
||||
// All rights reserved. Copying, modification,
|
||||
// distribution or publication without the prior written
|
||||
// consent of the author is prohibited.
|
||||
//
|
||||
// Created on 16. April 2002, 23:31
|
||||
//**********************************************************************************************
|
||||
package de.hardcode.jxinput.keyboard;
|
||||
|
||||
/**
|
||||
* Exeception to be thrown if keycode is not in then range [0,255].
|
||||
*
|
||||
* @author Herkules
|
||||
*/
|
||||
public class InvalidKeyCodeException
|
||||
extends IllegalArgumentException
|
||||
{
|
||||
|
||||
/**
|
||||
* Creates a new instance of InvalidKeyCodeException.
|
||||
*/
|
||||
public InvalidKeyCodeException()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new instance of InvalidKeyCodeException.
|
||||
*/
|
||||
public InvalidKeyCodeException( String s )
|
||||
{
|
||||
super( s );
|
||||
}
|
||||
}
|
||||
175
java/de/hardcode/jxinput/keyboard/JXKeyboardInputDevice.java
Normal file
175
java/de/hardcode/jxinput/keyboard/JXKeyboardInputDevice.java
Normal file
@@ -0,0 +1,175 @@
|
||||
//**********************************************************************************************
|
||||
// (C) Copyright 2002 by Dipl. Phys. Joerg Plewe, HARDCODE Development
|
||||
// All rights reserved. Copying, modification,
|
||||
// distribution or publication without the prior written
|
||||
// consent of the author is prohibited.
|
||||
//
|
||||
// Created on 9. April 2002, 22:40
|
||||
//**********************************************************************************************
|
||||
package de.hardcode.jxinput.keyboard;
|
||||
|
||||
|
||||
import de.hardcode.jxinput.*;
|
||||
import java.awt.Component;
|
||||
|
||||
|
||||
/**
|
||||
* Virtual input device treating a AWT keyboard as a source for Buttons.
|
||||
*
|
||||
* @author Herkules
|
||||
*/
|
||||
public class JXKeyboardInputDevice
|
||||
implements JXInputDevice
|
||||
{
|
||||
private static final String DEVICENAME = "Swing Keyboard";
|
||||
|
||||
/** The driver doing all the real work. */
|
||||
private final KeyboardDriver mDriver = new KeyboardDriver();
|
||||
|
||||
/** The Component I am listening to. */
|
||||
private Component mComponent = null;
|
||||
|
||||
/** Hold the biggest keycode for which a button has been created. */
|
||||
private int mMaxIdxCreated = 0;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new instance of JXKeyboardInputDevice.
|
||||
*/
|
||||
public JXKeyboardInputDevice()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new instance of JXKeyboardInputDevice
|
||||
* immediately listening to a JComponent.
|
||||
*/
|
||||
public JXKeyboardInputDevice( Component comp )
|
||||
{
|
||||
listenTo( comp );
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes this device listen to a certain JComponent.
|
||||
*/
|
||||
public final void listenTo( Component comp )
|
||||
{
|
||||
shutdown();
|
||||
mComponent = comp;
|
||||
mComponent.addKeyListener( mDriver );
|
||||
}
|
||||
|
||||
/**
|
||||
* Shut down. No longer listen to my JComponent.
|
||||
*/
|
||||
public final void shutdown()
|
||||
{
|
||||
if ( null != mComponent )
|
||||
mComponent.removeKeyListener( mDriver );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create a button object for a certain keycode.
|
||||
*/
|
||||
public Button createButton( int keycode )
|
||||
{
|
||||
if ( 0 > keycode || 0x100 < keycode )
|
||||
throw new InvalidKeyCodeException();
|
||||
|
||||
KeyButton b;
|
||||
if ( null == (b = mDriver.getButton( keycode ) ) )
|
||||
{
|
||||
b = new KeyButton( keycode );
|
||||
mDriver.registerKeyButton( b );
|
||||
if ( keycode > mMaxIdxCreated )
|
||||
mMaxIdxCreated = keycode;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
public void removeButton( Button b )
|
||||
{
|
||||
mDriver.unregisterKeyButton( (KeyButton) b );
|
||||
}
|
||||
|
||||
|
||||
|
||||
//*********************************************************************************************
|
||||
//
|
||||
// Implement JXInputDevice
|
||||
//
|
||||
//*********************************************************************************************
|
||||
|
||||
public Axis getAxis(int idx)
|
||||
{
|
||||
// No axes on keyboard.
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public Button getButton(int idx)
|
||||
{
|
||||
// idx is interpreted as the keycode
|
||||
return mDriver.getButton( idx );
|
||||
}
|
||||
|
||||
public Directional getDirectional(int idx)
|
||||
{
|
||||
// No directionals on keyboard.
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Maximum number of axes as an upper bound for index values. */
|
||||
public int getMaxNumberOfAxes()
|
||||
{
|
||||
// No axes on keyboard.
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Maximum number of buttons as an upper bound for index values. */
|
||||
public int getMaxNumberOfButtons()
|
||||
{
|
||||
// Return biggest keycode (inclusive).
|
||||
return mMaxIdxCreated + 1;
|
||||
}
|
||||
|
||||
/** Maximum number of directional features as an upper bound for index values. */
|
||||
public int getMaxNumberOfDirectionals()
|
||||
{
|
||||
// No directionals on keyboard.
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Devices may have a name.
|
||||
* This name might be provided by a system dependant driver.
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return DEVICENAME;
|
||||
}
|
||||
|
||||
/** Actual number of available axes. */
|
||||
public int getNumberOfAxes()
|
||||
{
|
||||
// No axes on keyboard.
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Actual number of available buttons. */
|
||||
public int getNumberOfButtons()
|
||||
{
|
||||
return mDriver.getNumberOfButtons();
|
||||
}
|
||||
|
||||
/** Actual number of available directional features. */
|
||||
public int getNumberOfDirectionals()
|
||||
{
|
||||
// No directionals on keyboard.
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
94
java/de/hardcode/jxinput/keyboard/KeyButton.java
Normal file
94
java/de/hardcode/jxinput/keyboard/KeyButton.java
Normal file
@@ -0,0 +1,94 @@
|
||||
//**********************************************************************************************
|
||||
// (C) Copyright 2002 by Dipl. Phys. Joerg Plewe, HARDCODE Development
|
||||
// All rights reserved. Copying, modification,
|
||||
// distribution or publication without the prior written
|
||||
// consent of the author is prohibited.
|
||||
//
|
||||
// Created on 9. April 2002, 22:51
|
||||
//**********************************************************************************************
|
||||
package de.hardcode.jxinput.keyboard;
|
||||
|
||||
import de.hardcode.jxinput.Button;
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
|
||||
/**
|
||||
* Associates a keycode with a Button and handles the current state of that button.
|
||||
*
|
||||
* @author Herkules
|
||||
*/
|
||||
class KeyButton
|
||||
implements Button
|
||||
{
|
||||
private final int mKeyCode;
|
||||
private boolean mIsPressed;
|
||||
private boolean mHasChanged;
|
||||
|
||||
/**
|
||||
* Creates a new instance of KeyButton.
|
||||
*/
|
||||
public KeyButton( int keycode )
|
||||
{
|
||||
mKeyCode = keycode;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the keycode assigned with this button.
|
||||
*/
|
||||
public final int getKeyCode()
|
||||
{
|
||||
return mKeyCode;
|
||||
}
|
||||
|
||||
final void setIsPressed( boolean flag )
|
||||
{
|
||||
mIsPressed = flag;
|
||||
}
|
||||
|
||||
//*********************************************************************************************
|
||||
//
|
||||
// Implement Button
|
||||
//
|
||||
//*********************************************************************************************
|
||||
|
||||
|
||||
/**
|
||||
* Features may have a name provided e.g. by the driver.
|
||||
*/
|
||||
public String getName()
|
||||
{
|
||||
return KeyEvent.getKeyText( mKeyCode );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tells the state of the button at last update.
|
||||
*/
|
||||
public boolean getState()
|
||||
{
|
||||
return mIsPressed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the type of the button.
|
||||
* Pushbutton will deliver <code>true==getState()</code> as long as they are pressed down.
|
||||
* Togglebuttons will change their state once they are pressed and keep that state
|
||||
* until they are pressed again.
|
||||
* @return [ PUSHBUTTON | TOGGLEBUTTON ]
|
||||
*/
|
||||
public int getType()
|
||||
{
|
||||
return Button.PUSHBUTTON;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Denote wether this feature has changed beyond it's resolution since it got last
|
||||
* updated.
|
||||
*/
|
||||
public boolean hasChanged()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
141
java/de/hardcode/jxinput/keyboard/KeyboardDriver.java
Normal file
141
java/de/hardcode/jxinput/keyboard/KeyboardDriver.java
Normal file
@@ -0,0 +1,141 @@
|
||||
//**********************************************************************************************
|
||||
// (C) Copyright 2002 by Dipl. Phys. Joerg Plewe, HARDCODE Development
|
||||
// All rights reserved. Copying, modification,
|
||||
// distribution or publication without the prior written
|
||||
// consent of the author is prohibited.
|
||||
//
|
||||
// Created on 9. April 2002, 22:43
|
||||
//**********************************************************************************************
|
||||
package de.hardcode.jxinput.keyboard;
|
||||
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.util.HashMap;
|
||||
import java.security.InvalidParameterException;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Listen to a JComponent handle handle all associated button objects.
|
||||
* This is the main worker class for JXKeyboardInputDevice.
|
||||
*
|
||||
* @author Herkules
|
||||
*/
|
||||
class KeyboardDriver implements KeyListener
|
||||
{
|
||||
// HashMap mKeysToObserveMap = new HashMap();
|
||||
|
||||
int mNumberOfKeysObserved = 0;
|
||||
KeyButton [] mKeysObserved = new KeyButton [ 0x100 ];
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new instance of KeyboardDriver.
|
||||
*/
|
||||
public KeyboardDriver()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* How many buttons are registered?
|
||||
*/
|
||||
final int getNumberOfButtons()
|
||||
{
|
||||
return mNumberOfKeysObserved;
|
||||
// return mKeysToObserveMap.size();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Place a new button under my observation.
|
||||
*/
|
||||
final boolean registerKeyButton( KeyButton b )
|
||||
{
|
||||
final int keycode = b.getKeyCode();
|
||||
|
||||
if ( 0 > keycode || 0x100 < keycode )
|
||||
throw new InvalidKeyCodeException();
|
||||
|
||||
if ( null == mKeysObserved[ keycode ] )
|
||||
{
|
||||
mKeysObserved[ keycode ] = b;
|
||||
mNumberOfKeysObserved++;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Integer code = new Integer( b.getKeyCode() );
|
||||
// if ( ! mKeysToObserveMap.containsKey( code ) )
|
||||
// {
|
||||
// mKeysToObserveMap.put( code, b );
|
||||
// return true;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
}
|
||||
|
||||
final void unregisterKeyButton( KeyButton b )
|
||||
{
|
||||
final int keycode = b.getKeyCode();
|
||||
|
||||
if ( 0 > keycode || 0x100 < keycode )
|
||||
throw new InvalidKeyCodeException();
|
||||
|
||||
if ( null != mKeysObserved[ b.getKeyCode() ] )
|
||||
{
|
||||
mKeysObserved[ keycode ] = null;
|
||||
mNumberOfKeysObserved--;
|
||||
}
|
||||
|
||||
// Integer code = new Integer( b.getKeyCode() );
|
||||
// mKeysToObserveMap.remove( code );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve the button from its keycode.
|
||||
*/
|
||||
final KeyButton getButton( int keycode )
|
||||
{
|
||||
if ( 0 > keycode || 0x100 < keycode )
|
||||
throw new InvalidKeyCodeException();
|
||||
|
||||
return mKeysObserved[ keycode ];
|
||||
|
||||
// Integer code = new Integer( keycode );
|
||||
// return (KeyButton)mKeysToObserveMap.get( code );
|
||||
}
|
||||
|
||||
|
||||
//*********************************************************************************************
|
||||
//
|
||||
// Implement KeyListener
|
||||
//
|
||||
//*********************************************************************************************
|
||||
|
||||
public void keyPressed( KeyEvent keyEvent )
|
||||
{
|
||||
KeyButton b = getButton( keyEvent.getKeyCode() );
|
||||
if ( null != b )
|
||||
b.setIsPressed( true );
|
||||
}
|
||||
|
||||
public void keyReleased( KeyEvent keyEvent )
|
||||
{
|
||||
KeyButton b = getButton( keyEvent.getKeyCode() );
|
||||
if ( null != b )
|
||||
b.setIsPressed( false );
|
||||
}
|
||||
|
||||
public void keyTyped( KeyEvent keyEvent )
|
||||
{
|
||||
// Intentionally empty.
|
||||
}
|
||||
|
||||
}
|
||||
10
java/de/hardcode/jxinput/keyboard/package.html
Normal file
10
java/de/hardcode/jxinput/keyboard/package.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||||
|
||||
<HTML>
|
||||
<HEAD>
|
||||
<TITLE>de.hardcode.jxinput.keyboard</TITLE>
|
||||
</HEAD>
|
||||
<BODY>
|
||||
Connects Swing keyboard handling to the JXInput infrastructure.
|
||||
</BODY>
|
||||
</HTML>
|
||||
Reference in New Issue
Block a user