• Post Reply Bookmark Topic Watch Topic
  • New Topic

Control as a keystroke?!  RSS feed

 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this really not possible?! I see how to get things like control and shift as modifiers, but what if I just want to know when the user hits control?!

getInputMap().put( KeyStroke.getKeyStroke( KeyEvent.VK_SPACE , 0 ), "what a crock" );
getActionMap().put( "what a crock" , action );

works fine, but with KeyEvent.VK_CONTROL (or ("CONTROL" , 0)), nothing.
[ June 19, 2008: Message edited by: Nick George ]
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to bump myself here, but it seems very strange that you just can't know when the user has hit control in a JFrame. Anyone?
[ June 19, 2008: Message edited by: Nick George ]
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry that you haven't had a reply.

Is there anything useful in this page from the Java Tutorials? It mentions caps-lock, so you might get the same thing to work for CTRL.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just reread my inital post, and, it stands to reason that I didn't get much answer. Let me explain myself a bit better:

KeyListeners are wonderful and easy to use. The problem is that they rely on focus, which apparently is very hard for frames. I can't add the listener directly to my panel, because I need it to respond any time the whole JFrame is focused. You can't add listeners to JFrames, because there are too many different panes that the odds of the right one being focused is low. Thus, I've found many recommendations to use input/action maps in a component. The problem seems to be that the input maps only recognize control as a mask for other keystrokes. I've done a lot of googling on this, and I'm amazed that I'm seemingly the first to run into this problem (and whine about it on the internet).
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found this interesting, but the answer wasn't difficult to find. To aid your debugging ability for future problems, I've left in the code I wrote to lead me to the answer, which is to use KeyStroke.getKeyStroke("ctrl CONTROL").
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!