Hotkey Listener overrides hotkey use (ctrl-c)

Asked by Max

I'm trying to write a java program that can listen when a user copies a text like this:

String key = "c";
        int modifiers = KeyModifier.CTRL;

        HotkeyListener listener = new HotkeyListener() {
            @Override
            public void hotkeyPressed(HotkeyEvent hotkeyEvent) {
                copyListener.executeCopy();
            }
        };

HotkeyManager.getInstance().addHotkey(key, modifiers, listener);

My problem is that this code overrides the global hotkey so that nothing is actually copied to clipboard when I press ctrl + c (when I get result from clipboard I only get what I copied before running this program, it works fine when I'm not using this hotkeylistener though). Is it possible to let the user copy text with ctrl + c and just listen to the event without intercepting it, or somehow place the selected text in clipboard?

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Max
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

It is always very interesting, to see, how the usage of Sikuli sometimes narrows the imagination of possible solutions.

Since you already are on the Java level, I just googled: java clipboard
one of the suggestions was
java clipboard listener

... and this is the topmost result (of course on stack overflow ;-)
http://stackoverflow.com/questions/14226064/calling-a-method-when-content-of-clipboard-is-changed

Revision history for this message
Max (max-klockar) said :
#2

Lol that's stupid of me, but I also realized that I will need a way to copy code anyways. Because I want two different ways of copying with two different key combinations, can I throw a ctrl + c event in Sikuli to copy a text (or do it another way)? (I'm extremely new to Sikuli)

Revision history for this message
RaiMan (raimund-hocke) said :
#3

for copying some text programmatically we have paste() (internally this puts something on the clipboard and then issues a ctrl-c)

Revision history for this message
Max (max-klockar) said :
#4

Which class is that in?

Revision history for this message
RaiMan (raimund-hocke) said :
#5

it is a region function, like click, find and all these features.

Revision history for this message
Max (max-klockar) said :
#6

I ended up using java.awt.Robot, works as I'd like it to now. Thanks for the answers though!

final HotkeyListener listener = new HotkeyListener() {
            @Override
            public void hotkeyPressed(HotkeyEvent hotkeyEvent) {
                try {
                    HotkeyManager.getInstance().removeHotkey(key, modifiers);
                    Robot robot = new Robot();
                    robot.keyPress(KeyEvent.CTRL_DOWN_MASK);
                    robot.delay(100);
                    robot.keyPress(KeyEvent.VK_C);
                    robot.delay(50);
                    robot.keyRelease(KeyEvent.VK_C);
                    robot.delay(100);
                    robot.keyRelease(KeyEvent.CTRL_DOWN_MASK);
                    robot.delay(1000);
                    copyListener.executeCopy();
                    HotkeyManager.getInstance().addHotkey(key, modifiers, this);
                } catch (AWTException e) {
                    e.printStackTrace();
                }
            }
        };

A bit tricky playing around with the delays, but it doesn't need to be fast so it's fine

Revision history for this message
RaiMan (raimund-hocke) said :
#7

have you tried

type("c", Key.CTRL)

before experimenting with the Robot class yourself?

If you need to handle individual keys, we have

key.Down(Key.CTRL)
wait(100)
type("c")
keyUp()

both solutions would replace the sequence
Robot robot = new Robot();
robot.keyPress(KeyEvent.CTRL_DOWN_MASK);
robot.delay(100);
robot.keyPress(KeyEvent.VK_C);
robot.delay(50);
robot.keyRelease(KeyEvent.VK_C);
robot.delay(100);
robot.keyRelease(KeyEvent.CTRL_DOWN_MASK);

Revision history for this message
RaiMan (raimund-hocke) said :
#8

BTW: what system are you working on?

Revision history for this message
Max (max-klockar) said :
#9

I tried the first one first, didn't work so well, I will check again with both solutions when I get back from work or maybe at lunch. It could possibly be a little problematic because I'm in my example actually physically pressing ctrl + c the same time, I think that's maybe why the delays and timing seem so critical. I'm on Ubuntu 14.04.

Revision history for this message
RaiMan (raimund-hocke) said :
#10

ok understood.

then this should help:

wait(0.3f); // or an even higher value
type("c", Key.CTRL)

or
wait(0.3f); // or an even higher value
key.Down(Key.CTRL)
wait(0.1f)
type("c")
keyUp()

I doubt that the "inner" extra waits are needed.

sorry for the wrong wait values: a float as seconds is needed.

Revision history for this message
Max (max-klockar) said :
#11

Wow.. I don't know why I thought it didn't work, it works perfectly, better than my robot solution (using your first suggestion). Maybe I used the wait function wrong the first time or something. Thanks a lot!