日期:2014-05-20 浏览次数:20663 次
package jni; public class Example implements KeyboardEventListener { static GlobalEventListener gl; public static void main(String[] args) throws Exception { Example inst = new Example(); gl = new GlobalEventListener(); gl.addKeyboardEventListener(inst); } public void GlobalKeyPressed( KeyboardEvent event ) { System.out.println( "Key Pressed: " + event.getVirtualKeyCode() ); } public void GlobalKeyReleased( KeyboardEvent event ) { System.out.println( "Key Released: " + event.getVirtualKeyCode() ); } }
package jni; public class GlobalEventListener { PoolHook pt; public GlobalEventListener() { pt = new PoolHook(this); pt.start(); } protected javax.swing.event.EventListenerList listenerList = new javax.swing.event.EventListenerList(); public void addKeyboardEventListener(KeyboardEventListener listener) { listenerList.add( KeyboardEventListener.class, listener ); } public void removeKeyboardEventListener(KeyboardEventListener listener) { listenerList.remove( KeyboardEventListener.class, listener ); } void keyPressed(KeyboardEvent event) { Object[] listeners = listenerList.getListenerList(); for ( int i = 0; i < listeners.length; i += 2 ) { if ( listeners[ i ] == KeyboardEventListener.class ) { ( (KeyboardEventListener)listeners[i + 1] ).GlobalKeyPressed( event ); } } } void keyReleased(KeyboardEvent event) { Object[] listeners = listenerList.getListenerList(); for ( int i = 0; i < listeners.length; i += 2 ) { if ( listeners[ i ] == KeyboardEventListener.class ) { ( (KeyboardEventListener)listeners[i + 1] ).GlobalKeyReleased( event ); } } } }
package jni; import java.util.*; public interface KeyboardEventListener extends EventListener { public void GlobalKeyPressed( KeyboardEvent event ); public void GlobalKeyReleased( KeyboardEvent event ); } class KeyboardEvent extends EventObject { private static final long serialVersionUID = 2341653211621224652L; boolean ts, ap, ek; int vk; public KeyboardEvent( Object source, boolean ts, int vk, boolean ap, boolean ek ) { super(source); this.ts = ts; this.vk = vk; this.ap = ap; this.ek = ek; } public boolean getTransitionState() { return ts; } public long getVirtualKeyCode() { return vk; } public boolean isAltPressed() { return ap; } public boolean isExtendedKey() { return ek; } public boolean equals( KeyboardEvent event ) { if( event.getVirtualKeyCode() == vk ) { if( event.isExtendedKey() == ek ) { if( event.isAltPressed() == ap ) { return true; } } } return false; } }