i scanned the internet and couldnot find solution so, i was trying to integrate keypad shortcuts but later realised that the keyevent does not work properly which i could not find the reason for it works for others. but not for me .
here is the code:
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class KeyHandler implements KeyListener {
NotePad note;
public KeyHandler(NotePad note){
this.note=note;
}
@Override
public void keyTyped(KeyEvent e) {
System.out.println("some key is pressed");
if (e.getKeyCode() == KeyEvent.VK_S) {
System.out.println("s is down");
note.file.save();
}
else if (e.isControlDown()&&e.getKeyCode() == KeyEvent.VK_O)
note.file.open();
else if (e.isControlDown()&&e.getKeyCode() == KeyEvent.VK_Z)
note.functionEdit.undo();
else if (e.isControlDown()&&e.getKeyCode() == KeyEvent.VK_Y)
note.functionEdit.redo();
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}
in the key typed method i first used it with control down it happens but not with other keys which are alpahbetical or numeric.
i was trying to implement the keypad shortcuts basically fire keypad input like ctrl+s and all and get them to perform certain function . but it does not work.
here is my implementation of keyListener.
public void createTextArea(){
textArea=new JTextArea();
textArea.addKeyListener(keyHandler);
textArea.getDocument().addUndoableEditListener(
new UndoableEditListener() {
@Override
public void undoableEditHappened(UndoableEditEvent e) {
um.addEdit(e.getEdit());
}
}
);
scrollPane=new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBorder(BorderFactory.createEmptyBorder());
frame.add(scrollPane);
// frame.add(textArea); }
Generally speaking, you're better off making use of the key bindings API. This helps you decouple the functionality as well as control the level of focus under which the events are triggered.