重新获得焦点后,Java KeyListener不会侦听

时间:2011-03-01 05:46:38

标签: java linux eclipse keyboard

我正在使用 Eclipse使用OpenJDK开发java应用程序 在Ubuntu 10.10上

当使用带有Jframe的密钥列表时,Keylistner可以完美地工作直到帧具有焦点,但是如果开关聚焦到另一个应用程序然后将焦点返回到Jframe,则此时它不会开始听到按键。 我需要做的是当框架重新获得焦点时,keylistener应该再次开始工作。我的申请中只有一个框架。

我已经实现了KeyListner,将其添加到JFrame:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import code.NewJFrame;
public class Gamepad implements KeyListener

{

    boolean button_up;
    boolean button_down;
    boolean button_right;
    boolean button_left;

    boolean b1;
    boolean b2;
    boolean b3;
    boolean b4;

    boolean select;
    boolean start;
    boolean r1;
    boolean r2;

    boolean l1;
boolean l2;

boolean joyl_up;
boolean joyl_down;
boolean joyl_right;
boolean joyl_left;

boolean joyr_up;
boolean joyr_down;
boolean joyr_right;
boolean joyr_left;



    final static char BUTTON_UP ='Q' ;
    final static char BUTTON_DOWN='W';
    final static char BUTTON_RIGHT='E';
    final static char BUTTON_LEFT='R';

    final static char B1='T';
    final static char B2='Y';
    final static char B3='U';
    final static char B4='I';

    final static char SELECT='O';
    final static char START='P';

    final static char R1='A';
    final static char R2='S';

    final static char L1= 'D';
    final static char L2= 'F';

    final static char JOYL_UP='G';
    final static char JOYL_DOWN='H';
    final static char JOYL_RIGHT='J';
    final static char JOYL_LEFT='K';

    final static char JOYR_UP='L';
    final static char JOYR_DOWN='Z';
    final static char JOYR_RIGHT='X';
    final static char JOYR_LEFT='C';


public Gamepad()
    {
        button_up = false;
        button_down= false;
        button_right= false;
        button_left= false;

        b1= false;
        b2= false;
        b3= false;
        b4= false;

        select= false;
        start= false;
        r1= false;
        r2= false;

        l1= false;
        l2= false;
    }

    /**
     * @param args
     */
    public static void main(String[] args)
        {
         NewJFrame nj = new NewJFrame();
         Gamepad gp = new Gamepad();
         nj.addKeyListener(gp);
         nj.setVisible(true);

        }



    @Override
    public void keyPressed(KeyEvent e)
        {
            char c= Character.toUpperCase( e.getKeyChar() );

             System.out.println("PRESSED:\t"+c);
             if(c==BUTTON_UP) button_up=true;
             else if(c==BUTTON_DOWN)                    button_down=true;
             else if(c==BUTTON_RIGHT)                button_right=true;
             else if(c==BUTTON_LEFT)                     button_left=true;
             else if(c==B1)                                                      b1=true;
             else if(c==B2)                                                      b2=true;
             else if(c==B3)                                                     b3=true;
             else if(c==B4)                                                      b4=true;
             else if(c==SELECT)                                     select=true;
             else if(c==START)                                      {   start=!start; Global.playToggle();}
             else if(c==R1)                                                     r1=true;
             else if(c==R2)                                                     r2=true;
             else if(c==L1)                                                     l1=true;
             else if(c==L2)                                                      l2 =true;
             else if(c==JOYL_DOWN)                              joyl_down   =true;
             else if(c==JOYL_LEFT)                              joyl_left   =true;
             else if(c==JOYL_RIGHT)                         joyl_right  =true;
             else if(c==JOYL_UP)                                        joyl_up =true;
             else if(c==JOYR_DOWN)                              joyr_down   =true;
             else if(c==JOYR_LEFT)                              joyr_left   =true;
             else if(c==JOYR_RIGHT)                         joyr_right  =true;
             else if(c==JOYR_UP)                                        joyr_up =true;


        }



    @Override
    public void keyReleased(KeyEvent e)
        {
            char c= Character.toUpperCase( e.getKeyChar() );


         System.out.println("RELEASED:\t"+e.getKeyChar());

         if(c==BUTTON_UP) button_up=true;
         else if(c==BUTTON_DOWN)                    button_down=false;
         else if(c==BUTTON_RIGHT)                button_right=false;
         else if(c==BUTTON_LEFT)                     button_left=false;
         else if(c==B1)                                                      b1=false;
         else if(c==B2)                                                      b2=false;
         else if(c==B3)                                                     b3=false;
         else if(c==B4)                                                      b4=false;
         else if(c==SELECT)                                     select=false;
         else if(c==START)                                          ;//{start=false; Global.playToggle();}
         else if(c==R1)                                                     r1=false;
         else if(c==R2)                                                     r2=false;
         else if(c==L1)                                                     l1=false;
         else if(c==L2)                                                      l2 =false;
         else if(c==JOYL_DOWN)                              joyl_down   =false;
         else if(c==JOYL_LEFT)                              joyl_left   =false;
         else if(c==JOYL_RIGHT)                         joyl_right  =false;
         else if(c==JOYL_UP)                                        joyl_up =false;
         else if(c==JOYR_DOWN)                              joyr_down   =false;
         else if(c==JOYR_LEFT)                              joyr_left   =false;
         else if(c==JOYR_RIGHT)                         joyr_right  =false;
         else if(c==JOYR_UP)                                        joyr_up =false;

        }



    @Override
    public void keyTyped(KeyEvent e)
        {
            // TODO Auto-generated method stub

        }

}

1 个答案:

答案 0 :(得分:1)

我自己在Linux(gnome)中遇到了keylistener / window焦点问题。

我做了一些追踪并发现: 1)当您单击窗口时,不会触发LOST FOCUS(不会触发焦点事件)。 2)重新获得焦点/点击窗口,使其触发LOST FOCUS事件,而不是增益焦点。

现在似乎对我有用的修复:

        frame.addFocusListener(new FocusListener(){
            public void focusGained(FocusEvent e){
                System.out.println("Focus GAINED:"+e);
            }
            public void focusLost(FocusEvent e){
                System.out.println("Focus LOST:"+e);

                // FIX FOR GNOME/XWIN FOCUS BUG
                e.getComponent().requestFocus();
            }
        });

基本上在焦点丢失时请求焦点。对于Windows用户来说似乎没有破坏任何东西,当你失去焦点时,图标可能会在你的开始栏上闪烁,更糟糕的是。

另外,如果你想像我一样进行调试,那么这就是代码:

        Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener(){
            public void eventDispatched(AWTEvent e) {
                    System.out.println("AWT:"+e);
                    System.out.flush();
            }
        }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);

希望这有助于某人!