Combined keystrokes as shortcuts - e.g. CTRL+ALT+Z

I would like to map a keystroke to an action in a panel. Assume the panel contains a text field and a button. Assume I want to assign the stroke "CTRL+ALT+Z" to activate the button.

Modifying input map and action map works great, no problems there.

The problem is that Swing receives another keystroke which in this case is an glyph of the letter a and an e back to back. I think that this keystroke originates from the OS.

So Swing receives:

1) CTRL

2) CTRL+ALT

3) Z modified with CTRL, ALT

4) <strange character>

What happens is that the shortcut works allright, no problems. BUT ... if the textbox had focus when pressing the key combination it will receive this strange character.

My question is: is there any way (except finding all these combinations and blocking them explicitly) to catch these combinations and prevent them from having effect. Is there a way to block the combination keystroke from appearing (step 4) the moment I handle the preceding keystroke (step 3)?

Thanks,

Roger

[1074 byte] By [rlaenena] at [2007-10-3 9:26:49]
# 1

> Assume the panel contains a text field and a button.

Why do we need to assume? Post your demo code so we can see the behaviour. Maybe the problem is with your code. Maybe its a version platform issue.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-15 4:41:06 > top of Java-index,Desktop,Core GUI APIs...
# 2

If a small test application can help, here it is. The demo app creates a JFrame that contains a JPanel and this contains a JTextField.

Put the cursor in the text field, and press CTRL+ALT+z (example keystroke; there are lots of combinations). The acton attached to the input map is executed correctly (wanted behaviour), but the text field is also modified with a unicode character (unwanted). Is there a way to prevent this?

import javax.swing.*;

import java.awt.*;

import java.awt.event.KeyEvent;

import java.awt.event.ActionEvent;

public class KeystrokeProblem

{

private static final KeyStroke EXAMPLE_STROKE = KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_MASK + KeyEvent.ALT_MASK);

public static void main(String[] aArgs)

{

final JFrame lFrame = new JFrame();

lFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel lPanel = new JPanel();

final JTextField lText = new JTextField();

lText.setText("Try to enter CTRL+ALT+Z");

lPanel.add(lText, BorderLayout.CENTER);

lFrame.setContentPane(lPanel);

// Here we want to capture the keystroke and do some action.

final InputMap lIn = lPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

lIn.put(EXAMPLE_STROKE, "DOIT");

lPanel.getActionMap().put("DOIT", new AbstractAction()

{

public void actionPerformed(ActionEvent e)

{

System.out.println("Bingo!");

}

});

lFrame.pack();

lFrame.setVisible(true);

}

}

rlaenena at 2007-7-15 4:41:06 > top of Java-index,Desktop,Core GUI APIs...
# 3

> If a small test application can help

It always helps. In fact I think it should be mandatory.

It works fine using JDK1.4.2 on XP.

I don't know where the problem is, but you could try adding a dummy CTRL+ALT+Z action to the text components input map so that it intercepts the keystroke and does nothing.

camickra at 2007-7-15 4:41:06 > top of Java-index,Desktop,Core GUI APIs...
# 4

I just tested in on XP, both 1.4.2_08 and 1.5.0_08 jre's show the problem. The unwanted character in this case is the "ae" glyph. The "Bingo" is shown allright, that is not the problem. The problem is the extra chacacter showing up in the text field.

I cannot put the inputmap in the textfield. The real application contains multiple components, one of these should react to the keystrokes. You cannot forsee how the component will be combined in the future.

Indeed, the solution is to collect all unwanted generated keystrokes and adjust all the components, it is a hack.

1) If I want to reuse the component in another app I have to do the same trick,

2) I cannot distribute the component as is.

rlaenena at 2007-7-15 4:41:06 > top of Java-index,Desktop,Core GUI APIs...
# 5
I found the source of the problem, I use the XP international keyboard and this driver generates the extra characters. When switching to another standard keyboard layout in XP the problem did not show up anymore.
rlaenena at 2007-7-15 4:41:06 > top of Java-index,Desktop,Core GUI APIs...