This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

A good keyListener for mulitple key's pressed  RSS feed

 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just wondering of the best, or maybe the most efficient, at least performance wise, of implementing a keylistener for multiple keys? The app simply needs to take control + T to put a new tab, like firefox.

Thanks in advance,

Christopher Dancy
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
KeyListeners need focus, KeyBindings don't
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I took your advice and looked up keybindings, however I am a bit stuck... I used this code to register all the keys I want, which are alt, control, s and t


super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, 0),"VK_CONTROL");
super.getActionMap().put("VK_CONTROL", new GlassPaneKeyAction(17));
super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0),"VK_ALT");
super.getActionMap().put("VK_ALT", new GlassPaneKeyAction(18));
super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0),"VK_S");
super.getActionMap().put("VK_S", new GlassPaneKeyAction(83));
super.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_T, 0),"VK_T");
super.getActionMap().put("VK_T", new GlassPaneKeyAction(84));


yet in the class GlassPaneKeyAction() that I use, which simply system.out's what number is being passed, it does not work for alt or control...am I doing something wrong?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you don't register all the keys, but the keys with their modifiers (getKeyStroke's 2nd parameter is for modifiers)

for control-T

.put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, 0),"VK_CONTROL"); //remove the line
.put(KeyStroke.getKeyStroke(KeyEvent.VK_T, 0),"VK_T"); //note the 0 - modifiers

becomes
.put(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_DOWN_MASK),"VK_T");
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok that works, however to better understand things, how would I go about just getting a response for only the control key than? Say if i just press the control key i want to output "hello world".
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> how would I go about just getting a response for only the control key than?
> Say if i just press the control key i want to output "hello world".

I've never written anything just for the control key (such a bad idea in a program),
but, just for the exercise, a minute or so of tinkering and this seems to work OK

.put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL,InputEvent.CTRL_DOWN_MASK)

doesn't work with 0 as modifiers (like a normal key)
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael, I figured that out as well, but seemed silly to me to use VK_CONTROL and InputEvent.CTRL_DOWN_MASK seemingly dont they both do the same thing? Anyway it's not a big deal I was simply wondering how it worked was all which is why I asked the question, not that I'm actually doing anything useful with a simple CTRL button being pressed.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm amazed it works at all.

consider the alt key and setting up a keyBinding to do something.

common user practice is alt-tab to switch between apps, only now
it will do something, possibly totally unwanted, prior to switching
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted a question on the java.sun forums here Shift + Tab Key for trying to get the shift or tab keys working with binding but i've no luck thus far. Any ideas?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just followed the advice given in the link, and shift-tab works fine
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read the java doc's and still have gotten nowhere, here is what I tried first...



This did not work. It cleared the focus traversal keys but i still got no response with the following code...



where Tab_Projects and Tab_Current_Project_Files are AbstractActions which simply spit out a line telling me i've reached it. Where am I going wrong?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS

there's a BACKWARD set as well - and that's what shift-tab is
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok that did work for the shift and tab key. However it still does not work for control + tab or control + shift + tab, which I assumed is another type of traversal, so I cleared all those as well but still nothing..
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> However it still does not work for control + tab or control + shift + tab

both combos worked fine for me
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't mind taking a look at my exact code and see how it differs from yours...



This is my exact code and I'm not getting any input from either of those.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
differences seem to be:

p.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,java.util.Collections.EMPTY_SET);
p.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,java.util.Collections.EMPTY_SET);

where 'p' is a JPanel, and you appear to be extending a JFrame (super.....)
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still nothing....very interesting, as i've now tested this on a windows machine as well ( i use linux). Would it make a difference because I'm using a GlassPane for this?
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok that solved the problem. As soon as I removed it from the GlassPane( which originally had a keylistener, which is how this all got started) and added this to the programs main panel it solved the problem. Which makes absolute sense to me now. So thank you Mike for dealing with me and answering my questions and using a good amount of patience going back and forth with me these past days. Thank you.


Sincerely,
Christopher Dancy
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Would it make a difference because I'm using a GlassPane for this?

depends on what you're doing with the glassPane - remove it, see what happens

[edit]
didn't see your final post, above is irrelevant now - anyway, it's working now
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!