JNativeHook将密钥写入textarea

时间:2016-03-22 14:49:33

标签: java swing

当关键事件得到控制时,它会打印到控制台。但不向textarea添加任何文本。

我这里有一个框架:

public class A extends javax.swing.JFrame {

public A() {
    initComponents();
}


@SuppressWarnings("unchecked")                         
private void initComponents() {
 //code for components
}                     

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {  
    try {
        GlobalScreen.registerNativeHook();
    }
    catch (NativeHookException ex) {
        System.err.println("There was a problem registering the native hook.");
        System.err.println(ex.getMessage());

        System.exit(1);
    }

    GlobalScreen.addNativeKeyListener(new GlobalKeyListenerExample());
    jTextArea1.append("logging started:\n");
}                                        
    }

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new A().setVisible(true);
        }
    });
}

//calling this method when ever a keyevent is occured. but its not appending any text.
public void getTA(){
    jTextArea1.append("something was pressed");
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration                   
}

globalkeylistener的代码

public class GlobalKeyListenerExample implements NativeKeyListener {
A a;
public GlobalKeyListenerExample(){
  a = new A();
}

public void nativeKeyPressed(NativeKeyEvent e) {
    a.getTA();
    System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}

public void nativeKeyReleased(NativeKeyEvent e) {
    a.getTA();
    System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}

public void nativeKeyTyped(NativeKeyEvent e) {
    a.getTA();
    System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
}

}

在发生事件时调用getTA()但没有附加任何内容。任何helkp都会很棒。

2 个答案:

答案 0 :(得分:1)

作为A扩展框架它正在代码的这一部分创建一个新框架

Interval

。通过设置a.setVisible(true),它是可见的,每次事件发生时,文本都会附加到此框架中的textarea。

public GlobalKeyListenerExample(){
    a = new A();
}

感谢fast snail帮助我。

答案 1 :(得分:1)

正如我所说,你正在class A类中创建一个GlobalKeyListenerExample的新对象。所以你有两个第一帧是可见的,一个是隐藏的。

当你调用a.getTA();时,你正在调用从GlobalKeyListenerExample类开始创建的对象。所以你将文本设置为不可见框架的jtextfield

public  GlobalKeyListenerExample(){
  a = new A();//invisible frame
}

所以如何解决这个问题。 一种方法是在不创建NativeKeyListener类的情况下为您的类A实现GlobalKeyListener。因此,您实现了像GlobalKeyListener类中所做的所有抽象方法。

并使用GlobalScreen.addNativeKeyListener(this);添加列表器。

例如

public class A extends javax.swing.JFrame implements NativeKeyListener {

    public A() {
        initComponents();
    }

    //implement abstract methods  
    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {
        a.getTA();
        System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    public void nativeKeyReleased(NativeKeyEvent e) {
        a.getTA();
        System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    public void nativeKeyTyped(NativeKeyEvent e) {
        a.getTA();
        System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());

            System.exit(1);
        }

        GlobalScreen.addNativeKeyListener(this);// add listner
        jTextArea1.append("logging started:\n");
    }

}

我无法测试此代码示例。如果您有任何问题,请询问。