• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Control as a keystroke?!

 
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 ]
 
Marshal
Posts: 64705
225
  • 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).
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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").
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!