KeyEvent 特殊键(如静音)

发布于 2024-11-14 01:51:13 字数 474 浏览 8 评论 0原文

我目前正在尝试为 Android 创建一个小型远程应用程序来控制我的 PC 上的 MediaPlayer(如 Rythmbox)。

大多数媒体播放器都能理解键盘上的特殊键(例如“播放/暂停”或“下一个/上一个”)。我的想法是 Android 应用程序向 PC 发送命令(如“暂停”)。在 PC 上运行一个普通的 Java 应用程序,它接收此命令并模拟按下此特殊按钮。

优点是您可以在所有平台上为每个支持此特殊键的播放器使用此应用程序(并且它们几乎在每个新的 USB 键盘上)。

我在 JavaDocs 中搜索了 KeyEvent 类中的常量,但找不到任何内容。有谁知道如何模拟按下其中一个按钮,以及这是否可以用 Java 实现?

只要没有其他解决方案,额外的库对我来说也可以。

另外,我知道我应该使用 Robot 来模拟按键,这适用于键盘上的所有普通按键。我根本找不到任何方法来模拟这些特殊键上的按键。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

饮湿 2024-11-21 01:51:13

所以,我认为用纯Java 不可能做到这一点。我尝试了其他方法来找出特殊键具有哪些键码,但是这个小程序只为这些键返回 0 (它适用于“普通”键):

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) {}

}

我希望这将在JRE 的未来版本。但目前看来,这个问题还没有解决办法。

不管怎样,感谢所有的答案。

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.

倥絔 2024-11-21 01:51:13

您是否已尝试将操作系统相关的按键代码发送到机器人?遗憾的是,Java 尚未直接支持多媒体键,甚至在 Java 1.7 中也不支持,但 java.awt.event.KeyCode 中的大多数键码定义与其本机 Windows 挂件具有相同的值。机器人不会直接在 Java 中过滤未知的关键代码,而是让其本机后端决定如何处理它们。因此,它至少有可能在某些平台上运行。

MUTE 键代码为 0xAD。以下是Windows 密钥代码。

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.

半夏半凉 2024-11-21 01:51:13
VK_MEDIA_PLAY_PAUSE
VK_VOLUME_MUTE
VK_VOLUME_DOWN
VK_MEDIA_NEXT_TRACK
VK_MEDIA_PREV_TRACK

使用 Java 控制 Windows 应用

要暂时解决您的问题,只需 google“rhythmbox android”偏僻的。”已经有一些很棒的项目了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文