gpt4 book ai didi

java - JNativeHook (GlobalScreen) 不使用 switch 和 if 语句

转载 作者:行者123 更新时间:2023-11-30 07:53:41 27 4
gpt4 key购买 nike

我正在创建一个简单的工具来检索当前的鼠标坐标,以帮助我编写 future 的项目。我决定不下载一个,因为我想作为一名自学成才的程序员扩展我的知识。

我将 JNativeHook 用作 NetBeans 中的库,以在 JFrame 未获得焦点时帮助我使用 KeyListeners。我研究和调试了几个小时,发现 KeyReleased 方法不适用于 if 和 switch 语句。它检测到 KeyRelease,因为我将 System.out.println(); 放在方法中;它打印出来了。我的代码在下面。

package Main;

import java.awt.MouseInfo;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.logging.LogManager;
import javax.swing.*;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.dispatcher.SwingDispatchService;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

public class MainClass implements NativeKeyListener {

static JFrame frame;
static JLabel label;
static JPanel panel;
static boolean run = true; //pause variable

private static void jframe(){ //JFrame code
frame = new JFrame("Mouse Coordinates");
label = new JLabel();
panel = new JPanel();
frame.setUndecorated(true);
frame.setVisible(true);
frame.setResizable(false);
frame.setAlwaysOnTop(true);

panel.add(label);
frame.add(panel);
frame.pack();
}

private static void check(){ //updating label text
if(run){ //if not paused
new Thread(new Runnable() {
@Override
public void run() {
while(run){ //loop
label.setText(MouseInfo.getPointerInfo().getLocation().toString().replaceAll("java.awt.Point", ""));
frame.setSize(label.getWidth() + 5, label.getHeight() + 5); //Adapt frame size to fit label
}
}
}).start();
}
}

public void nativeKeyPressed(NativeKeyEvent e) {

}

public void nativeKeyReleased(NativeKeyEvent e) {
switch(e.getKeyCode()){
case 27: //close code (esc key)
System.exit(0);
case 80: //pause code (p key)
run = !run;
check();
case 67: //copy code (c key)
StringSelection ss = new StringSelection(MouseInfo.getPointerInfo().getLocation().toString().replaceAll("java.awt.Point", "").replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("=", "").replaceAll("x", "").replaceAll("y", "").replaceAll(",", ", ")); //get mouse coordinates and set them as a StringSelection
Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); //get the clipboard
cb.setContents(ss, ss); //set what is copied to the current mouse coordinates
break;
}
}

public void nativeKeyTyped(NativeKeyEvent e) {

}

public static void main(String[] args){

LogManager.getLogManager().reset(); //stop the annoying constant logging of GlobalScreen

try {
GlobalScreen.setEventDispatcher(new SwingDispatchService());
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 MainClass());

jframe(); //Run JFrame code
check(); //Run check code

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new MainClass();
}
});
}
}

感谢任何帮助重新使用 JNativeHook 的 switch 语句!

最佳答案

这段代码解决了你的问题。

  1. 我认为主要问题出在 e.getKeyCode() 上因为它不会在 nativeKeyPressed 上返回相同的值和 nativeKeyReleased所以我把你的代码放在方法中 public void nativeKeyPressed(NativeKeyEvent e)

  2. 我还添加了一些注释以直观地指示您按的是哪个键。

  3. 为了避免“魔数(Magic Number)”(27、87、67 谁能猜出是什么意思?)我将其转换为字符串形式,我认为代码的可读性有所提高。

  4. 我把 break;每个 case block 末尾的指令,如果您不放置,代码将遍历所有 case。

    public void nativeKeyPressed(NativeKeyEvent e) {
    String keyText = NativeKeyEvent.getKeyText(e.getKeyCode());
    switch(keyText){
    case "Escape": //close code (esc key)
    System.out.println("Pressed esc");
    System.exit(0);
    break;
    case "P": //pause code (p key)
    System.out.println("Pressed p");
    run = !run;
    check();
    break;
    case "C": //copy code (c key)
    System.out.println("Pressed c");
    StringSelection ss = new StringSelection(MouseInfo.getPointerInfo().getLocation().toString().replaceAll("java.awt.Point", "").replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("=", "").replaceAll("x", "").replaceAll("y", "").replaceAll(",", ", ")); //get mouse coordinates and set them as a StringSelection
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); //get the clipboard
    cb.setContents(ss, ss); //set what is copied to the current mouse coordinates
    break;
    }
    }

    public void nativeKeyReleased(NativeKeyEvent e) {

    }

关于java - JNativeHook (GlobalScreen) 不使用 switch 和 if 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44596100/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com