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.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111

3 Answers3

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.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
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.

x4u
  • 13,877
  • 6
  • 48
  • 58
  • 1
    Nice idea, but under Linux it simply does nothing and Windows reports a `IllegalArgumentException` because it's an unknown key-code. – Lukas Knuth Jun 04 '11 at 17:42
  • under windows it says: IllegalArgumentException: Invalid key code – basin Oct 31 '13 at 11:29
-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.

Community
  • 1
  • 1
expelledboy
  • 2,033
  • 18
  • 18
  • Sorry its late. I wasnt thinking linux. I would use bash, ssh, and rhythmbox-client. – expelledboy Jun 10 '11 at 22:56
  • 3
    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. – Lukas Knuth Jun 10 '11 at 23:05
  • 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. – expelledboy Jun 11 '11 at 00:18