Mouse and keyboard events: BasicInputStateMachine class


A BasicInputStateMachine allows to handle all mouse and keyboard Java events. BasicInputStateMachine implements java.awt.MouseListener, java.awt.MouseMotionListener, java.awt.MouseWheelListener and java.awt.KeyListener. Thus, it can be attached to any Java graphical component: using the method sm.addAsListenerOf(component). (this method attaches the machine as a listener for each type of mouse and keyboard events: component.attachMouseListener(sm) ; component.attachMouseMotionListener(sm) ; ...)

Here is the list of transitions supported by BasicInputStateMachine:

In the following example, we use two BasicInputStateMachines to define an original interaction with a numeric text field (javax.swing.JTextField). When the mouse cursor is over the text field, user can:

Failed to load applet. No Java 2 SDK, Standard Edition v 1.5.0 support for applet.

smClasses.jpg

See applet sources.

Here is the code of the state machine:

BasicInputStateMachine interaction = new BasicInputStateMachine(){

    Point2D ptInit = null;
    JTextField tf;
		
    public State out = new State() {
        
        Transition enter = new Enter(">> in") {
            public void action() {
                tf = (JTextField)(((MouseEvent)getEvent()).getComponent());
                // tf.hilite()
            }
        };
			
    };
		
    public State in = new State() {
			
		Transition leave = new Leave(">> out") {
            public void action() {
                // tf.unhilite()
            }
        };
            
        Transition pressPlus = new KeyPress('+') {
            public void action() {
                // increment tf.value
            }
        };
            
        Transition pressMinus = new KeyPress('-') {
            public void action() {
                // decrement tf.value
            }
        };
            
        Transition press = new Press(BUTTON1, ">> control") {
            public void action() {
                // ptInit <-- current point
                // arm a timer to update value even if mouse does not move
                armTimer(200, true);
            }
        };
			
    };
		
    public State control = new State() {
        
        Transition drag = new Drag(BUTTON1) {
            public void action() {
                disarmTimer();
                // update tf.value according to vector (pInit, current point)
                // arm a timer to update value even if mouse does not move
                armTimer(200, true);
            }
        };
			
        Transition timeout = new TimeOut() {
            public void action() {
                // update tf.value according to vector (pInit, current point)
            }
        };
			
        Transition release = new Release(BUTTON1, ">> out") { };
            public void leave() {
                // tf.unhilite()
            }
        };
		
    };

};

...
JTextField text = new JTextField("0");
interaction.addAsListenerOf(text);
...