我正在创建一个用于检索当前鼠标坐标的简单工具,以帮助我编写将来的项目。我决定不下载一个,因为我想作为一个自学成才的程序员来扩展我的知识。
当JFrame不集中时,我将JNativeHook用作NetBeans中的库来帮助我使用KeyListeners。我已经研究和调试了几个小时,并且发现KeyReleased方法不适用于if和switch语句。正如我在方法中放入System.out.println();
一样,它会检测到KeyRelease。它打印出来了。我的代码在下面。
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语句用法的任何帮助!
最佳答案
此代码解决了您的问题。
我认为主要问题出在e.getKeyCode()
上,因为它在nativeKeyPressed
和nativeKeyReleased
上没有返回相同的内容,所以我将代码放在方法public void nativeKeyPressed(NativeKeyEvent e)
中
另外,我还添加了一些注释以直观地指示您正在按哪个键。
为了避免“魔术数字”(27、87、67谁会猜到这是什么意思?),我将其转换为字符串形式,我认为代码的可读性得到了提高。
我将break;
指令放在每个case块的末尾,如果不输入,代码将遍历所有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) {
}