JFrame Close to Background and Listen to Keys

前端 未结 2 1836
耶瑟儿~
耶瑟儿~ 2021-01-17 05:33

Working on a new personal project with jframe. My goal is to close the frame in an ActionListener to the background, and when specific keys are pressed (Ctrl+

2条回答
  •  无人共我
    2021-01-17 05:52

    Well, the problem is that java is designed to be platform independent. Too achieve that, there have to be some limitations for the programms written in this programming language. You want to capture keystrokes even if your window/programm doesn't have the focus set on it. In fact what you need to write is some kind of global keylistener. You can't do such things in java. In fact you have to choose a much more machine-oriented programming language like c/c++ to achieve what you want.

    In java such stuff is only possible using the Java Native Interface (short JNI). Using JNI it is possible to write a library for hooking the keyevents in for example c/c++ and call the librarys' methods using a java programm.

    JNativeHook ( https://github.com/kwhat/jnativehook ) is using this exact approach. But well, i haven't tried this framework so i can't tell if it's working.

    But i once used this and it worked fine for me: http://softk.de/opensource/jglobalkeylistener.html

    You can just download the source and don't panic even if the site is written in german, the source-code is documented in english and even the comments within the code are in english.

    PS: if that doesn't work, it may help you to google for things like "java global keylogger", because thats exactly what a keylogger is doing (well it obviously also logs the keys) and i think there will be much more stuff that may help you.

    Greetings, Loki

提交回复
热议问题