Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2005

Tip: Looking for answers? Try searching our database.

Overriding keyboard events in Swing components

Thread view: 
Wes Harrison - 10 Nov 2005 00:38 GMT
I know how to add listeners on key events but they appear to get the key
after it has actually been processed by Swing and any default operations
performed.  What I want to do is to override completely what happens when a
particular key is pressed seemingly before Swing processes it and perhaps
prevent Swing from doing anything with it.  How do I do that?

Thanks,

Wes
Roedy Green - 10 Nov 2005 00:53 GMT
>I know how to add listeners on key events but they appear to get the key
>after it has actually been processed by Swing and any default operations
>performed.  What I want to do is to override completely what happens when a
>particular key is pressed seemingly before Swing processes it and perhaps
>prevent Swing from doing anything with it.  How do I do that?

see http://mindprod.com/jgloss/event11.html
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Wes Harrison - 10 Nov 2005 11:44 GMT
> >I know how to add listeners on key events but they appear to get the key
> >after it has actually been processed by Swing and any default operations
[quoted text clipped - 3 lines]
>
> see http://mindprod.com/jgloss/event11.html

Yes, I have seen that now (thank you) but I would like to know why the
following code does not work.  I am trying to allow cursor key movements in
a JTextArea but disallow all other keystrokes but the code disallows up
arrow and down arrow etc. as well.  I have tried using the KEY_PRESSED event
too but it still does the same thing.  It seems the arrow keys are not being
detected at all.  I am using JDK 6.0 by the way but I doubt that is the
problem.

   class ConsoleTextArea extends JTextArea
   {
    protected void processKeyEvent(KeyEvent event)
    {
     switch (event.getID())
     {
      case KeyEvent.KEY_TYPED:
       switch (event.getKeyChar())
       {
        case KeyEvent.VK_UP:
        case KeyEvent.VK_DOWN:
        case KeyEvent.VK_LEFT:
        case KeyEvent.VK_RIGHT:
        case KeyEvent.VK_KP_UP:
        case KeyEvent.VK_KP_DOWN:
        case KeyEvent.VK_KP_LEFT:
        case KeyEvent.VK_KP_RIGHT:
         super.processKeyEvent(event);
         break;

        default:
         event.consume();
         break;
       }
       break;
     }
    }

Thanks,

Wes
jonck@vanderkogel.net - 10 Nov 2005 12:47 GMT
I think your problem lies in this (quoted from the JavaDoc for
KeyEvent):

"Notes:
Key combinations which do not result in Unicode characters, such as
action keys like F1 and the HELP key, do not generate KEY_TYPED events."
Wes Harrison - 10 Nov 2005 12:49 GMT
> I think your problem lies in this (quoted from the JavaDoc for
> KeyEvent):
>
> "Notes:
> Key combinations which do not result in Unicode characters, such as
> action keys like F1 and the HELP key, do not generate KEY_TYPED events."

So how do I process them then?  As I said, using the KEY_PRESSED event has
the same result.

Wes
jonck@vanderkogel.net - 10 Nov 2005 13:46 GMT
> So how do I process them then?  As I said, using the KEY_PRESSED event has
> the same result.

I have written an SSCCE for you, if you run it, typing in the TextArea
will result in showing you in the console which events were captured
and its details. As you will see the KEY_PRESSED event is captured.

Kind regards, Jonck

import java.awt.Dimension;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JTextArea;

/**
* @author jonck
*/
public class TestWindow {
   private JFrame window;
   private ConsoleTextArea text;

   public static void main(String[] args) {
       new TestWindow();
   }

   public TestWindow() {
       initComponents();
       buildPanel();
   }

   private void initComponents() {
       setWindow(new JFrame());
       setText(new ConsoleTextArea());
       getText().setPreferredSize(new Dimension(100, 100));
   }

   private void buildPanel() {
       getWindow().getContentPane().add(getText());
       getWindow().pack();
       getWindow().setVisible(true);
   }

   private class ConsoleTextArea extends JTextArea {
       protected void processKeyEvent(KeyEvent ke) {
           super.processKeyEvent(ke);
           System.out.println(ke);
       }
   }
   public ConsoleTextArea getText() {
       return text;
   }
   public void setText(ConsoleTextArea text) {
       this.text = text;
   }
   public JFrame getWindow() {
       return window;
   }
   public void setWindow(JFrame window) {
       this.window = window;
   }
}
jonck@vanderkogel.net - 10 Nov 2005 13:54 GMT
I just looked at the output of my own program a little closer :-) and I
have found the problem you are facing.

A sample output line is:
java.awt.event.KeyEvent[KEY_PRESSED,keyCode=39,keyText=Right,keyChar=Undefined
keyChar,keyLocation=KEY_LOCATION_STANDARD] on
testCaptureKeyEvents.TestWindow$ConsoleTextArea[,0,0,100x100,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=null,alignmentY=null,border=javax.swing.plaf.basic.BasicBorders$MarginBorder@79dce4,flags=296,maximumSize=,minimumSize=,preferredSize=java.awt.Dimension[width=100,height=100],caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=128,g=128,b=128],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=apple.laf.CColorPaintUIResource[r=0,g=0,b=0],selectionColor=apple.laf.CColorPaintUIResource[r=180,g=213,b=213],colums=0,columWidth=0,rows=0,rowHeight=0,word=false,wrap=false]

As you can see, it says "keyChar=Undefined". Since you are testing for
keyChar's in your code switch (event.getKeyChar())

Your case statements will never be valid, since the keyChar is
undefined. So the solution for you is instead of testing for keyChar's,
you will want to test for keyCode's, which are defined (for example,
the right arrow is keyCode 39, left is keyCode 37).

Kind regards, Jonck
Roedy Green - 10 Nov 2005 14:40 GMT
>I have written an SSCCE for you, if you run it, typing in the TextArea
>will result in showing you in the console which events were captured
>and its details. As you will see the KEY_PRESSED event is captured.

I have a similar program that gives detail on each key event.
See http://mindprod.com/products1.html#KEYPLAY
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.