Detect Control-Plus and Control-Minus

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 nothingIt 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.

KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, KeyEvent.SHIFT_DOWN_MASK | KeyEvent.CTRL_DOWN_MASK)This works indeed with CTRL_DOWN_MASK alone (not tested with the other mask), thanks.
However, this solution has two drawbacks:
1. The accelerator string is shown as Ctrl-=, while I want Ctrl-+ to be shown
2. '+' key may not coincide with '=' in non-English keyboards (e.g. it doesn't coincide in Spanish keyboards)
I've found a solution that required a bit more work:
1. Create the actions with the keyStroke you want to be shown (in my case, Ctrl-- and Ctrl-+):
ctrlPlus  = getKeyStroke("ctrl typed +");  // Ctrl-+ is shown
ctrlMinus = getKeyStroke("ctrl typed -");  // Ctrl-- is shown
Action actionPlus = new AbstractAction() { ... };
actionPlus.setAccelerator(ctrlPlus);
Action actionMinus = new AbstractAction() { ... };
actionMinus.setAccelerator(ctrlMinus);2. Bind the keys you actually want to the corresponding action:
JComponent window = ...;  // root window
InputMap inputMap = window.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = window.getActionMap();
Object actionMapKeyPlus  = "ctrl+";
Object actionMapKeyMinus = "ctrl-";
inputMap.put(getKeyStroke(VK_EQUALS  , CTRL_DOWN_MASK), actionMapKeyPlus);  // + key in English keyboards
inputMap.put(getKeyStroke(VK_PLUS    , CTRL_DOWN_MASK), actionMapKeyPlus);  // + key in non-English keyboards
inputMap.put(getKeyStroke(VK_ADD     , CTRL_DOWN_MASK), actionMapKeyPlus);  // + key on the numpad
inputMap.put(getKeyStroke(VK_MINUS   , CTRL_DOWN_MASK), actionMapKeyMinus); // - key
inputMap.put(getKeyStroke(VK_SUBTRACT, CTRL_DOWN_MASK), actionMapKeyMinus); // - key on the numpad
actionMap.put(actionMapKeyPlus, actionPlus);
actionMap.put(actionMapKeyMinus, actionMinus);More complicated than expected, but at least works.
Thanks for the help.
God bless,
Jaime

Similar Messages

  • MacBook Pro 2.3 os 10.6.8 control-C and Control-v don't work

    On MacBook Pro 2.3 OS 10.6.8, control-c and control-v don't work in any app Please help

    If you mean copy and paste, that's Command c and Command v, not control key.

  • Drag and drop doesn't work and also control C and control V

    drag and drop doesn't work and also control C and control V

    Try this first. On your hard drive, go to Users / yourself / Library / Preferences.
    In that Preferences folder there is a file called com.apple.finder.plist.
    Trash that file and restart.

  • Control cost and control quantity in production order cost analysis

    Hi,
    When I checked the WIP value, I found there are two field, control cost and control quantity, in the cost analysis of production order which has some relationship with WIP calculation. But I do not know where the control cost from and by what the control quantity is controlled. Could anyone who know advise please?
    Regards

    Anyone who can help please?

  • Difference control refnum and control reference

    Hi guys.
    I am new on labview reference, can you explain difference between control refnum and control reference. 
    Gary Wang

    Are you sure you don't mean "refnum control"? That is a front panel control that can be selected as a reference to any one of several data types such as application, VI (including strictly typed), control or indicator. Whereas the "control reference" is a block diagram object that points to a specific control on your front panel (it can also be "linked" to other objects so it is similar to a "refnum control" in that respect).
    “A child of five could understand this. Send someone to fetch a child of five.”
    ― Groucho Marx

  • What does the shortcut control-3 and control-4 do, and how can I turn it off?

    I'm trying to create some macros and remap my keyboard for a game, but running into some issues not being able to remap control-3 and control-4. I finally figured out that control-1 and 2 were used by Mission Control, and once that was off I gained control over those keys. But for the life of me I can't figure out what control-3 and control-4 do. I've searched various shortcut lists, but none seem to list this combination (except that it controlled Spaces, but that's gone with the new OSX).
    Does anyone know what these keys are shortcuts for and how I can release them? Thanks!

    user293, you must not understand what I mean, because this particular thing is brand new this past week. (I guess you think I am talking about a closely related thing, because I know that most of the way it is now has been this way for a while.)
    Thanks cor-el, that totally fixed it.
    Although I am dismayed that it's not just a search option and requires user hacking.

  • I want to use Control-W and Control-T to close and open tabs in OS X

    On Windows, you can use Ctrl-W and Ctrl-T to close and open tabs. On OS X, you have to use Cmd-W and Cmd-T instead (this bothers me because it is asymmetric with using Ctrl-Tab and Shift-Ctrl-Tab to navigate between tabs). Ctrl-W and Ctrl-T don't seem to do anything by default, so it would be nice if both Cmd or Ctrl could be used as a modifier for closing and opening tabs. Is there a way to accomplish this?
    I tried using the System Preferences > Keyboard > Keyboard Shortcuts tab to set Ctrl-W and Ctrl-T to operate the Close Tab and New Tab menu options, but, this does not work; when I press those key combinations, the File menu briefly flashes, but nothing else seems to happen.
    Thanks in advance for your help!

    Try this addon: keyconfig
    http://forums.mozillazine.org/viewtopic.php?t=72994

  • Tolerance Plus and Tolerance Minus for each Truck entry

    For each material & Vendor, we have different Weighbridge tolerance.
    for eg Material A : Weighbridge tolerance 5%.
    for each truck entry, weighbridge tolerance is checked & Net Qty is captured for GRN.
    How to capture Tolerance % during each Goods receipt (different for each material).
    Mit

    Thanks for the suggestion. I've edited the query and it works - if I limit it. Getting ORA-01452 error on the tablespace size when going for the full scope.
    Further Question: the merge example in the 10g documentation shows:
    WHEN MATCHED THEN UPDATE SET D.bonus = D.bonus + S.salary*.01
    WHEN NOT MATCHED THEN INSERT (D.employee_id, D.bonus)
    I only want to Insert - is it ok if I leave out the 'when matched'?

  • Control center and CBII archive

    The latest versions of CC and CBII do not work for overclocking and in fact are barely adequate for stock cpu clocks. 
    So, is there an archive somewhere here for older versions that DO work, or if not can one be created?  I would post my working versions here if I could.

    I never used control center so I could easily be wrong.  There are two of them on my cd control center and control center II.
    This is mine from my Dropbox.  Many don't trust files posted like this which is why I am asking if it could be possible to set up an archive here.
    https://www.dropbox.com/sh/gpalg0tpyyfcivy/AAA_vvHgq7MUkdcXPH3Nh5rWa/CLICKBIOSII.7z

  • My plus and minus volume control doesn't work.. Is there any way of turning them on or off? Or is my phone faulty?

    My plus and minus volume control doesn't seem to work.
    Can I switch the volume on?

    Hi Timjazzhair,
    Go to Settings ==> Sounds and in 'Ringer and Alert' set 'Change with Buttons' to YES.
    Woops! Meg beat me to it!

  • AE lost my control plus, minus and reduce in Top RH corner? Tech Help!

    Hi all,
    I have a strange thing going on. I could swear there used to be the standard Browser control of  - + and reduce box in the Top RH corner like in any window and every other adobe product. However, I got a virus it was hiding in Adobe reader. I believe I cleaned it as I scanned with two different virus companies. Both came up nothing found. Cleared my Temp folder, dumped trash after un installing AE. Re installed AE and still no window control?
    Plus in my system start tray where my programs are where Adobe Master Collection Folder is I open that an my AE icon is a generic icon, although it will work as a shortcut.
    The other thing is that it  is evident in my task bar as a 'pinned icon' Here is a screen shot of AE with no + or - in top corner...
    Win7 Pro Srv Pk1
    3770k intel
    z77 mobo
    Nividia quadro 4000
    All is updated drivers/versions
    I also lost Adobe Reader as I had to uninstall as that was the Trojan file home. I stuck the disc in but did not see the icon to re-install.

    Ouch....HAHAHHAHAHAHHA LOL! THE PAIN my stomach from the laughter..... That was it! UGHH Damn you Adobe and remembering my last screw up.....LOL! Thanks Todd. You are correct.
    Mylenium Not sure what you are referring to?  Is that for the 2nd part of my question for Reader? I tried you command of Cntrl & # it went from one panel to the other in  AE..
    Now if I could get my Obsessive Compulsive disorder to settle down about the dead icon in my Program file under start bar. I will follow the link for the cleaner tool...
    Thanks guys
    Regards
    Simon
    EDIT: OCD handled. Restart Computer after reinstall to place icons globally, within my Workstation.

  • HT204053 Is there a limit to the number of devices I can have on one account.  I have all two of my kids ipods, plus, mine, an ipad, and my iphone, and need to add one more.  I do this so that I have control over and can see what they are downloading.

    Is there a limit to the number of devices I can have on one account.  I have been putting my kids' ipods on my account so that I can manage their downloads, and control it.  Then I just create playlists for them specifically.  Now, adding another one for my youngest, but have no idea if there are limits. 

    Well the part about putting songs in the "iTUnes Music" folder I only brought up because it seemed you liked things very organized. You could use iTunes to so all the organizeing. Plus once you add something to the iTUnes library, if you change ANYTHING about it in windows explorer, like its name, change the name of the folder its in or move it to another folder, iTUnes "Looses Track" of it. Its not like other programs that "Watch" files and folders for changes outside its "iTUnes Music" folder
    But to have iTUnes auto search, you have to delete a hidden pref file. After doing this, when you open iTUnes again it will throw up a setup wizard, just like its the first time ever installing it. Its in this wizard where you get your ONLY chance to have iTUnes auto search and add files from the system. Just goto the link below and follow Scott P's steps
    http://discussions.apple.com/message.jspa?messageID=607357
    Also since your new to the ipod, you my want to check out the "iPod 101" section so you can learn the ropes of everything
    http://www.apple.com/support/ipod101/

  • Before I upgraded my iPad mini, you could turn off apps to save battery power by double clicking the control button and then holding your finger on the app button until the minus sign appeared.  That doesn't work anymore.  How do you turn off the apps wit

    Before I upgraded my iPad mini, you could turn off apps to save battery power by double clicking the control button and then holding your finger on the app button until the minus sign appeared.  That doesn't work anymore.  How do you turn off the apps with ios7

    Now you swipe downward, the app will appear and you delete, keep scolling to the right as in the past.  With the new operating system you also swipe downward to get the search bar to find an app

  • How to generate a random number within a specified tolerance and control that tolerance.

    I am trying to simulate data without using a DAQ device by using a random number generator and I want to be able to control the "range" of the random number generated though.  On the front pannel, the user would be able to input a nominal number, and the random number generator would generate numbers within plus or minus .003 of that nominal number.  The data would then be plotted on a graph.  Anyone know how to do this?  Or if it's even possible?
    Just for information sake, the random number is supposed to look like thickness measurements, that's why I want to simulate plus and minus .003 because the data could be thinner or thicker than the nominal.  
    Thanks in advance!!
    Solved!
    Go to Solution.

    You can create a random number with a gaussian probability profile using two equal distributed (0...1) random numbers and applying the Box Muller transform.
    I wrote one many years ago, her it is. (LabVIEW 8.0). 
    Message Edited by altenbach on 04-12-2010 09:13 PM
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    BoxMuller.vi ‏10 KB

  • No volume control elements and/or devices found

    Hi, I recently installed Arch Linux and I followed the tutorials on how to configure it. At this moment I have Gnome installed and it is working fine, I only have one problem.
    Every time if I try to access volume control I get the following error:
    "No volume control elements and/or devices found"
    I'm relatively new to Linux so it might be me just being stupid(Maybe I just don't have the right packages installed) but I've searched the web and I didn't find anything that could help, I also searched this site but there are only a few things about sound problems. I think the problem is caused because Linux is not detecting my sound card (its an on board sound card) or maybe I have not given the command to detect it/activate it. In Google I've found that 'lsmod' and 'lspci' commands are for detecting drivers, so I executed those in my terminal and put them here, I hope this might help fixing my problem.
    Module Size Used by
    ipv6 252608 8
    nvidia 6828532 22
    jfs 172540 1
    ext2 57352 1
    snd_seq_oss 29184 0
    snd_seq_midi_event 6528 1 snd_seq_oss
    snd_seq 46672 4 snd_seq_oss,snd_seq_midi_event
    snd_seq_device 6796 2 snd_seq_oss,snd_seq
    ppdev 7556 0
    eth1394 16772 0
    lp 9604 0
    snd_intel8x0 28828 0
    rtc 10416 0
    ppp_generic 23444 0
    snd_pcm_oss 39072 0
    snd_mixer_oss 14464 1 snd_pcm_oss
    sg 26652 0
    usbhid 34592 0
    hid 25088 1 usbhid
    slhc 6272 1 ppp_generic
    shpchp 29588 0
    pci_hotplug 27976 1 shpchp
    ff_memless 5256 1 usbhid
    parport_pc 35940 1
    tsdev 6336 0
    parport 31176 3 ppdev,lp,parport_pc
    i2c_sis96x 5124 0
    i2c_core 17792 2 nvidia,i2c_sis96x
    snd_intel8x0m 14476 0
    snd_ac97_codec 91940 2 snd_intel8x0,snd_intel8x0m
    ac97_bus 2432 1 snd_ac97_codec
    snd_pcm 68484 4 snd_intel8x0,snd_pcm_oss,snd_intel8x0m,snd_ac97_codec
    snd_timer 18820 2 snd_seq,snd_pcm
    snd 43492 10 snd_seq_oss,snd_seq,snd_seq_device,snd_intel8x0,snd_pcm_oss,snd_mixer_oss,snd_intel8x0m,snd_ac97_codec,snd_pcm,snd_timer
    pcspkr 2816 0
    evdev 8192 0
    ohci1394 31920 0
    ieee1394 84056 2 eth1394,ohci1394
    sis_agp 6404 1
    agpgart 26200 2 nvidia,sis_agp
    soundcore 6496 1 snd
    snd_page_alloc 7816 3 snd_intel8x0,snd_intel8x0m,snd_pcm
    8139too 22912 0
    mii 4992 1 8139too
    ext3 119688 1
    jbd 55336 1 ext3
    mbcache 7044 2 ext2,ext3
    ehci_hcd 30092 0
    sr_mod 14372 0
    cdrom 34464 1 sr_mod
    sd_mod 16768 5
    ohci_hcd 18948 0
    usbcore 115464 4 usbhid,ehci_hcd,ohci_hcd
    ata_generic 5380 0
    pata_sis 10508 4
    libata 94100 2 ata_generic,pata_sis
    00:00.0 Host bridge: Silicon Integrated Systems [SiS] 651 Host (rev 01)
    00:01.0 PCI bridge: Silicon Integrated Systems [SiS] Virtual PCI-to-PCI bridge (AGP)
    00:02.0 ISA bridge: Silicon Integrated Systems [SiS] SiS962 [MuTIOL Media IO] (rev 04)
    00:02.1 SMBus: Silicon Integrated Systems [SiS] SiS961/2 SMBus Controller
    00:02.3 FireWire (IEEE 1394): Silicon Integrated Systems [SiS] FireWire Controller
    00:02.5 IDE interface: Silicon Integrated Systems [SiS] 5513 [IDE]
    00:02.6 Modem: Silicon Integrated Systems [SiS] AC'97 Modem Controller (rev a0)
    00:02.7 Multimedia audio controller: Silicon Integrated Systems [SiS] AC'97 Sound Controller (rev a0)
    00:03.0 USB Controller: Silicon Integrated Systems [SiS] USB 1.0 Controller (rev 0f)
    00:03.1 USB Controller: Silicon Integrated Systems [SiS] USB 1.0 Controller (rev 0f)
    00:03.2 USB Controller: Silicon Integrated Systems [SiS] USB 1.0 Controller (rev 0f)
    00:03.3 USB Controller: Silicon Integrated Systems [SiS] USB 2.0 Controller
    00:09.0 RF controller: Advanced Micro Devices [AMD] Am 1771 MBW [Alchemy] (rev 04)
    00:0b.0 Ethernet controller: Realtek Semiconductor Co., Ltd. RTL-8139/8139C/8139C+ (rev 10)
    01:00.0 VGA compatible controller: nVidia Corporation NV34 [GeForce FX 5200] (rev a1)
    Any help would be appreciated because this is  getting very frustrating >_>
    Last edited by NomenNescio (2007-04-28 10:50:10)

    ryurhrt wrote:
    NomenNescio wrote:
    Thanks for the help, didn't get the sound to work but yet because I kinda broke my linux(wont boot)>_>
    Gonna reinstall it, things usually go better the second time.
    make sure u have adding the user to audio group in order to access sound device.
    gpasswd -a user audio
    Yes, I think that was the problem. Anyways, I reinstalled Arch Linux and everything is working fine now.

Maybe you are looking for