001/* An actor which pops up a keystroke-sensing JFrame.
002
003 Copyright (c) 1998-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.lib.gui;
029
030import ptolemy.data.IntToken;
031import ptolemy.kernel.CompositeEntity;
032import ptolemy.kernel.util.IllegalActionException;
033import ptolemy.kernel.util.NameDuplicationException;
034
035///////////////////////////////////////////////////////////////////
036//// ArrowKeySensor
037
038/**
039 Detect when the user presses or releases an arrow key and produce an
040 integer on the corresponding output.
041
042 <p>When this actor is preinitialized, it pops up a new JFrame window on
043 the desktop, usually in the upper left hand corner of the screen.
044 When this JFrame has the focus (such as when it has been clicked on)
045 it is capable of sensing keystrokes.
046
047 <p>This actor senses only the four non-numeric-pad arrow-key
048 keystrokes.  This actor responds to key releases as well as key
049 presses.  Upon each key press, the integer 1 is broadcast from the
050 corresponding output.  Upon each key release, the integer 0 is output.
051
052 <p>This actor contains a private inner class which generated the JFrame.
053 The frame sets up call-backs which react to the keystrokes.  When called,
054 these call the director's fireAtCurrentTime() method.  This causes
055 the director to call fire() on the actor.   The actor then broadcasts
056 tokens from one or both outputs depending on which keystroke(s) have
057 occurred since the actor was last fired.
058
059 <p>NOTE: This actor only works in the DE domain due to its reliance on
060 this director's fireAtCurrentTime() method.
061
062 @author Winthrop Williams
063 @version $Id$
064 @since Ptolemy II 8.0
065 @Pt.ProposedRating Red (winthrop)
066 @Pt.AcceptedRating Red (winthrop)
067 */
068public class ArrowKeyProbe extends ArrowKeySensor {
069
070    /**
071     * Create an actor that detects user presses on the arrow key.
072     *
073     * @param container The container for this actor.
074     * @param name The name of this actor
075     * @exception IllegalActionException If the actor cannot be contained
076     *  by the proposed container.
077     * @exception NameDuplicationException If the container already has an
078     *  actor with this name.
079     */
080    public ArrowKeyProbe(CompositeEntity container, String name)
081            throws NameDuplicationException, IllegalActionException {
082        super(container, name);
083    }
084
085    ///////////////////////////////////////////////////////////////////
086    ////                         public methods                    ////
087
088    /** Broadcast the integer value 1 for each key pressed and 0 for
089     *  each released.
090     */
091    @Override
092    public synchronized void fire() throws IllegalActionException {
093        //super.fire();
094        // Broadcast key presses.
095        if (_upKeyPressed) {
096            //_upKeyPressed = false;
097            upArrow.broadcast(new IntToken(1));
098        } else {
099            upArrow.broadcast(new IntToken(0));
100        }
101
102        if (_leftKeyPressed) {
103            leftArrow.broadcast(new IntToken(1));
104        } else {
105            leftArrow.broadcast(new IntToken(0));
106        }
107
108        if (_rightKeyPressed) {
109            rightArrow.broadcast(new IntToken(1));
110        } else {
111            rightArrow.broadcast(new IntToken(0));
112        }
113
114        if (_downKeyPressed) {
115            downArrow.broadcast(new IntToken(1));
116        } else {
117            downArrow.broadcast(new IntToken(0));
118        }
119    }
120}