Created
October 11, 2013 05:00
-
-
Save msapariya/6929799 to your computer and use it in GitHub Desktop.
jackson fails to jnativehook class with following exception
=========================================
The class of event object is : org.jnativehook.keyboard.NativeKeyEvent
com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.jnativehook.GlobalScreen and no properties discovered to create BeanSerializer (to avoi…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jnativehook.GlobalScreen; | |
import org.jnativehook.NativeHookException; | |
import org.jnativehook.keyboard.NativeKeyEvent; | |
import org.jnativehook.keyboard.NativeKeyListener; | |
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; | |
import com.fasterxml.jackson.annotation.JsonIgnore; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.ObjectWriter; | |
import com.fasterxml.jackson.databind.introspect.VisibilityChecker; | |
abstract class JacksonMixIn { | |
JacksonMixIn( | |
@JsonProperty("rawCode") int rawCode | |
) | |
{ /* do nothing */ } | |
} | |
public class GlobalKeyListenerExample implements NativeKeyListener { | |
public void nativeKeyPressed(NativeKeyEvent e) { | |
System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode())); | |
if (e.getKeyCode() == NativeKeyEvent.VK_ESCAPE) { | |
GlobalScreen.unregisterNativeHook(); | |
} | |
/* | |
* try { Thread.sleep(5000); } catch(InterruptedException ex) { | |
* Thread.currentThread().interrupt(); } | |
*/ | |
} | |
public void nativeKeyReleased(NativeKeyEvent e) { | |
System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode())); | |
System.out.println("The class of event object is : " + e.getClass().getName()); | |
ObjectMapper mapper = new ObjectMapper(); | |
//mapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(Visibility.ANY)); | |
mapper.addMixInAnnotations(NativeKeyEvent.class, JacksonMixIn.class); | |
//ObjectWriter ow = new ObjectMapper().writer(); // .withDefaultPrettyPrinter(); | |
ObjectWriter ow = mapper.writer(); // .withDefaultPrettyPrinter(); | |
try { | |
String json = ow.writeValueAsString(e); | |
System.out.println("Here is jsonized event : " + json); | |
} catch (JsonProcessingException e1) { | |
e1.printStackTrace(); | |
} | |
} | |
public void nativeKeyTyped(NativeKeyEvent e) { | |
System.out.println("Key Typed: " + NativeKeyEvent.getKeyText(e.getKeyCode())); | |
} | |
public static void main(String[] args) { | |
try { | |
GlobalScreen.registerNativeHook(); | |
} catch (NativeHookException ex) { | |
System.err.println("There was a problem registering the native hook."); | |
System.err.println(ex.getMessage()); | |
System.exit(1); | |
} | |
// Construct the example object and initialze native hook. | |
GlobalScreen.getInstance().addNativeKeyListener(new GlobalKeyListenerExample()); | |
System.out.println("Do I ever come here?"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jnativehook | |
- https://jnativehook.googlecode.com/files/JNativeHook-1.1.4.zip | |
jackson jar files | |
- http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.2.3/jackson-annotations-2.2.3.jar | |
- http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.2.3/jackson-core-2.2.3.jar | |
- http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.2.3/jackson-databind-2.2.3.jar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment