我如何制作一个实现KeyListener的控制台应用程序?

时间:2021-04-04 20:49:52

I am trying to create a simple Java Program which implements KeyListener .

我正在尝试创建一个实现KeyListener的简单Java程序。

I want to run my program continuously until i abort it or upon a certain condition. What i wanted to do is after i run the program , whatever keys i pressed , it will get stored in the out.txt file.

我想继续运行我的程序,直到我中止它或在某种情况下。我想要做的是在我运行程序后,无论我按下什么键,它都会存储在out.txt文件中。

public class StoreInFile implements KeyListener{
    public static void main(String[] args) {
    while(true){
    // **What should i do here such that it will call the keypressed event**.
    }
    }
    @Override
    public void keyPressed(KeyEvent e) {
        try{
              FileWriter fstream = new FileWriter("d:\\out.txt",true);
              BufferedWriter out = new BufferedWriter(fstream);
              out.write(e.getKeyChar());
              out.close();
              }catch (Exception ex){
              System.err.println("Error: " + ex.getMessage());
              }
    }

}

Is it possible to achieve in console Application? Basically i want to create a KeyLogger or KeyCatcher. How i can achieve this? Thanks in advance.

是否可以在控制台应用程序中实现?基本上我想创建一个KeyLogger或KeyCatcher。我怎么能做到这一点?提前致谢。

3 个解决方案

#1


1  

You can try a open source framework,jLine: jLine

您可以尝试使用开源框架jLine:jLine

But this is not 100% java.Some native implementation is also there.

但这不是100%java.Some本地实现也在那里。

#2


2  

For line based input, i.e. "users types text, user presses enter, your program receives what the user typed", the Scanner class should do just fine.

对于基于行的输入,即“用户键入文本,用户按下输入,您的程序接收用户输入的内容”,Scanner类应该可以正常工作。

For single character input, such as "user presses x-key, your program receives a notification." you'll have to look into libraries such as CHARVA or Java Curses.

对于单字符输入,例如“用户按下x键,您的程序会收到通知”。你将不得不研究诸如CHARVA或Java Curses之类的库。

#3


0  

If you're running on Windows, I will recommend JNA, and write keyhook and/or mousehook to monitor key/mouse events. It still works when your console window is inactive.

如果你在Windows上运行,我会推荐JNA,并编写keyhook和/或mousehook来监控键/鼠标事件。当控制台窗口处于非活动状态时,它仍然有效

JNA Keyboard Hook in Windows

Windows中的JNA键盘挂钩

Working example of JNA mouse hook

JNA鼠标钩子的工作示例

#1


1  

You can try a open source framework,jLine: jLine

您可以尝试使用开源框架jLine:jLine

But this is not 100% java.Some native implementation is also there.

但这不是100%java.Some本地实现也在那里。

#2


2  

For line based input, i.e. "users types text, user presses enter, your program receives what the user typed", the Scanner class should do just fine.

对于基于行的输入,即“用户键入文本,用户按下输入,您的程序接收用户输入的内容”,Scanner类应该可以正常工作。

For single character input, such as "user presses x-key, your program receives a notification." you'll have to look into libraries such as CHARVA or Java Curses.

对于单字符输入,例如“用户按下x键,您的程序会收到通知”。你将不得不研究诸如CHARVA或Java Curses之类的库。

#3


0  

If you're running on Windows, I will recommend JNA, and write keyhook and/or mousehook to monitor key/mouse events. It still works when your console window is inactive.

如果你在Windows上运行,我会推荐JNA,并编写keyhook和/或mousehook来监控键/鼠标事件。当控制台窗口处于非活动状态时,它仍然有效

JNA Keyboard Hook in Windows

Windows中的JNA键盘挂钩

Working example of JNA mouse hook

JNA鼠标钩子的工作示例