Skip to content

Instantly share code, notes, and snippets.

@msapariya
Created October 11, 2013 05:00
Show Gist options
  • Save msapariya/6929799 to your computer and use it in GitHub Desktop.
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…
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?");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment