/********************************************************************************
* The contents of this file are subject to the GNU General Public License *
* (GPL) Version 2 or later (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at *
* http://www.gnu.org/copyleft/gpl.html *
* *
* Software distributed under the License is distributed on an "AS IS" basis, *
* without warranty of any kind, either expressed or implied. See the License *
* for the specific language governing rights and limitations under the *
* License. *
* *
* This file was originally developed as part of the software suite that *
* supports the book "The Elements of Computing Systems" by Nisan and Schocken, *
* MIT Press 2005. If you modify the contents of this file, please document and *
* mark your changes clearly, for the benefit of others. *
********************************************************************************/
package builtInChips;
import Hack.Gates.BuiltInGateWithGUI;
import Hack.Gates.GateException;
import Hack.Gates.GatesManager;
import Hack.Utilities.Definitions;
import SimulatorsGUI.KeyboardComponent;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
* /* A Keyboard, implemented as a 16 bit register that stores the currently pressed key code.
*/
public class Keyboard extends BuiltInGateWithGUI implements KeyListener {
// The currently pressed key.
private short key;
// The gui.
private KeyboardComponent gui;
/**
* Constructs a new Keyboard.
*/
public Keyboard() {
if (GatesManager.getInstance().isChipsGUIEnabled()) {
gui = new KeyboardComponent();
gui.setLocation(4, 264);
gui.getKeyEventHandler().addKeyListener(this);
gui.reset();
}
}
@Override
public Component getGUIComponent() {
return gui;
}
/**
* Activated when a key is pressed.
*/
@Override
public void keyPressed(KeyEvent e) {
key = Definitions.getInstance().getKeyCode(e);
if (key > 0) {
outputPins[0].set(key);
evalParent();
if (gui != null) {
gui.setKey(Definitions.getInstance().getKeyName(e));
}
}
}
/**
* Activated when a key is released.
*/
@Override
public void keyReleased(KeyEvent e) {
key = 0;
outputPins[0].set(key);
evalParent();
if (gui != null) {
gui.clearKey();
}
}
/**
* Activated when a key is typed.
*/
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public short getValueAt(int index) throws GateException {
checkIndex(index);
return key;
}
// checks the given index. If illegal throws GateException.
private void checkIndex(int index) throws GateException {
if (index != 0) {
throw new GateException("Keyboard has no index. Use ARegister[]");
}
}
@Override
public void setValueAt(int index, short value) throws GateException {
throw new GateException("Keyboard is read only");
}
}