🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #2969

Re: Event queue?

#2969
From: Joe Nuxoll
Date: Tue, 09 Jun 1998 00:00
59 lines
2724 bytes
Torsten Welches wrote:
> I have a frame with a number of controls on it - say edits. I would
> like to perform a specific action whenever a special key - say F5 - is
> pressed, regardless of which edit currently has input focus.
> I tried enableEvents(AWTEvent.KEY_EVENT_MASK) for the frame. However
> the processKeyEvent (KeyEvent e) handler doesn't get called when an
> edit (another key event listener) has input focus.

Here are two possible paths to a solution, one will work for sure, but isn't as
elegant as the second. The second, however, may not work at all <g>...

1) Listen to everthing on your form, and 'consume' the F5 key:

  // the KeyBoard hook class
  KeyListener keyHook = new KeyAdapter {
    public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == e.VK_F5) {
        doF5Action();
        e.consume();
      }
    }
  }

  // use this to register KeyListeners (pass in 'this' from your Frame)
  public void hook(Component root) {
    root.addKeyListener(keyHook);
    if (root instanceof Container) {
      Component[] children = ((Container)root).getChildren();
      for (int i = 0; i < children.length; i++)
        hook(children[i]);
    }
  }

  // use this to unregister KeyListeners
  public void unhook(Component root) {
    root.removeKeyListener(keyHook);
    if (root instanceof Container) {
      Component[] children = ((Container)root).getChildren();
      for (int i = 0; i < children.length; i++)
        unhook(children[i]);
    }
  }

In initialization code, after all the child components have been added to your
form, go ahead and call hook passing the Frame object ('this').  This will hook
every component's key events and listen for the F5 key.  Be sure to also unhook
all the listeners if you no longer need them.

2) Another way to try to get at this is to look at the event queue itself:

AWTEvent EventQueue.peekEvent()
AWTEvent EventQueue.peekEvent(int id)

This will allow you to scan the event queue for events of a specific type.  Once
you have found your KeyEvent holding the F5 keystroke, you can consume it as
above...  but this may not work.  You'll have to experiment.

 - Joe
Technical Details
Message-ID: <357D8484.DA8764D7@inprise.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <35770ac3.200718@forums.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,9af2f9605a0f2ee5
X-Google-Attributes: gid1089d0,public
From: Joe Nuxoll <jnuxoll@inprise.com>
Subject: Re: Event queue?
Date: 1998/06/09
Message-ID: <357D8484.DA8764D7@inprise.com>
X-Deja-AN: 361106513
Content-Transfer-Encoding: 7bit
References: <35770ac3.200718@forums.borland.com>
Content-Type: text/plain; charset=us-ascii
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.api