gpt4 book ai didi

java - JNativeHook 将键写入文本区域

转载 作者:行者123 更新时间:2023-12-02 03:47:51 28 4
gpt4 key购买 nike

当关键事件被记录时,它会打印到控制台。但不向文本区域添加任何文本。

我这里有一个框架:

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
}

全局keylistener代码

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() 但没有附加任何内容。任何帮助都会很棒。

最佳答案

正如我所说,您正在 GlobalKeyListenerExample 类中创建 A 类 的新对象。因此您有 2 个框架,一个是可见的,一个是隐藏的。

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

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

那么如何解决这个问题。一种方法是在您的类 A 中实现 NativeKeyListener,而不创建 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");
}

}

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

关于java - JNativeHook 将键写入文本区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36157630/

28 4 0
文章推荐: java - 从 arraylist 中删除重复的字符串