Event Handling

Key and Mouse Events

 

Events are generated by

1.    Controls (Mostly)

2.    Also from keyboard and mouse.

 

Key Events

§  When a key is typed on the keyboard, a KeyEvent is generated.

§  There are three types of key events:

o   a key is pressed (KEY_PRESSED)

o   a key is released (KEY_RELEASED)

o   and a key is typed (KEY_TYPED)

§  These events are defined by KeyEvent as objects of EventType.

§  special keys – function, SHIFT, ALT, or ARROW keys.

§  event handler for the various types of key events.

              final void setOnKeyPressed(EventHandler<? super KeyEvent> handler)

              final void setOnKeyReleased(EventHandler<? super KeyEvent> handler)

              final void setOnKeyTyped(EventHandler<? super KeyEvent> handler)

 

§  When a key-typed event is received

o   can obtain the character by calling getCharacter( ) on the event.

                        final String getCharacter( )

§  When a key-pressed / key-released event is received

o   can obtain the key code by calling getCode( ) on the key event.

o   Key codes are enumeration constants defined by KeyCode – they represent the various keys on a keyboard

              final KeyCode getCode( )

 


 


// JavaFX controls

// Keyboard events - Press, release

 

import javafx.application.*;

import javafx.stage.*;

import javafx.scene.*;

import javafx.scene.layout.*;

import javafx.scene.control.*;

import javafx.scene.input.*;

import javafx.event.*;

import javafx.geometry.*;

 

public class  KB_EventHandling_Demo extends Application

{     

   public static void main(String[] as)

   {

        // Start the JavaFX application by calling launch().

        launch(as);

   }

 

   // Override the start() method.

   public void start(Stage myStage)

   {       

        // Give the stage a title.

        myStage.setTitle("JavaFx - Keyboard Events.");

 

        // Use a FlowPane for the root node.

        FlowPane fp = new FlowPane(Orientation.VERTICAL,25,25);

 

        // Center the controls in the scene.

        fp.setAlignment(Pos.CENTER);

       

        // Creating a heading and response text

        Label heading = new Label("Keyboard Events.");

        Label respText = new Label("");

       

        // Add to the scene graph.

        fp.getChildren().addAll(heading, respText);

       

       

        // Create a scene.

        Scene myScene = new Scene(fp, 300, 100);

 

        // Set the scene on the stage.

        myStage.setScene(myScene); 

       

        // Event Handling       

        myScene.setOnKeyPressed(new EventHandler<KeyEvent>(){

             public void handle(KeyEvent ke)

             {  

                 String str = "Key Pressed : "+ke.getCode();

                 respText.setText(str.toString());

             }

        } ) ;

       

        myScene.setOnKeyReleased(new EventHandler<KeyEvent>(){

             public void handle(KeyEvent ke)

             {  

                 String str = "Key Released : "+ke.getCode();

                 respText.setText(str.toString());

             }

        } ) ;

       

        // Show the stage and its scene.

        myStage.show();

   }

}

 


 


 


 

// JavaFX controls

// Keyboard Event handling

 

import javafx.application.*;

import javafx.scene.*;

import javafx.stage.*;

import javafx.event.*;

import javafx.scene.layout.*;

import javafx.scene.control.*;

import javafx.scene.input.*;

import javafx.geometry.*;

 

public class JfxKB_EH extends Application

{     

   public static void main(String[] args)

   {

        // Start the JavaFX application by calling launch().

        launch(args);

   }

 

   // Override the start() method.

   public void start(Stage myStage)

   {

        StringBuffer str = new StringBuffer();

       

        // Give the stage a title.

        myStage.setTitle("JavaFx - Keyboard Event handling");

 

        // Use a FlowPane for the root node.

        FlowPane fp = new FlowPane(Orientation.VERTICAL,25,25);

 

        // Center the controls in the scene.

        fp.setAlignment(Pos.CENTER);

       

        // Creating a heading and response text

        Label heading = new Label("Keyboard Event Handling.");

        Label respText = new Label("");

       

        // Add to the scene graph.

        fp.getChildren().addAll(heading, respText);

       

        // Create a scene.

        Scene myScene = new Scene(fp, 300, 100);

 

        // Set the scene on the stage.

        myStage.setScene(myScene); 

       

        // Event Handling       

        myScene.setOnKeyTyped(new EventHandler<KeyEvent>(){

             public void handle(KeyEvent ke)

             {  

                 str.append(ke.getCharacter());

                 respText.setText(str.toString());

             }

        } ) ;

       

        // Show the stage and its scene.

        myStage.show();

   }

  

}

 

 

Mouse Events

§  Mouse events are represented by the MouseEvent class, which is packaged in javafx.scene.input.

§  Like KeyEvent, a MouseEvent can be handled by instances of various classes, and the Node and Scene classes define convenience methods for mouse events.

§  When a mouse event is handled by a Node, mouse events are received only when that node has input focus.

§  When a mouse event is handled by a Scene, mouse events are received when the scene has input focus.

§  Different types of events that can be generated by the mouse

o   when the mouse is moved,

o   when a button is clicked or released,

o   when the mouse enters or exits an element that handles mouse events,

o   when the mouse is dragged.

§  EventType objects defined by MouseEvent to represent these events are MOUSE_CLICKED and MOUSE_MOVED.

§  methods used to register event handlers for mouse events are

     final void setOnMouseClicked(EventHandler<? super MouseEvent> handler)

     final void setOnMouseMoved(EventHandler<? super MouseEvent> handler)

 

§  MouseEvent defines a number of methods to determine what has occurred.

§  getButton( ) – When the mouse is clicked, find which button was used

     final MouseButton getButton( )

§  returns

o   MouseButton.PRIMARY – if left button was clicked

o   MouseButton.SECONDARY – if right button was clicked

o   MouseButton.MIDDLE – if middle button was clicked

o   MouseButton.NONE – if no button was clicked

§  getClickCount( ) – to obtain the number of times a mouse button has been clicked

     final int getClickCount( )

§  Returns the number of times the button has been pressed.

o   a single-click has the value 1. A double-click has the value 2. 0 for a non-click event.

§  getSceneX( ) and getSceneY( ) methods – to obtain the location of the mouse at the time an event occurred

     final double getSceneX( )

     final double getSceneY( )

 

Filtering and Consuming Events

§  The event handling mechanism in JavaFX occurs in two phases:

o   event capturing and event bubbling.

§  Event handlers execute during the event bubbling phase. Event filters execute during the event capturing phase. Thus, event filters execute before event handlers. This means an event filter can act on an event before it is received by the event handler registered to the event target.

§  To register an event filter, use the addEventFilter( ) method. It is defined for Node, Scene, and Stage, among others.

§  final <T extends Event> void addEventFilter (EventType<T> eType, EventHandler<? super T> handler) Here, eType specifies the type of event to be captured, such as ActionEvent.ACTION or

§  MouseEvent.MOUSE_MOVED. The event handler is passed to handler. You can remove an event filter by calling removeEventFilter( ).

§  One way that a filter can be used is to prevent an event from reaching its target. Recall that the event dispatch chain starts at the top and works its way down. Therefore, an event filter inserted into the chain prior to the element that generated the event can prevent that element from receiving the event. A filter does this by consuming the event. This is done by calling consume( ) on the event. The consume( ) method is defined by Event, as shown here:

          void consume( )

§  After consume( ) has been called on an event, the processing of the event is terminated.

§  Thus, a consumed event will never reach an event handle

 

  

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu