現在のマウス座標を取得するための簡単なツールを作成し、将来のプロジェクトのコード作成に役立てています。私は独学のプログラマーとして自分の知識を広げたいので、私はそれをダウンロードしないことに決めました。JNativeHook(GlobalScreen)switchとif文が動作しない
JFrameがフォーカスされていないときに、KeyListenersを手助けするためにNetBeansのライブラリとしてJNativeHookを使用しています。私は数時間調査してデバッグし、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文の使用に関するヘルプがありがたいです!
ありがとうございます!私のプログラムは今、あなたの助けにもう一度感謝します! –