• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to get the key code for a character (mnemonic in AbstractAction)  RSS feed

 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a problem regarding mnemonics. I would like to add mnemonics to a subclass of AbstractAction. However, I am using a resource bundle, and the action text differs in different languages. Some titles don't even have common letters, so I put the mnemonics to the resource bundle too.

Now my problem is that there seems to be no method that accepts characters as mnemonics. Even worse, I can't seem to find a method that accepts a character and returns the corresponding KeyEvent keycode! I tried

, but it returned 0, as the KEY_TYPED and the KEY_PRESSED, KEY_RELEASED events are entirely separated in the API.

I know I could just assume that VK_A = 65 (ASCII value), and so on, but it isn't very robust...
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Nemeskey:
...problem is that there seems to be no method that accepts characters as mnemonics.


Check out
Where the "key" is Action.MNEMONIC_KEY
 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but it expects a key code, one of the KeyEvent.VK_XX constants, and not a character, as 'A'.

Of course, I can assume that (int)'A' == 65 == KeyEvent.VK_A, and that is what I am doing now, but that is not very robust, is it? What I am looking for is an API way that can "translate" 'A' to VK_A.
[ July 10, 2008: Message edited by: David Nemeskey ]
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This will display 'N' as the mnemonic in 'New'
 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer, but I already know that.

What I would like to know, if I have a character, like 'N', how do I get its key code; that is, KeyEvent.VK_N. I don't know the character at compile-time; it is read from a file, so the code has to be generic. It should be without any ASCII magic, only by calling API methods.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Nemeskey:
Thanks for your answer, but I already know that.

What I would like to know, if I have a character, like 'N', how do I get its key code; that is, KeyEvent.VK_N. I don't know the character at compile-time; it is read from a file, so the code has to be generic. It should be without any ASCII magic, only by calling API methods.



In your case you can write a method which will return the required char and call it like


The implementation of the getMyMnemonicChar() will have code to load the appropriate resource bundle and stuff.
[ July 10, 2008: Message edited by: Maneesh Godbole ]
 
David Nemeskey
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that is what I am doing now, but it is not robust enough. What if a system uses a different enconding, where (int)'N' != VK_N?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Nemeskey:
Yes, that is what I am doing now, but it is not robust enough. What if a system uses a different enconding, where (int)'N' != VK_N?


VK_N returns the ASCII value. I dont think system encoding comes into the picture. But then someone with a computer science background or better knowledge than me can shed some light on this. I am a bit unsure beyond this point.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!