kwhat / jnativehook

Global keyboard and mouse listeners for Java.
Other
1.73k stars 344 forks source link

Not able to get the selected Content using HotKey combination in Mac #96

Open MohnishSingla opened 8 years ago

MohnishSingla commented 8 years ago

Hello Sir,

I am using jnativehoook to support global hot key events for my application. We can select the content and then press the hot key combination, the selected content is getting searched in my application. I am using the following code to get the selected content using Robot api

Robot r = new Robot(); r.setAutoDelay(40); r.setAutoWaitForIdle(true); r.delay(200); r.keyPress(KeyEvent.VK_META); r.delay(100); r.keyPress(KeyEvent.VK_C); r.delay(200); r.keyRelease(KeyEvent.VK_META); r.keyRelease(KeyEvent.VK_C);

when i am using jnativehook of version 1.1.4 everything works fine for me but when I am using jnativeHook of verion 2.0.3, I am not able to get the selected content using the above code. The same code works fine for windows in both versions (only change in above code is KeyEvent. For windows it is KeyEvent.VK_CTRL and for MAC, i am using VK_META).

Can you please point out the what can be the probable cause?

Thanks, Mohnish

kwhat commented 8 years ago

Hi Mohnish,

What does the JNativeHook code look like for the above robot example?

MohnishSingla commented 8 years ago

Hi Sir,

Even I am not getting how robot API's are related to jnativehook as robot API's are independent of jnative hook,ideally the text should be selected from clipboard.

For the reference I am using the below jnative hook event handlers on pressing hotkeys: nativeKeyPressed() and nativeKeyReleased()

public void nativeKeyPressed(NativeKeyEvent e) { if(HotKeyDataManager.getHK_Activate())
{ int tocheck; int otherKeyCode = 0; if(IS_OS_WINDOWS) { tocheck=e.getKeyCode(); otherKeyCode=getOtherKeyCode(tocheck); } else {
if(e.getKeyCode()==16) tocheck=e.getRawCode(); else tocheck=e.getKeyCode(); //System.out.println(tocheck + " si tocheck"); }

        if(tocheck==HotKeyDataManager.getHK_Modifier1() || otherKeyCode ==    HotKeyDataManager.getHK_Modifier1())          
            {
                //Pressed=true;
        //System.out.println("modifier 1 pressed");
                Modifier1_flag=true;
            }
        else
            if(tocheck==HotKeyDataManager.getHK_Modifier2() || otherKeyCode == HotKeyDataManager.getHK_Modifier2())
            {
                //Pressed=true;
            //System.out.println("modifier 2 pressed");
                Modifier2_flag=true;
            }
        else
            if(tocheck==HotKeyDataManager.getHK_Modifier3() || otherKeyCode == HotKeyDataManager.getHK_Modifier3())
            {
                Modifier3_flag=true;
            }
        else
        if(tocheck==HotKeyDataManager.getHK_Key())
            {
                Key_flag=true;
            }
        else
            {
                OtherKeyPressed=true;
                Pressed=false;
            }

    }
}

public void nativeKeyReleased(NativeKeyEvent e) {
    if(Modifier1_flag && Modifier2_flag && Modifier3_flag && Key_flag && !OtherKeyPressed )
    {
        System.out.println("Hotkey pressed  " );
        //shell.forceFocus();
        if(HotKeySettingsObject.startHotkeysOnStartup)
        TriggerHotKeySearch();

    }
    Pressed=true;
    OtherKeyPressed=false;
    setModifierFlags();         //RESETS THE FLAGS
    // TODO Auto-generated method stub
}

And I am using the following method to get the selected text from clipboard:

public String getSelectedText() { SelectedText=" "; //$NON-NLS-1$ System.out.println("in getSelectedText method"); //$NON-NLS-1$ saveExistingTextInClipBoard(); //1 clearClipBoard(); //2 copySelectedTextToClipboard(); //3 getSelectedContentsFromClipBoard(); //4 restoreTextInClipBoard(); //5

    System.out.println("Selected tex is  "+ SelectedText); //$NON-NLS-1
    return SelectedText;

}

All the methods inside getSelectedText() method have been implemented by me.

Thanks, Mohnish