简体   繁体   中英

Java paste bug in some programs

I'm a Newbie in Java programming. I have wrote this program to paste "Hello world!" every second. The program run properly and the text pastes in many windows programs such as Notepad, Microsoft Word, Browsers and every program's textarea or inputbox. But in some programs my code didn't work for example Windows End Task Manager or Garena room.

Now my question is: Can some programs denies JAVA codes for security reasons? If it's true how to enable Java in them?

Be aware that I use NetBeans IDE for programing and compiling the jar file. My code is as below:

package com.javacodegeeks.snippets.desktop;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

public class SimulateMouseMoveAndKeyPress {

  public static void main(String[] args) throws InterruptedException {

    try {

        // Create counter 

        int counter = 0; 

        while(true) {
            Thread.sleep(1000);
            String myString = "[" + counter + "] Hello world!";
            StringSelection stringSelection = new StringSelection (myString);
            Clipboard clpbrd = Toolkit.getDefaultToolkit ().getSystemClipboard ();
            clpbrd.setContents (stringSelection, null);
            Robot r = new Robot();
            r.keyPress(KeyEvent.VK_CONTROL);
            r.keyPress(KeyEvent.VK_V);
            r.keyRelease(KeyEvent.VK_CONTROL);
            r.keyRelease(KeyEvent.VK_V);
            r.keyPress(KeyEvent.VK_ENTER);
            r.keyRelease(KeyEvent.VK_ENTER);
            counter = (counter)+1; 
        }

    } catch (AWTException e) {

        System.out.println("Low level input control is not allowed " + e.getMessage());
    }

  }

}

Please help me. Thanks all.

Can some programs denies JAVA codes for security reasons? The no-Java programs are not aware of any Java program. They simple react to the Ctrl-V hot key (coming thru the operating system) and paste what they find in the systems clipboard in their structure. Or not, that should be the case where you won't see your "Hello world" string.

Probably where you don't see your pasted data, they won't react to a manual Ctrl-V either.


The Robot docs says:

Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform's native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events.

Your generated events goes into the system event queue or not (should throw an Exception). Seems they work for some, others not. But AFAIK that is not due to use Java to create the events.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM