Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: How to "enforce" pushing i.e. ctrl + c?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Location
    Poland
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to "enforce" pushing i.e. ctrl + c?

    Hello,

    I need to "enforce" autonomic pushing of a key combination in a program, like ctrl + c. I tried with VK_PASTE but on the list of errors I found that there was a problem with invalid key value or something and security issue. Somehow I understand the last one. Java won't let me do anything outside my program. But...

    Now more detalis:

    I mark with mouse some text, for example in pdf, independently running my program where I click let's say copy button, so I don't have to manually pushing combination on a keyboard. I have got what I wanted - that is a "thing" copied from other window in a clipboard BUT I didn't have to use my keyboard.

    Robot class also fails cause it probably allows pushing one VK_key at once. I seen it lets use VK_PASTE, but as mentioned before - throws invalid key code + ...java.security.AccessController.doPrivileged(Nat ive Method)...

    I'm little confused about it. Please don't write me that I should write my own OS with native problem solved ; ) Is there really any chance to make pushing virtually two buttons?

    THE BEST example of my dissertation ; ) is Screen Keyboard available in WindowsOS. In the background like now writing this thread I mark some text, using my mouse I click on Ctrl, then C and voil'a. It works. So there must be a solution. But please, give me the right pathway to do so. Which library I should use or what technic?

    I appreciate anyone who read it (or write me some clues).


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: How to "enforce" pushing i.e. ctrl + c?

    Are you doing this via an applet or webstart? Via a runnable jar? Directly from an IDE? Something else?

    What exact piece of code is causing the error? I recommend putting together an SSCCE that demonstrates exactly what you're doing.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Grossi.BP (November 27th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Location
    Poland
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to "enforce" pushing i.e. ctrl + c?

    Right now I'm running it directly from an IDE (NetBeans).

     JButton copy = new JButton("Copy");
            copy.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
     
                    try {
                        Robot cpd = new Robot();
                        cpd.keyPress(KeyEvent.VK_COPY); // <<<<<<<<<< I think here lies the issue.
     
                    } catch (AWTException ex) {
                        Logger.getLogger(Ramka.class.getName()).log(Level.SEVERE, null, ex);
                    }
     
                }
            });

    I've prepared an "SSCCE package" if this part of code isn't SSCE enough.
    package sscceforum;
     
    import java.awt.AWTException;
    import java.awt.EventQueue;
    import java.awt.GridLayout;
    import java.awt.Robot;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
     
    public class SscceForum {
     
        public static void main(String[] args) 
        {
          EventQueue.invokeLater(new Runnable()
             {
                 public void run()
                 {
                     JFrame ramka = new sceF();
                     ramka.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                     ramka.setVisible(true);
     
                 }
             });       
        }
     
    }
     
    class sceF extends JFrame
    {
        public sceF()
        {
            setUndecorated(true);
            setBounds(100,100,100,100);
     
            JButton copy = new JButton("Copy");
            copy.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
     
                    try {
                        Robot cpd = new Robot();
                        cpd.keyPress(KeyEvent.VK_COPY);
     
                    } catch (AWTException ex) {
                        Logger.getLogger(sceF.class.getName()).log(Level.SEVERE, null, ex);
                    }
     
                }
            });
     
     
            JButton exit = new JButton("Close");
            exit.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    System.exit(0);
                }
            });
     
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(2,1));
     
            panel.add(copy);
            panel.add(exit);
     
            add(panel);
        }
    }
    And thanks to enlight me with sscce idea. It's a very gentle way of getting me by the shoulders, shake furiously, hit me on the ground and shout: show me the code! ; )

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: How to "enforce" pushing i.e. ctrl + c?

    Okay, I see. VK_COPY isn't a valid key code because, well, it isn't a key. You can do something like this:

    Robot cpd = new Robot();
                        cpd.keyPress(KeyEvent.VK_CONTROL);
                        cpd.keyPress(KeyEvent.VK_C);

    ...but I predict that that won't do exactly what you expect, because you have to click on the button to make it happen, which takes the focus away from whatever window contains the selection, so nothing happens.

    Alternatively, you could look into using the Clipboard class, which was designed for stuff like this.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #5
    Junior Member
    Join Date
    Nov 2012
    Location
    Poland
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to "enforce" pushing i.e. ctrl + c?

    Exactly. I've already tried your suggestion. It doesn't work. But I noticed that Screen Keyboard all the time is out of focus if I can say so. It's only focused if I move it on the monitor and immediately turns "grey". I will try to do it on my own frame. About Clipboard class, I left it aside as I was thinking to find other solution, apparently wrong decision ; ) Ok, I try to figure it out and get this thread solved.

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,612
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: How to "enforce" pushing i.e. ctrl + c?

    Well, it might get a little complicated. That on-screen keyboard is a native application, which means it has access to other windows and native resources. Java doesn't really do that- everything is up to the OS.

    There are hacks around this problem, such as having the Robot click inside a native application to give it focus before doing the copy command, or perhaps hiding/minimizing the frame, or even using something like a toolbar icon instead of a frame? I'm not sure all of them will work for you, but it at least gives you a few directions to explore.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Grossi.BP (November 27th, 2012)

  9. #7
    Junior Member
    Join Date
    Nov 2012
    Location
    Poland
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to "enforce" pushing i.e. ctrl + c?

    OK, right now I'm pretty sure I solved the puzzle. I'll make some more tests and later on leave the general résumé here.

    After edit:

    It looks like it works. Turned out the very begin thinking about Robot class was correct. I don't know how I missed a line in documentation with a combination of letters ; ) like: keyRelease(int keycode) O.o' I also don't know why I assumed a keyPress method like a "click the thing and release" ; ) oh, dear... I realized it when I had screen keyboard with permanently pressed the control button. What's more, in order to get the focus out I used setFocusableWindowState with "false" parameter. All in all I don't need access to system clipboard or any serious and mysterious technics. It works great no matter if I choose text, shortcut to copy or whatever.

    Problem solved.
    Last edited by Grossi.BP; November 27th, 2012 at 03:42 PM. Reason: I needed to perform some tests to check the solution.

  10. #8
    Junior Member
    Join Date
    Nov 2012
    Location
    Poland
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to "enforce" pushing i.e. ctrl + c?

    I wrote the post with the answer to this problem but it didn't appear here... Anyway THANK YOU Kevin for your participation and good advices. Cheers.

Similar Threads

  1. Replies: 2
    Last Post: July 15th, 2012, 10:19 AM
  2. "Program execution skips the "if" statement"!
    By antony1925 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 7th, 2012, 08:15 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 02:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM

Tags for this Thread