I have a Class that extends JTextField where I’d like to have CTRL-Shift-O do something. I had been listening for it in
JTextFieldExtension.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e)
{
}
}
with the help of e.isControlDown() and e.isShiftDown(), and that worked fine. But I noticed that the text in the field was also shifting from the left to the right side. Apparently this is a default behavior of the JTextField. So I found this thread on SO which appeared to be helpful:
How to disable default textfield shortcuts in JTextField
From that thread, calling jtextField.getInputMap().setParent(null); did deactivate that behavior. But it also got rid of Ctrl-C and other useful mappings I still want to keep. So I tried the suggested methods for removing just the KeyStroke “ctrl shift O”. But none of them seem to work.
Currently in the class’s constructor I have the following:
this.getInputMap().put(KeyStroke.getKeyStroke("shift ctrl pressed O"), null);
KeyStroke[] strokes = this.getInputMap().allKeys();
for (KeyStroke ks : strokes)
{
System.out.println(ks.toString());
}
It doesn’t work despite the System.out.Println showing “shift ctrl pressed O” as one of the allKeys that it lists. I also tried calling InputMap.remove instead of Put(), without success.
What am I missing?
Removing keys from the input map does not seem to be working as I would expect in this case, but you can override the installed action to effectively disable the ‘switch component orientation’ thing. In Swing, the input map maps keystrokes to objects (usually Strings) which work as identifiers for the action map, which again holds the corresponding action. A simple way of disabling ctrl+shift+O would therefore be as follows:
This simply remaps the keystroke to something not contained in the action map (the String “Nothing”, might as well be “Foo”), therefore nothing happens when you press ctrl+shift+O.
Edit: I can see that this was proposed in the thread you linked. I did, however, confirm that the above code will work for a text field. If it doesn’t for you, please provide a short example of the call in your code.