When I use this code, I get "Delete" as a shortcut. I want to get "Del" (delete key)
private JMenuItem delRef = null;
del = new JMenuItem("delete");
del.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
From an answer in a related post, I found out that the keyCode
(first parameter of getKeyStroke
) which you are requesting is 110
.
Then I did:
System.out.println(KeyStroke.getKeyStroke(110, 0));
Which prints pressed DECIMAL
.
So the keyCode
you are looking for is actually KeyEvent.VK_DECIMAL
and it will only work (at least as far as I tested it) for the numpad's delete key (only when numlock is on).
So to answer, you can use:
JMenuItem item = new JMenuItem("Delete");
item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DECIMAL, 0));
item.addActionListener(e -> System.out.println("Action delete!"));