Hello,
I work on an app that uses single-character app-wide keyboard
accelerators to trigger Actions. Not ideal, but that's what the legacy
version of the app does, so we are honoring that.
We have a dockable panel that contains a JTextField, and when the user
types in that field, the actions that correspond to keys pressed are
fired. We do not want this to happen, but are unable to figure out how
to stop it. I scoured this and other groups, and the intarweb, with no
success.
Does anyone have any ideas as to how to make a JTextField ignore all
app-wide keymaps?
I tried code like this (_pickNameText is the name of the JTextField,
and "c" is one of the accelerator keys we're trying to ignore):
Action doNothing = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//do nothing
System.out.println( "hi" );
}
};
_pickNameText.getInputMap().put(KeyStroke.getKeyStroke("c"),
"doNothing");
_pickNameText.getActionMap().put("doNothing", doNothing);
_pickNameText.getActionMap().put("c", doNothing);
And I also tried code like this:
KeyStroke ksC = KeyStroke.getKeyStroke('c');
_pickNameText.getKeymap().removeKeyStrokeBinding( ksC );
_pickNameText.getKeymap().setResolveParent( null );
and all I ever got was blocking the input of the actual 'c' key, which
wasn't the intention.
Any help from you gurus out there would be most appreciated!
Thanks, --mtnK
Christian Kaufhold - 09 Jan 2005 15:27 GMT
> I work on an app that uses single-character app-wide keyboard
> accelerators to trigger Actions. Not ideal, but that's what the legacy
[quoted text clipped - 11 lines]
> I tried code like this (_pickNameText is the name of the JTextField,
> and "c" is one of the accelerator keys we're trying to ignore):
It is unclear what you want to achieve. Do you want the keyboard
accelerators to work (and the character not to be inserted into the
text field), or vice-versa.
How are the accelerators implemented?
Christian
mtnkodiak@gmail.com - 10 Jan 2005 16:51 GMT
Thank you for your response. Sorry for being unclear.
I want the characters to be inserted into the text box normally,
without triggering any previously-defined app-level actions to occur at
all. Regular text-editing accelerators like ctrl-C and ctrl-V are
desireable, just not any additional accelerators defined in our app.
The accelerators are implemented in the corresponding Action
constructors when the actions are initialized. These actions are
initialized and set up before the JTextField control in question is
even created. As far as I'm concerned, the accelerators are standard
action mnemonics.
Christian Kaufhold - 11 Jan 2005 16:44 GMT
> I want the characters to be inserted into the text box normally,
> without triggering any previously-defined app-level actions to occur at
[quoted text clipped - 6 lines]
> even created. As far as I'm concerned, the accelerators are standard
> action mnemonics.
Use "typed" key strokes for the actions.
Otherwise (if you used "pressed" key strokes), the pressed key strokes
will cause the action and the following "typed" key strokes will also
insert into the text field. There is nothing that can be done about it
as the pressed key stroke is processed even before (possibly) a type
key event follows.
Alternatively, map all "pressed" key strokes in the JTextField input
map to a disabled action.
Christian