8

I made a simple Media player in Java but I want to record global key presses like Ctrl + P to pause/resume the current music being played without the JFrame having focus but it seems that its not possible due to JVM security issues.

I came across JNativeHook but I want to implement my own method only for Windows. Please Suggest how to do it and where to begin ?

Ernestas Gruodis
  • 8,567
  • 14
  • 55
  • 117
Snehasish
  • 1,051
  • 5
  • 20
  • 37
  • To implement your own method in windows you will need to use JNI or JNA. It's do-able, but if you're not familiar with the Windows API and C coding, there will be a bit of a learning curve involved. – Hovercraft Full Of Eels Apr 23 '13 at 22:02
  • I am familiar with C coding but not with Windows API. Please suggest me where to start with or what to search for as I am highly eager to implement it for my project. – Snehasish Apr 23 '13 at 22:12
  • For the Windows API, Google the MSDN library as it all can be found there. As for JNI you will want to Google for tutorials. JNA is easier to use but requires that you download the JNA library. – Hovercraft Full Of Eels Apr 23 '13 at 22:23

2 Answers2

9

Try JNativeHook. Here is a sample that shows how to use it to capture global key presses:

try
{
    GlobalScreen.registerNativeHook();
    GlobalScreen.addNativeKeyListener(new NativeKeyListener()
    {

        @Override
        public void nativeKeyTyped(NativeKeyEvent nativeEvent)
        {
        }

        @Override
        public void nativeKeyReleased(NativeKeyEvent nativeEvent)
        {
            String keyText=NativeKeyEvent.getKeyText(nativeEvent.getKeyCode());
            System.out.println("User typed: "+keyText);
        }

        @Override
        public void nativeKeyPressed(NativeKeyEvent nativeEvent)
        {   
        }
     });
}
catch (NativeHookException e)
{
    e.printStackTrace();
}

If you are using maven, add this to your pom.xml:

<dependency>
    <groupId>com.1stleg</groupId>
    <artifactId>jnativehook</artifactId>
    <version>2.1.0</version>
</dependency>
Thunder
  • 2,994
  • 1
  • 24
  • 19
4

Jintellitype is a somewhat easy solution.

https://code.google.com/p/jintellitype/

The other easy solution would be to use a windows hook with JNA:

JNA Keyboard Hook in Windows

I have some experience with JNA and have really liked the api.

And a third solution would be to manage your own calls with JNI.

Portability-wise, as far as I know, windows dlls and api architecture as far as responding to user input, has been preserved in different OS versions. If memory serves, hooks for user input are in the user32 dll. Maybe you have to jump through some hoops for a x64 bit version, but I doubt it would be that hard.

Community
  • 1
  • 1
Nikola Yovchev
  • 9,498
  • 4
  • 46
  • 72
  • So I would have to separately code for 32 bit and 64 bit architecture ? – Snehasish Apr 24 '13 at 06:36
  • probably not, but there is a possibility. I would expect user32 dll to be present on both x86 and x64 architectures. Still, I would at least run it on a 64-bit OS to see whether it works. Hopefully it does. – Nikola Yovchev Apr 24 '13 at 07:57