java - All ObjectEvents created share similar source information? -


any time use objectevent, every statement in it's "performed" method called. actionevent, if put separate action commands different objects, every action command called every object. similarly, using public library, jnativehook, utilizes global screen listening keyboard/mouse. there individual constants defined describe each key keyboard pressed, each "nativekeyevent" (the object event) performs every command despite conditional statements. in context:

@override

public void nativekeypressed(nativekeyevent nativekeyevent) {     nativekeyevent e = nativekeyevent;     color col;     piece.tetcolor t;      if(e.getkeycode() == (nativekeyevent.vc_space));     {         system.out.println("space pressed");      }     if(e.getkeycode() == nativekeyevent.vc_escape);     {         system.out.println("escape pressed");     } } 

this action performed of nativekeyevent. no matter key press, print out:
space pressed escape pressed

i had problem earlier in year actionevents , event commands wrote separate anonymous classes every case wanted handle. i'm confused , appreciate possible.

there simple typo in method. there should no semi-colon (;) after if statement. putting semi-colon there, println statement following no longer part of if statement.

instead use:

if(e.getkeycode() == nativekeyevent.vc_space)      system.out.println("space pressed"); else if(e.getkeycode() == nativekeyevent.vc_escape)     system.out.println("escape pressed"); 

or:

switch(e.getkeycode()) { case nativekeyevent.vc_space:     system.out.println("space pressed"); break; case nativekeyevent.vc_escape:     system.out.println("escape pressed"); break; } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -