3

I am having trouble mapping the Control-Backspace key to a KeyStroke. The following makes no sense to me.

import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;
public class TestControlBackspace {
    public static void main(String[] args) {
        KeyStroke ks1 = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, KeyEvent.VK_CONTROL);
        KeyStroke ks2 = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, KeyEvent.VK_SHIFT);
        KeyStroke ks3 = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0);
        System.out.println(ks1);
        System.out.println(ks2);
        System.out.println(ks3);
    }
}

Output:

shift pressed BACK_SPACE

pressed BACK_SPACE

pressed BACK_SPACE

Am I missing something here?

Steve Cohen
  • 4,679
  • 9
  • 51
  • 89

1 Answers1

7

You probably forgot to read the documentation. Note that the modifier masks come from a different location than the key pressed.

import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;
public class TestControlBackspace {
    public static void main(String[] args) {
        KeyStroke ks1 = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, InputEvent.SHIFT_DOWN_MASK);
        KeyStroke ks2 = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, InputEvent.CTRL_DOWN_MASK);
        KeyStroke ks3 = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0);
        System.out.println(ks1);
        System.out.println(ks2);
        System.out.println(ks3);
    }
}
Atreys
  • 3,741
  • 1
  • 17
  • 27