Hi friend, Pleas refer below example of 'KeyStroke class'. Which will show you the use of KeyStroke class methods:
Code:
import javax.swing.KeyStroke;
public class KeyTesterDemo {
static class MyActionListener extends AbstractAction {
MyActionListener(String s) {
super(s);
}
public void actionPerformed(ActionEvent e) {
System.out.println(getValue(Action.NAME));
}
}
public static void main(String args[]) {
String actionKey = "theAction";
JFrame ft = new JFrame("Key Tester");
JButton jb1 = new JButton("<html><center>B<br>Focused/Typed");
Container panet = f.getContentPane();
panet.add(jb1, BorderLayout.NORTH);
stroke = KeyStroke.getKeyStroke("ctrl C");
action = new MyActionListener("Action Didn't Happen");
inputMap = jb2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap1.put(stroke, actionKey);
actionMap1 = jb2.getActionMap();
actionMap1.put(actionKey, action);
ft.setSize(400, 200);
ft.show();
}
}
Bookmarks