javaandroidkeyeventmedia-keys

KeyEvent special Keys (like mute)


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.


Solution

  • 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.