KeyEvent special Keys (like mute)
Asked Answered
K

3

4

I'm currently trying to create a little remote-app for Android to control a MediaPlayer (like Rythmbox) on my PC.

Most media-players understand the special keys on my keyboard (like "play/pause" or "next/previous"). My idea is that the Android App sends a command (like "pause") to the PC. On the PC runs a normal Java-Application which receives this commands and simulates a key-press of this special button.

The advantage would be that you can use this App on all platforms for every player which supports this special keys (and they are on almost every new USB-Keyboard).

I searched the JavaDocs for a constant in the KeyEvent-class, but I can't find any. Does anyone know how to simulate a press of one of those buttons and if this is even possible with Java?

Additional library's are okay with me, too, as long as there is no other solution.

Also, I know i should use a Robot to simulate the key-press and this works for all normal keys on my keyboard. I simply can't find any way to simulate a key press on those special keys.

Kallman answered 4/6, 2011 at 10:53 Comment(2)
Have you use KEYCODE_MUTE. Reference : developer.android.com/reference/android/view/…Craigie
I want to emulate a press on the KeyBoard so the PC is muted, not the Android Device.Kallman
K
3

So, I think it's not possible to do this with pure Java. I tried something else to find out which key-code the special keys have, but this small program only returns 0 for those keys (it works for "normal" keys):

public class GetKeycode implements KeyListener{

    private JFrame f;
    private JLabel feld;

    public GetKeycode(){
        f = new JFrame("GetKeycode");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.addKeyListener(this);
        feld = new JLabel();
        f.add(feld);
        f.pack();
        f.setVisible(true);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        feld.setText(e.getKeyCode()+"");        
    }

    public static void main(String[] args) {
        new GetKeycode();
    }

    // Unused:
    @Override public void keyPressed(KeyEvent e) {}
    @Override public void keyTyped(KeyEvent arg0) {}

}

I hope this will be implemented in future versions of the JRE. But at the moment, there seems to be no solution for this.

Thanks for all the answers anyways.

Kallman answered 12/6, 2011 at 19:25 Comment(0)
C
1

Have you already tried to send the OS dependent key codes to the Robot? The multimedia keys are unfortunately not directly supported in Java yet, not even in Java 1.7 but most of the keycode definitions in java.awt.event.KeyCode have the same value as their native Windows pendants. The Robot doesn't filter unknown key codes directly in Java but lets its native back end decide what to do with them. So there is a chance that it might work at least on certain platforms.

The MUTE key code would be 0xAD. Here is a list of the Windows Key Codes.

Canonicate answered 4/6, 2011 at 11:30 Comment(2)
Nice idea, but under Linux it simply does nothing and Windows reports a IllegalArgumentException because it's an unknown key-code.Kallman
under windows it says: IllegalArgumentException: Invalid key codeCavin
F
-1
VK_MEDIA_PLAY_PAUSE
VK_VOLUME_MUTE
VK_VOLUME_DOWN
VK_MEDIA_NEXT_TRACK
VK_MEDIA_PREV_TRACK

Control a Windows apps using Java

To temporarily solve your problem just google "rhythmbox android remote." There are some great projects already.

Fiore answered 10/6, 2011 at 22:52 Comment(3)
Sorry its late. I wasnt thinking linux. I would use bash, ssh, and rhythmbox-client.Fiore
Those constants don't exist in the Java's KeyCode-class. Also, I don't want to control only Rythmbox, but any player who knows how to work with this keys no matter on which platform (Windows, Linux, OS X) he runs. I want to emulate a press on one of those keys, using some other application is not an option.Kallman
I recall trying to do this before. I eventually included in the installation of the app a procedure to train which codes to use because the multimedia keys changed from system to system. Maybe its changed since.Fiore

© 2022 - 2024 — McMap. All rights reserved.