How to emulate pressing media keys in Java?

前端 未结 4 716
南笙
南笙 2021-01-12 13:59

How can I emulate pressing media keys in Java? Such as play/pause, next/previous, volume control.

C# has VK_MEDIA_PLAY_PAUSE, VK_MEDIA_NEXT_TRACK

4条回答
  •  一个人的身影
    2021-01-12 14:35

    You can achieve it with https://github.com/kwhat/jnativehook and afterwards spy on the keys. In my question I've written some sample methods:

    public static void MediaKeyForward(){
        GlobalScreen.postNativeEvent(new NativeKeyEvent(2401,0,176,57369,org.jnativehook.keyboard.NativeKeyEvent.CHAR_UNDEFINED));
    
    }
    public static void MediaKeyBack(){
        GlobalScreen.postNativeEvent(new NativeKeyEvent(2401,0,177,57360,org.jnativehook.keyboard.NativeKeyEvent.CHAR_UNDEFINED));
    
    }
    public static void MediaKeyPause(){
     GlobalScreen.postNativeEvent(new NativeKeyEvent(2401,0,179,57378,org.jnativehook.keyboard.NativeKeyEvent.CHAR_UNDEFINED));
    
    }
    

    Everything to do is to include the library into your project. A sample of how to spy on keys and get the neccesary Parameters to create a key event can be found here.

提交回复
热议问题