Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Detect Control-Plus and Control-Minus

800474Apr 3 2009 — edited Apr 3 2009
Hi,

I want to detect keyboard events in a swing application, with the Control key and the '+' or '-' keys pressed (the ones from the keyboard, not from the numpad).

This is to provide similar behaviour than Firefox for increasing and decreasing the font size.

I have an English keyboard, in which + is with = key, to be pressed with shift, and - can be pressed directly, without shift.

I've tried many ways to achieve this behaviour, but have not succeeded yet.
Platform is Java SE 1.6.0_07 on Red Hat Enterprise Linux 4 with Gnome.
import static java.awt.event.InputEvent.*;
import static java.awt.event.KeyEvent.*;
import static javax.swing.KeyStroke.getKeyStroke;

// Within some method in some class...
KeyStroke ctrlPlus, ctrlMinus;

// Menu items seem correct, but none of the combinations work for me
ctrlPlus  = getKeyStroke(Character.valueOf('+'), CTRL_DOWN_MASK); // menu item shows Ctrl-+
ctrlMinus = getKeyStroke(Character.valueOf('-'), CTRL_DOWN_MASK); // menu item shows Ctrl--

// Menu items seem a bit strange; Ctrl - works, but Ctrl + doesn't
ctrlPlus  = getKeyStroke(VK_PLUS , CTRL_DOWN_MASK); // menu item shows Ctrl-Plus
ctrlMinus = getKeyStroke(VK_MINUS, CTRL_DOWN_MASK); // menu item shows Ctrl-Minus

// None of these combinations work
ctrlPlus  = getKeyStroke("control +"); // menu item shows nothing
ctrlMinus = getKeyStroke("control -"); // menu item shows nothing
It seems I can get the desired behaviour with '+' and '-' of the numpad, but:

1. They are more weird than the normal '+' and '-' keys, which are the ones used by Firefox
2. The menu items show Ctrl-Numpad + and Ctrl-Numpad - as shortcuts, which are weird also
3. Not all keyboards have a numpad.

A solution would be very much appreciated... as well as rewarded with the dukes.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 1 2009
Added on Apr 3 2009
5 comments
1,177 views