Здравствуйте, Lucker, Вы писали:
L>Здравствуйте, <Аноним>, Вы писали:
А>>вот написал тестовый пример для работы с клавиатурой
А>>моя проблема заключается в том что когда фокус устанавливается в TextArea кнопки UP и DOWN (стрелочки) перестают работать
А>>а мне нужно чтобы кнопки работали во всех случаях где бы фокус не стоял у приложения !
А>>как сделать подскажите !?
А>>
А>>import javax.swing.*;
А>>import java.awt.*;
А>>import java.awt.event.ActionEvent;
А>>import java.awt.event.KeyEvent;
А>>public class Test extends JFrame {
А>> JPanel jPanel1 = new JPanel();
А>> JButton jButton5 = new JButton("UP");
А>> JButton jButton6 = new JButton("Down");
А>> JTextArea t1 = new JTextArea(5, 25);
А>> public Test() {
А>> t1.setFocusable(true);
А>> t1.setEditable(false);
А>> JComponent c = (JComponent) getContentPane();
А>> InputMap inputMap = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
А>> keyRegistry(c, inputMap, KeyEvent.VK_UP, jButton5);
А>> keyRegistry(c, inputMap, KeyEvent.VK_DOWN, jButton6);
А>> jPanel1.add(t1);
А>> jPanel1.add(jButton5);
А>> jPanel1.add(jButton6);
А>> c.add(jPanel1, BorderLayout.CENTER);
А>> c.setFocusable(true);
А>> }
А>> public void keyRegistry(JComponent c1, InputMap inputMap1, int ch, JButton jb) {
А>> inputMap1.put(KeyStroke.getKeyStroke(ch, 0), "alph" + ch);
А>> c1.getActionMap().put("alph" + (int) ch, new MyAbstractAction(jb));
А>> }
А>> private class MyAbstractAction extends AbstractAction {
А>> private JButton jb;
А>> public MyAbstractAction(JButton jb) {
А>> this.jb = jb;
А>> }
А>> public void actionPerformed(ActionEvent e) {
А>> jb.doClick();
А>> }
А>> }
А>> public static void main(String args[]) {
А>> Test frame = new Test();
А>> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
А>> frame.setBounds(200, 200, 300, 200);
А>> frame.show();
А>> }
А>>}
А>>
L>лови
L>L>import javax.swing.*;
L>import javax.swing.text.EditorKit;
L>import javax.swing.text.DefaultEditorKit;
L>import java.awt.*;
L>import java.awt.event.ActionEvent;
L>import java.awt.event.KeyEvent;
L>...
L> Action upButton = keyRegistry(c, inputMap, KeyEvent.VK_UP, jButton5);
L> Action downButton = keyRegistry(c, inputMap, KeyEvent.VK_DOWN, jButton6);
L> EditorKit editorKit = t1.getUI().getEditorKit(t1);
L> if (editorKit instanceof DefaultEditorKit) {
L> Action upAtcion = t1.getActionMap().get(DefaultEditorKit.upAction);
L> Action downAtion = t1.getActionMap().get(DefaultEditorKit.downAction);
L> t1.getActionMap().put(DefaultEditorKit.upAction, new CompoudAction(upAtcion, upButton));
L> t1.getActionMap().put(DefaultEditorKit.downAction, new CompoudAction(downAtion, downButton));
L> }
L>...
L> public Action keyRegistry(JComponent c1, InputMap inputMap1, int ch, JButton jb) {
L> final Action action = new MyAbstractAction(jb);
L> inputMap1.put(KeyStroke.getKeyStroke(ch, 0), "alph" + ch);
L> c1.getActionMap().put("alph" + (int) ch, action);
L> return action;
L> }
L>...
L> private class CompoudAction extends AbstractAction {
L> private final Action first, second;
L> public CompoudAction(Action firs, Action second) {
L> this.first = firs;
L> this.second = second;
L> }
L> public void actionPerformed(ActionEvent e) {
L> first.actionPerformed(e);
L> second.actionPerformed(e);
L> }
L> }
L>}
L>
К сожалению не работает

(