Creating a keyboard hook for a Java program that s

2019-06-24 03:59发布

问题:

Basically I have a java program that runs in the system tray and would like to add make it so if the user pressed say CTRL+SHIFT+1 it selects one of the right click options of the java program that sits in the tray.

I guess what I'm looking for is a tutorial on doing this, or what the simplest approach would be. I have never done something like this before, so possibly some sort of noobie tutorial?

Thanks

回答1:

so , creating a keyboard hook isn't as easy as it looks , java doesn't provide a direct way to interact with the system events for purposes of security ; as it might lead to serious problems concerning the JVM , sysytem's memory and the portability of Java Framework..

you have 4 ways to make global keyboard hooks :

1- Learn JNI / JNA , and I prefer JNA since its much easier than JNI , in both cases you shall deal with .dll files.

2 - Use JIntellitype , which - as you said - issues some problems.

3- the elegant solution by Sergei Biletnikov here.

4- ignore Java , and try Autoit V3 ( I'm not 100% sure about it , but I think you could send signals/events from Autoit to your Java app , so Autoit would just work as a bridge that catches the global key strokes)