2

How can I alter the Input/Action map so when I bind an action to a specific key it also binds an action to Some Modifier + specific key?

i.e. SHIFT_DOWN_MASK + specific key

So all of my key bindings work without modifiers. I am assigning a sound file to a certain letter on the key board and when I press that key, the sounds file plays. I also have code to loop the sound file, toggle whether or not it loops, and check if it loops. I want to have it so that, in addition to binding the play() action to the key, it will bind the toggleLoop() action to the action SHIFT_DOWN_MASK + specific key.

Where I bind actions:

    @SuppressWarnings("serial")
    public void bindKey(JPanel base)
    {
        base.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),"play"+key);
        base.getActionMap().put("play"+key, new AbstractAction(){
            public void actionPerformed(ActionEvent e) 
            {
                System.out.println(key + " Pressed");
                play();

            }
        });

        //assigning action just the key pressed

        base.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke((char)(key-32), InputEvent.SHIFT_DOWN_MASK),"loop"+(char)(key-32));
        base.getActionMap().put("loop"+(char)(key-32), new AbstractAction(){
            public void actionPerformed(ActionEvent e) 
            {
                System.out.println(key + " toggled loop");
                toggleLoop();
                //fix toggle

            }
        });

        //^ where I try to assign action to modifier



    }
Alex
  • 21
  • 2

1 Answers1

1

FIXED: Changed to (char)(key-32) so it would assign it to the lowercase character, because it was setting 'W' instead of 'w' when I used the shift modifier.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • 2
    @Alex hmm .. thinking in terms of lower/uppercase is a bit off scope in keyBindings: it's always a-single-key plus (optionally) a-modifier. How/if that combination would map to a keyChar is irrelevant. So, if this is the "solution" something in the setup smells fishy :-) Were/how do you get the _key_ ? – kleopatra Dec 06 '12 at 09:22
  • @kleopatra I am getting the keys from the last button I clicked's get text. so keyboardBtns[i].getText().charAt(0), it may not be very efficient but I am just trying to get a working prototype that I can go back and make more efficient/more logical. – Alex Dec 18 '12 at 19:01