LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-16-2005, 11:25 PM   #1
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Rep: Reputation: 34
Listening to Tab and Enter Keys in Java


Hi,

I've been trying to listen to the Tab and Enter keys in Java Frame, so that I cna perform a custom action:


now I'm using the following methodology:

I add a keylistenter to the frame, and in the keyPressed function I write:

Code:
 if(arg0.getKeyCode() == KeyEvent.VK_TAB)
                {
                        JOptionPane.showMessageDialog(null,"TAB PRESSED");
                }
But When I fire up the applicaiton and press Tab, the dialog box is never displayed, and the control just moves to the next control.

When I display the getKeyCode() in the console, no matter what key I press I always get a '0'. Whereas getKeyChar() displays the characters and number nicely but does not show anything for Enter or Tab.

So how do I trap the key press on Tab and Enter button
 
Old 12-17-2005, 05:41 AM   #2
jamie_barrow
Member
 
Registered: Jul 2003
Location: Gauteng, South Africa
Distribution: Windows XP, yup :D Will be back onto Ubuntu probably someday
Posts: 107

Rep: Reputation: 15
Can you post your code, I can't get a frame to have a keylistener

I tried this but nothing gets printed out:

public MyFrame extends JFrame implements KeyListener {
public MyFrame() {
addKeyListener(this);
/* rest of code */
}

public void keyPressed(KeyEvent e) {
System.out.println(e);
}
}
 
Old 12-17-2005, 06:30 AM   #3
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Original Poster
Rep: Reputation: 34
@Jamie Barrow

Their are three functions you have to implement:
KeyPressed, keyTyped and keyReleased
 
Old 12-17-2005, 06:31 AM   #4
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Original Poster
Rep: Reputation: 34
I also tried putting the code in keyTyped, but it still doesnt trap Tab and Enter keys
 
Old 12-17-2005, 06:35 AM   #5
jamie_barrow
Member
 
Registered: Jul 2003
Location: Gauteng, South Africa
Distribution: Windows XP, yup :D Will be back onto Ubuntu probably someday
Posts: 107

Rep: Reputation: 15
I know about implementing all functions, laziness made me leave them out

Can you post your code? Did you also implement KeyListener? Did you add like I did, coz my program didn't even do anything!
 
Old 12-17-2005, 06:46 AM   #6
jamie_barrow
Member
 
Registered: Jul 2003
Location: Gauteng, South Africa
Distribution: Windows XP, yup :D Will be back onto Ubuntu probably someday
Posts: 107

Rep: Reputation: 15
The following code does trap enter key but not tab key. Since the tab key is used to switch between components, something must be listening for it already and must handle the keyEvent and not pass it on. So something higher up in Swings hierarchy. Dont ask me what though

public class MyFrame extends JFrame implements KeyListener {
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MyFrame();
}
});
}

public MyFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addKeyListener(this);
pack();
setVisible(true);
}

public void keyTyped(KeyEvent e) {
System.out.println(e);
}

public void keyPressed(KeyEvent e) {
System.out.println(e);
if (e.getKeyCode() == 10) {
//Enter pressed
JOptionPane.showMessageDialog(this, "Enter pressed");
}
}

public void keyReleased(KeyEvent e) {
System.out.println(e);
}
}
 
Old 12-18-2005, 06:59 AM   #7
irfanhab
Member
 
Registered: Jan 2004
Location: Pakistan
Distribution: OpenSuse 10.2, Slackware 11, Solaris 10
Posts: 415

Original Poster
Rep: Reputation: 34
Ok I figured out how to trap the TAB key:

This is what you need to do:

component.setFocusTraversalKeysEnabled(false)

After disabling setFocusTraversalKeysEnabled, you will be able to trap the TAB key, but you must handle the focus traversal yourself in the code
 
Old 12-18-2005, 02:18 PM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Actually, the whole idea of "hijacking" the tab key is probably wrong. Please consider how you might be able to "work within the system" by CUSTOMIZING the EXISTING Swing mechanisms, instead of re-writing your own.

If you can do this, you will have a MUCH better, more responsive user interface that gives the end-user a MUCH more pleasant experience,

Please look at the following:
http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html#customFocusTraversal

IMHO .. PSM
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
FC 4. Modify ALT+TAB behaviour to CTRL+TAB xtracto Linux - Software 1 09-22-2005 02:06 PM
Gnome stealing focus, only able to navigate with tab and enter stalefries Ubuntu 2 07-05-2005 02:50 PM
java applet doesn't real all keys nyk Programming 13 02-08-2005 12:04 PM
Configuring (g)vim-tab with C++/Java Mega Man X Programming 2 01-28-2004 06:27 AM
what is the best way for student to enter java code Brain Drop Programming 11 01-01-2004 02:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration