Java可以看到键盘的活动吗? [英] Can Java see activity of my keyboard?

查看:114
本文介绍了Java可以看到键盘的活动吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个可以监控键盘活动的程序。更详细地说,Java程序应该看到按下/释放哪个键以及何时按下。所有这些信息都应该存储在给定的文件中。

I would like to write a program which can monitor activity of my keyboard. In more details, the Java program should "see" which key is pressed/released and when. All this information should be stored in a given file.

首先,我想知道是否可以用Java来实现。我知道如果我输入Java程序生成的文本字段,就有可能。但是,如果我在浏览器的文本字段中键入(例如,在单词(或开放式办公室)文档中),Java是否可以监视键盘活动?

First of all I would like to know if it's possible to do it with Java. I know that it's possible if I type in a text field generated by Java program. But is it possible for Java to monitor the keyboard activity if I type, let say, in a text field of a browser or, for example, in word (or open office) document?

推荐答案

这些事件被定向到具有焦点的窗口,从桌面上的所有事件中你只能获得鼠标位置。

These events are directed to the window which has the focus, from all events on the desktop you can only get the mouse position.

import java.awt.MouseInfo;


    public class Mouse {
        public static void main(String[] args) {
            while ( true ) {
                System.out.println( MouseInfo.getPointerInfo().getLocation() );
            }
        }
    }

用于捕获sysem wide(什么)你需要单词)你需要包含一个原生的lib

For capturing sysem wide (what you need for word) you need to include a native lib

windows的代码示例:原生键盘鼠标钩

Code example for windows: Native keyboard mouse hook

这篇关于Java可以看到键盘的活动吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆