File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Handling User Events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Handling User Events" Watch "Handling User Events" New topic

Handling User Events

Charles Souders

Joined: Apr 25, 2006
Posts: 5
I have been having a hard time understaning the "Handling of User Events" and specifily exacty how this code works...


public void keyPressed(KeyEvent firstEvent) {


// The first line means:
// "keyPressed" is the method name we need to call from "java.awt.event.*;"
// "KeyEvent" a class in the "java.awt.event.*;" and "firstEvent"
// is just the name we choose for the object we are creating from this
// class "(KeyEvent firstEvent)" is in parenthesis becuse we are using
// this object as an argument, i.e. A keyPressed method should trigger
// this event and we are going to refer to this specific "KeyEvent
// as "firstEvent"

// The Second line means:
// execute the "getKeyChar()" method which is in the "firstEvent" object
// I am not sure if/what arguments "getKeyChar()" could/can have
// Does the "firstEvent.getKeyChar();" statement actually display the typed
// character onscreen, or is that being done by somewhere else in the
// application

I experimented with the code and referenced several books, tutorials, CBT, etc, however none of them really break down the code and explain it to you.
can anyone help?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

Hi Charles,

Welcome to JavaRanch!

A lot of your explanation is correct, but there are a few key points that are not. The most important one is that you won't call keyPressed(), and you won't create a KeyEvent; the AWT event thread will do these things. You supply this method, and Java calls it automatically under certain circumstances.

As to the second line (the body of the method), it doesn't actually do anything observable; it's a "tree falling in the forest" kind of thing. If this came from a book or example code, it's a misleading and silly example.

But you're right that the getKeyChar() method is being invoked on the firstEvent object. This method takes no arguments. It returns a value, which is being ignored, and has no side effects, so when this event handler is invoked, nothing observable will happen. If you wanted to print the actual character, you might replace that second line with


It seems to me as if you've gotten a little ahead of yourself as far as trying to understand this code. You might want to back up and get some practice with more basic "chapter 1" stuff before getting to GUI writing.

[Jess in Action][AskingGoodQuestions]
Shyam Prasad Murarka
Ranch Hand

Joined: May 02, 2005
Posts: 209
Dear Readers,
I'll try explaining this without going into too many details. This follows a Pattern called the Observer Pattern. The structure in this pattern is like this:
  • A class which fires events, (also called the source)
  • a class which encapulates information about the event,
  • and a class which is notified about the event.(also called the listener)

  • Here are the steps to listen for KeyEvents:
  • We'll make a Frame to listen for KeyEvents. To qualify itself for listening to KeyEvents, it HAS to implement the KeyListener interface (and obviously all the methods that come along with the interface.)
  • After qualifying itself, it has to register itself for KeyEvents by calling the addKeyListener() method and passing a reference of itself to the method. (This reference is actually any class that implements the KeyListener interface)
  • So, here, Frame is the listener.
  • KeyEvent, included in java.awt.event package, is the class that encapsulates information about the event.
  • The source will, of course, be the keyboard.

  • Here's what happens when a Key is pressed;
  • The source (the keyboard) fires an event.
  • This event is actually an instance of KeyEvent and it contains information about the Key that was pressed, such as which key was pressed.
  • Since, the Frame was registered to listen for KeyEvents, the keyPressed() method of this frame is invoked and a KeyEvent parameter is passed into it that contains the information associated with this event.

  • Hope I haven't made any mistakes. And also, hope I haven't sounded confusing. To me, it sure does look confusing.

    With Best Regards,
    Shyam Prasad Murarka
    Charles Souders

    Joined: Apr 25, 2006
    Posts: 5

    public void keyPressed(KeyEvent firstEvent) {
    firstEvent.getKeyChar(); }

    Thank you for all your input...I think I understand it a little better now, I have actually spent a lot of time on the basics, but I keep getting hung-up when I get to event handling, I understand how to create G.U.I's, how to implement listener interfaces, add eventlisteners and how to capture and utilize these events

    char myChar = firstEvent.getKeyChar();
    keylabel.setTest("You pressed the " + myChar + " key!");

    What I am not sure about is:

    1) is "keyPressed" the name of a method in "java.awt.event.*;" ?

    2) is "KeyEvent" a class in the "java.awt.event.*;" ?

    3) does the first line create a "KeyEvent" object named "firstEvent" ?

    4) does the second line mean call the method "getKeyChar" from the object "firstEvent" ?

    If anyone can confirm these assumptions or correct me I would be very greatfull. I have been able to utilize event handlers, but I am not really sure If I understand them.

    Shyam Prasad Murarka
    Ranch Hand

    Joined: May 02, 2005
    Posts: 209
    Dear Reader,
    1) keyPressed() IS a method belonging to the interface java.awt.event.KeyListener.
    2) Yes, KeyEvent IS a class in java.awt.event.*
    3) No, as I stated before, the source (keyboard) creates the KeyEvent object and it passes it to the keyPressed() method. Now, the reference of this event object that was fired is assigned to the "firstObject". A new object is NOT created.
    4) Yes, getKeyChar() is a method being called on the object firstEvent.
    Charles Souders

    Joined: Apr 25, 2006
    Posts: 5
    Thank you both for your assistance and patience. Shyam Prasad Murarka, thanks for taking the time to re explain your explanations this helped clarify many of my questions.

    I have spend another 5-6 hours reviewing your comments, the A.P.I. , several websites concerning Event Handling and experimenting with the code.

    So basically what is happening is when an event occurs (ie keyboard pressed), the component generates an KeyEvent object and passes the KeyEvent to a registered listener, which sends the KeyEvent to it's keyPressed method and implements the method.

    this said (and hopefully correct)

    When we write the statement:
    public void keyPressed(KeyEvent firstEvent) {

    All we are doing is creating a new method and calling it keyPressed so the listener knows where to sent the KeyEvent

    And when we say (KeyEvent firstEvent, we are just saying that within this method we will refer to the KeyEvent as the "firstEvent"

    "public void keyPressed(KeyEvent firstEvent)"
    does not really call on a method in the A.P.I., it creates a new method where we can react to user event with a block of code such as: { Char cmd = firstEvent.getKeyChar(); }

    Shyam Prasad Murarka
    Ranch Hand

    Joined: May 02, 2005
    Posts: 209
    Dear Reader,

    All we are doing is creating a new method and calling it keyPressed so the listener knows where to sent the KeyEvent

    No, we are NOT creating a NEW method. Infact, this is a method that has been defined in the java.awt.event.KeyListener interface. The method signature of keyPressed() in your class should be exactly like the signature in the KeyListener interface.
    NOTE: Your listener class has to implement the KeyListener interface to qualify itself as a listener.
    I agree. Here's the link:
    subject: Handling User Events
    It's not a secret anymore!