java - JNativeHook Keyboard listener isn't working with my Swing/GUI -


i'm making mouse/keyboard/pixels classes can simulate , read native input events. these implementated wrappers around java.awt.robot , jnativehook. they're intended used replacements autohotkey or autoit on mac. i'm doing testing on windows 7 64bit machine.

my problem can't figure out why keyboard listener jnativehook isn't working. haven't got around testing if mouse listeners work yet. it's possibly related multi-threading issues swing , jnativehook, haven't ruled out problems library or build version.

most of code directly example page. jnativehook spams logger , console debug information mouse/keyboard doesn't call listener. i'm using latest jar download page eclipse , latest jdk/jre unless went horribly wrong installs.

test class

the program displays button , colored panel. pressing button , typing "1" should change panel's color same pixel under mouse cursor.

import java.awt.awtexception; import java.awt.color; import java.awt.mouseinfo; import java.awt.point; import java.awt.robot; import java.awt.toolkit; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.windowevent; import java.awt.event.windowlistener; import java.util.logging.level; import java.util.logging.logmanager; import java.util.logging.logger;  import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.swingutilities; import javax.swing.windowconstants; import javax.swing.border.lineborder;  import org.jnativehook.globalscreen; import org.jnativehook.nativehookexception; import org.jnativehook.keyboard.nativekeyevent; import org.jnativehook.keyboard.nativekeylistener;  @suppresswarnings("serial") public class test extends jframe implements nativekeylistener, windowlistener {     jpanel panel1 = null;     jbutton button1 = null;     static final int pad = 20;      public test() {         // disable console/log spam jnativehook         /*         logmanager.getlogmanager().reset();         logger logger = logger.getlogger(globalscreen.class.getpackage()                 .getname());         logger.setlevel(level.warning);         */          settitle("test");         this.setlayout(null);         this.setsize(300, 270);          panel1 = new jpanel();         panel1.setbackground(color.red);         panel1.setsize(40, 40);         panel1.setlocation(20, 20);         panel1.setborder(new lineborder(color.black, 1));         this.getcontentpane().add(panel1);          button1 = new jbutton();         button1.settext("button1");         button1.setsize(100, 20);         button1.setlocation(20,                 panel1.getheight() + panel1.gety() + pad);         button1.addactionlistener(new actionlistener() {             public void actionperformed(actionevent e) {                 keys.addkeylistener(new nativekeylistener() {                     public void nativekeypressed(nativekeyevent e) {                         if(e.getkeycode() == nativekeyevent.vc_1) {                             keys.removekeylistener(this);                             point p = mouseinfo.getpointerinfo().getlocation();                             try {                                 panel1.setbackground(new robot().getpixelcolor(p.x, p.y));                             } catch (awtexception e1) {                                 e1.printstacktrace();                             }                             system.out.println("listener removed");                         }                         toolkit.getdefaulttoolkit().beep();                         system.out.println("pressed");                     }                      public void nativekeyreleased(nativekeyevent e) {                         system.out.println("released");                         toolkit.getdefaulttoolkit().beep();                     }                      public void nativekeytyped(nativekeyevent e) {                         system.out.println("typed");                         toolkit.getdefaulttoolkit().beep();                     }                  });                 system.out.println("button1");                 toolkit.getdefaulttoolkit().beep();             }         });         this.getcontentpane().add(button1);          this.setdefaultcloseoperation(windowconstants.dispose_on_close);         this.addwindowlistener(this);         this.setvisible(true); // display window.     }      public void windowopened(windowevent e) {         // initialize native hook         try {             globalscreen.registernativehook();         } catch (nativehookexception ex) {             system.err                     .println("there problem registering native hook.");             system.err.println(ex.getmessage());             ex.printstacktrace();             system.exit(1);         }          globalscreen.getinstance().addnativekeylistener(this);     }      public void windowclosed(windowevent e) {         // clean native hook         globalscreen.unregisternativehook();         system.runfinalization();         system.exit(0);     }      public void windowclosing(windowevent e) {         /* unimplemented */     }      public void windowiconified(windowevent e) {         /* unimplemented */     }      public void windowdeiconified(windowevent e) {         /* unimplemented */     }      public void windowactivated(windowevent e) {         /* unimplemented */     }      public void windowdeactivated(windowevent e) {         /* unimplemented */     }      public void nativekeyreleased(nativekeyevent e) {         if (e.getkeycode() == nativekeyevent.vc_space) {             swingutilities.invokelater(new runnable() {                 public void run() {                     joptionpane.showmessagedialog(null,                             "this run on swing's event dispatch thread.");                 }             });         }     }      public void nativekeypressed(nativekeyevent e) {         toolkit.getdefaulttoolkit().beep();         /* unimplemented */     }      public void nativekeytyped(nativekeyevent e) {         toolkit.getdefaulttoolkit().beep();         /* unimplemented */     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             public void run() {                 new test();             }         });     } } 

keys class

import java.awt.awtexception; import java.awt.awtkeystroke; import java.awt.robot; import java.awt.event.keyevent;  import org.jnativehook.globalscreen; import org.jnativehook.keyboard.nativekeylistener;  public class keys {     protected static robot r = null;      static {         try {             r = new robot();         } catch (awtexception e) {             e.printstacktrace();         }     }      private keys() {     } // static class      public static void setrobot(robot r) {         keys.r = r;     }      public static robot getrobot(robot r) {         return r;     }      /**      * sets delay between each key press/release.      *       * @param ms      *            number of milliseconds delay. 0 removes delay.      */     public static void setdelay(int ms) {         r.setautodelay(ms);     }      /**      * @param keycode      *            key send, example keyevent.vk_0 0 key.      */     public static void send(int keycode) {         r.keypress(keycode);         r.keyrelease(keycode);     }      /**      * @param keycode      *            key send, example keyevent.vk_0 0 key.      */     public static void press(int keycode) {         r.keypress(keycode);     }      /**      * @param keycode      *            key send, example keyevent.vk_0 0 key.      */     public static void release(int keycode) {         r.keyrelease(keycode);     }      public static void send(char c) {         string str = "" + c;         send(str);     }      /**      * doesn't support alt, ctrl, numlock, or capslock. might not support every      * key.      *       * @param str      *            string simulate typing.      */     public static void send(string str) {         boolean shifton = false;         (int = 0; < str.length(); i++) {             char c = str.charat(i);             int keycode = awtkeystroke.getawtkeystroke(c).getkeycode();             boolean islower = character.islowercase(c);             if (shifton) {                 if (character.islowercase(c)) {                     r.keyrelease(keyevent.vk_shift);                     shifton = false;                 }             } else if (!islower) {                 r.keypress(keyevent.vk_shift);                 shifton = true;             }             r.keypress(keycode);             r.keyrelease(keycode);         }         if (shifton) {             r.keyrelease(keyevent.vk_shift);         }     }      public static void addkeylistener(nativekeylistener listener) {         globalscreen.getinstance().addnativekeylistener(listener);     }      public static void removekeylistener(nativekeylistener listener) {         globalscreen.getinstance().removenativekeylistener(listener);     } } 

i switched jar build rc3 beta2 , works windows. beta2 didn't work on mac think rc 2 or 3 did.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -