欢迎来到天天文库
浏览记录
ID:37832755
大小:129.03 KB
页数:12页
时间:2019-06-01
《在Windows下实现Java程序的屏幕鼠标键盘监控》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、文件:/udata/archives/Java/jni/在Windows下实现Java程序的屏幕鼠标监控第1页,共12页在Windows下实现Java程序的屏幕鼠标监控原文地址:http://hi.baidu.com/ablazejobs/blog同事做了个Java的托盘程序,简单是简单,但客户突然提出要求,此托盘应监控鼠标和键盘,一旦到达一定的空闲时间,就应让托盘启动某种注销动作,这在Java中似乎不太好办。一、Java的鼠标和键盘侦听的限制Java提供了两个事件侦听接口用来管理鼠标移动和键盘动作,分别是MouseMotionListener和KeyListener,可以用
2、它们来捕捉鼠标和键盘,例如下面的例子:1)类KeyMonitor,是一个JPanel的扩展类,用来显示捕捉到的按键信息:packagemkdemo;importjava.awt.Graphics;importjava.awt.Rectangle;importjavax.swing.JPanel;publicclassKeyMonitorextendsJPanel{protectedint_count=0;protectedString_curKey;publicvoidtellKeyInfo(intcount,Stringkey){_count=count;_curKey=k
3、ey;repaint();}@OverrideprotectedvoidpaintComponent(Graphicsg){super.paintComponent(g);StringBuffersbuf=newStringBuffer();sbuf.append("Keyboardhasbeenstricked");sbuf.append(_count);sbuf.append("times,thekeyis<");sbuf.append(_curKey);sbuf.append(">");Rectanglerect=this.getBounds();g.drawStrin
4、g(sbuf.toString(),rect.x+20,(rect.height)/2);}}2)类MouseMonitor,也是一个JPanel的扩展类,用来显示到的鼠标移动信息:packagemkdemo;importjava.awt.Graphics;importjava.awt.Rectangle;importjavax.swing.JPanel;publicclassMouseMonitorextendsJPanel{protectedint_xPos;protectedint_yPos;publicvoidtellMouseInfo(intx,inty){_xPo
5、s=x;_yPos=y;repaint();}@OverrideprotectedvoidpaintComponent(Graphicsg){super.paintComponent(g);文件:/udata/archives/Java/jni/在Windows下实现Java程序的屏幕鼠标监控第2页,共12页StringBuffersbuf=newStringBuffer();sbuf.append("MouseLocationonscreen:x=");sbuf.append(String.valueOf(_xPos));sbuf.append(",y=");sbuf.ap
6、pend(String.valueOf(_yPos));Rectanglerect=this.getBounds();g.drawString(sbuf.toString(),rect.x+20,(rect.y+rect.height)/2);}}3)入口类Main,用匿名类的方式实现了真正的监听接口:packagemkdemo;importjava.awt.BorderLayout;importjava.awt.GridLayout;importjava.awt.Point;importjava.awt.event.KeyAdapter;importjava.awt.eve
7、nt.KeyEvent;importjava.awt.event.MouseEvent;importjava.awt.event.MouseMotionAdapter;importjavax.swing.JFrame;importjavax.swing.JPanel;publicclassMain{protectedstaticint_count=0;publicstaticvoidmain(String[]args){JFrameframe=newJFrame("鼠标捕获演示程序");JPanelpa
此文档下载收益归作者所有