Java read Windows Key State - java

I want to write a program that plays a sound everytime, a button on the Keyboard is pressed. (Even, if the program isn´t in focus)
I am using JNativeHook, but when i want to add a Key Listener, i get an error beacuse the method .getInstance isn´t contained in Global Screen. :(
Anyone got an idea?
My Code:
package test1;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class jNativeHookExample implements NativeKeyListener {
public void nativeKeyPressed(NativeKeyEvent e) {
System.out.println(NativeKeyEvent.getKeyText(e.getKeyCode()));
if (e.getKeyCode() == NativeKeyEvent.VC_F9) {
//play sound;
}
}
public void nativeKeyReleased(NativeKeyEvent e) {
System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public void nativeKeyTyped(NativeKeyEvent e) {
System.out.println("Key Typed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
}
public static void main(String[] args) {
try {
/* Register jNativeHook */
GlobalScreen.registerNativeHook();
} catch (NativeHookException ex) {
/* Its error */
System.err.println("There was a problem registering the native hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
GlobalScreen.getInstance().addNativeKeyListener(new jNativeHookExample());
}
}

This code is probably ok for the version 1.1 of jnativehook.
Starting from the version 2.0 the GlobalScreen class has no getInstance() method, and addNativeKeyListener() is now static, so it should be called directly on the GlobalScreen:
GlobalScreen.addNativeKeyListener(new jNativeHookExample());

Related

Telegram bot in Java does not start

Error: java: cannot access java.util.concurrent.CompletableFuture class file for java.util.concurrent.CompletableFuture not found
Connected the library using Maven. Here is the TelegramBotsApi code to start the bot:
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;
public class Main {
public static void main (String [] args) {
try {
TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class);
botsApi.registerBot(new telegaBot());
} catch (TelegramApiException e) {
e.printStackTrace();
}
}
}
And here is the code of the bot itself:
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.Update;
public class telegaBot extends TelegramLongPollingBot {
#Override
public String getBotUsername() {
return "bot_name";
}
#Override
public String getBotToken() {
return "token";
}
#Override
public void onUpdateReceived(Update update) {
}
}
Here are Java, Maven and IDEA versions
enter image description here
enter image description here
enter image description here

RandomAccessFile doesn't work with Minecraft Forge

I'm working at the moment on a Mod for Minecraft with a dedicated Gui system written in C++ and Qt5. I let my GUI and Minecraft communicate through a named pipe, but I have there a small problem. I can read and write with a simple Java and C++(Qt) program into the pipe. But when I create a new instance of my Pipeendpoint class in post init of Minecraft Forge it can't read anything from the Pipe. In a standalone system, it can read stuff.
Not working Forge Implementation:
package de.CoderDE.CodersAnimationEditor;
import java.io.FileNotFoundException;
import de.CoderDE.CodersAnimationEditor.Pipe.PipeEndpoint;
import net.minecraft.client.Minecraft;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
#SideOnly(Side.CLIENT)
public class ClientProxy extends CommonProxy {
static PipeEndpoint pendpoint;
#Override
public void preInit(FMLPreInitializationEvent e) {
super.preInit(e);
}
#Override
public void init(FMLInitializationEvent e) {
super.init(e);
}
#Override
public void postInit(FMLPostInitializationEvent e) {
super.postInit(e);
try {
pendpoint = new PipeEndpoint();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
Working standalone implementation:
import java.io.FileNotFoundException;
import de.CoderDE.CodersAnimationEditor.Pipe.PipeEndpoint;
public class Main {
static PipeEndpoint pipe;
public static void main(String[] args) {
try {
pipe = new PipeEndpoint();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
And the important PipeEndpoint class:
package de.CoderDE.CodersAnimationEditor.Pipe;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class PipeEndpoint {
private Thread reciever;
private RandomAccessFile pipe;
public PipeEndpoint() throws FileNotFoundException {
pipe = new RandomAccessFile("\\\\.\\pipe\\CodersAnimationEditor", "rw");
reciever = new Thread(new PipeEndpointReciever());
reciever.start();
}
private class PipeEndpointReciever implements Runnable {
#Override
public void run() {
try {
while (true) {
System.out.print((char)pipe.read());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
And with "can't read anything" I mean that it never returns from pipe.read().
Oh, and the Java application starts after the C++(Qt) LocalServer started listening and waits for a new connection.

error trying to attach to virtual machine

I'm trying to attach to a running JVM to debug it using the Virtual Machine class in java. I'm currently have java 8, with jdk1.8.0_11. I have tried to add a manifest to it, with but to no avail. I'm also importing the tools.jar file from my JDK\libs folder.
My code:
import java.io.IOException;
import java.util.List;
import com.sun.tools.attach.AgentInitializationException;
import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
public class loadVM {
public static void main(String[] args) {
String name = "replaceAfterNameFound";
List <VirtualMachineDescriptor> vms = VirtualMachine.list();
for (VirtualMachineDescriptor vmd: vms) {
System.out.println(vmd.displayName());
if (vmd.displayName().equals(name)) {
try {
VirtualMachine vm = VirtualMachine.attach(vmd.id());
String agent = "";
vm.loadAgent(agent);
} catch(AttachNotSupportedException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
} catch(AgentLoadException e) {
e.printStackTrace();
} catch(AgentInitializationException e) {
e.printStackTrace();
}
}
}
}
}
Here is a copy of the error I get when running it:
java.util.ServiceConfigurationError:com.sun.tools.attach.spi.AttachProvider:
Provider sun.tools.attach.WindowsAttachProvider could not be
instantiated
Thank you guys for all your help!

altgr doesn't work with jnativehook?

It's just for testing purposes.
I tried to change some parts of the code and read also the api-docs.
On begin I added "typedC += e.getKeyChar();" and it looked like to log the altgr-keys, but later not anymore.
How to change it to let work altgr-keys ?
Thanks
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class JkL implements NativeKeyListener {
private String typedC = "";
private String typedC1 = "";
private final JFileChooser fc = new JFileChooser();
private File direc;
private void openFchooser1() throws FileNotFoundException,
InterruptedException, IOException, Exception {
Thread.sleep(2000);
int returnVal = fc.showDialog(null, "Choose a Logfile");
if(returnVal == JFileChooser.APPROVE_OPTION) {
direc = fc.getSelectedFile();
}
/* Construct the example object and initialze native hook. */
GlobalScreen.addNativeKeyListener(new Kl());
try {
/* Register jNativeHook */
GlobalScreen.registerNativeHook();
} catch (NativeHookException ex) {
/* Its error */
System.err.println("There was a problem registering the native
hook.");
System.err.println(ex.getMessage());
System.exit(1);
}
// Clear previous logging configurations.
LogManager.getLogManager().reset();
// Get the logger for "org.jnativehook" and set the level to off.
Logger logger =
Logger.getLogger(GlobalScreen.class.getPackage().getName());
logger.setLevel(Level.OFF);
}
#Override
public void nativeKeyPressed(NativeKeyEvent nke) {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}
#Override
public void nativeKeyReleased(NativeKeyEvent nke) {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}
#Override
public void nativeKeyTyped(NativeKeyEvent nke) {
throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}
class Kl extends JkL {
/* Key Pressed */
#Override
public void nativeKeyPressed(NativeKeyEvent e) {
//typedC += NativeKeyEvent.getKeyText(e.getKeyCode());
typedC += e.getRawCode();
typedC += e.getKeyChar();
typedC += e.getKeyCode();
try {
writeToFile(typedC);
} catch (IOException ex) {
Logger.getLogger(JkL.class.getName()).log(Level.SEVERE, null,
ex);
}
/* Terminate program when one press ESCAPE */
if (e.getKeyCode() == NativeKeyEvent.VC_F12) {
try {
GlobalScreen.unregisterNativeHook();
} catch (NativeHookException ex) {
Logger.getLogger(JkL.class.getName()).log(Level.SEVERE,
null, ex);
}
}
}
#Override
public void nativeKeyReleased(NativeKeyEvent e) {
//System.out.println("Key Released: " +
//NativeKeyEvent.getKeyText(e.getKeyCode()));
}
/* I can't find any output from this call */
#Override
public void nativeKeyTyped(NativeKeyEvent e) {
//typedC1 += NativeKeyEvent.getKeyText(e.getKeyCode());
typedC1 += e.getRawCode();
typedC1 += e.getKeyChar();
typedC1 += e.getKeyCode();
try {
writeToFile(typedC1);
} catch (IOException ex) {
Logger.getLogger(JkL.class.getName()).log(Level.SEVERE, null,
ex);
}
}
}
private void writeToFile(String ln) throws IOException {
//System.out.println(direc);
FileWriter fw = new FileWriter(direc);
try (BufferedWriter bw = new BufferedWriter(fw)) {
bw.write(ln);
bw.newLine();
bw.flush();
}
}
public static void main(String[] args) throws
IOException,InterruptedException, Exception {
new JkL().openFchooser1();
}
}
What operating system? There are some known issues with 2.0 on Linux but they will be fixed in 2.1. If there are any other issues, you will probably need to report a bug. Please include your exact keyboard layout, model and language. Also provided detailed information about what key is pressed, the observed behavior and the expected behavior. I only know how to use a US keyboard, so I am not sure how all of the Alt-Gr language/region specific keys, masks and locks are suppose to work.

Java clipboard as stack in paste override

I have done my research, no useful results found.
Here is the deal, I'm writing a 'new' clipboard that works like a stack instead of a 'area'. And I'm brave or stupid enoght to do that in Java. So far in my tests to see if this is possible I have managed to create this stack behavior. The only problem I'm getting is that sometimes, mainly when I paste the top of the stack (pop operation), it doesn't pop or for some other reason it pastes twice.
Example:
If i copy this three words: Carlos, Lucas, Eastwood
The stack clipboard behaves like this at paste: Eastwood, Eastwood, Lucas, Carlos
I'm using JNativeHooks for reading the system keypresses and determining when the user is pasting.
I think what is happening is that the system is pasting before my code... Well, here is the code anyway (It is a test, that explains why it is badly commented):
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.Transferable;
import java.util.Stack;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.NativeInputEvent;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
public class Test3 implements NativeKeyListener {
Clipboard sysClip = Toolkit.getDefaultToolkit().getSystemClipboard();
Stack<Transferable> clipStack = new Stack<>();
public static void main(String[] args) {
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);
}
Test2 t2 = new Test2();
// Construct the example object and initialze native hook.
GlobalScreen.getInstance().addNativeKeyListener(t2);
}
#Override
public void nativeKeyPressed(NativeKeyEvent ev) {
// Copy
if (ev.getKeyCode() == NativeKeyEvent.VK_C
&& NativeInputEvent.getModifiersText(ev.getModifiers()).equals(
"Ctrl")) {
// Clip the pop
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
clipStack.push(sysClip.getContents(null));
System.out.println("Ctrl+C : Stack(" + clipStack.size() + ")");
}
// Paste
if (ev.getKeyCode() == NativeKeyEvent.VK_V
&& NativeInputEvent.getModifiersText(ev.getModifiers()).equals(
"Ctrl")) {
// Clip the pop
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (clipStack.size() > 1) {
sysClip.setContents(clipStack.pop(), null);
} else
sysClip.setContents(clipStack.peek(), null);
System.out.println("Ctrl+V : Stack(" + clipStack.size() + ")");
}
}
#Override
public void nativeKeyReleased(NativeKeyEvent e) {
}
#Override
public void nativeKeyTyped(NativeKeyEvent e) {
}
}
I would think the same as you suggested.
Have a copy and paste key combination something different from ctrl+c and ctrl+v and you can even bypass using the system clipboard. Simply push the selected text and pop the text directly to your text pointer in your application provided of course that you are using this stack behavior for only a particular application.
The system retains it's own copy of latest clip and it gets a duplicate from your sysClip.setContents(clipStack.pop(), null) on paste operation.
I don't have much idea on disabling the system behavior. You can do research on that. But you can always make sure that it's the only problem by changing the key combination.

Categories