Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中模拟按媒体键?_Java_Hotkeys_Awtrobot_Media Keys - Fatal编程技术网

如何在Java中模拟按媒体键?

如何在Java中模拟按媒体键?,java,hotkeys,awtrobot,media-keys,Java,Hotkeys,Awtrobot,Media Keys,如何在Java中模拟按媒体键?例如播放/暂停、下一个/上一个、音量控制 C#有VK#U媒体(播放)暂停,VK#U媒体(下一首)曲目等等 Java有类Robot,用于处理密钥,但没有媒体密钥 创建您自己的keylistener和spy,然后使用此值。 下面是一个简单的KeySpy类: import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.JFrame; import javax.

如何在Java中模拟按媒体键?例如播放/暂停、下一个/上一个、音量控制

C#有
VK#U媒体(播放)暂停
VK#U媒体(下一首)曲目
等等


Java有类
Robot
,用于处理密钥,但没有媒体密钥

创建您自己的keylistener和spy,然后使用此值。 下面是一个简单的KeySpy类:

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JLabel;


public class KeySpy {
    JLabel label=new JLabel("Enter the key");
    public KeySpy() {
        JFrame frame=new JFrame("KeySpy");
        frame.add(label);

        frame.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                label.setText(e.toString());
                System.out.println(e.toString());
            }
        });

        frame.setSize(200, 200);
        frame.setVisible(true);
    }

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

    }

}
这是我键盘上两个按钮的结果

   [Stop] = java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_STANDARD,rawCode=178,primaryLevelUnicode=0,scancode=36,extendedKeyCode=0x0] on frame0

   [Mute] = java.awt.event.KeyEvent[KEY_PRESSED,keyCode=0,keyText=Unknown keyCode: 0x0,keyChar=Undefined keyChar,keyLocation=KEY_LOCATION_STANDARD,rawCode=173,primaryLevelUnicode=0,scancode=32,extendedKeyCode=0x0] on frame0
正如您所看到的,它们没有键码,但有rawCode,所以请使用它。

我改进了。JFrame保持焦点,因此您可以最小化或最大化应用程序,并且在应用程序处于焦点时仍可以按任意键

这是GUI

我将信息放在标签/值网格中,以便更容易找到您感兴趣的值

这是代码。这是GridBagLayout的一个很好的示例

package com.ggl.testing;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class KeySpy implements Runnable {

    private KeySpyPanel keySpyPanel;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new KeySpy());
    }

    @Override
    public void run() {
        final JFrame frame = new JFrame("Key Spy");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.addWindowFocusListener(new WindowAdapter() {
            public void windowGainedFocus(WindowEvent e) {
                frame.requestFocusInWindow();
            }
        });

        keySpyPanel = new KeySpyPanel();
        frame.add(keySpyPanel.getPanel());
        frame.addKeyListener(new KeyPressedListener(this));

        frame.pack();
        frame.setVisible(true);
    }

    public KeySpyPanel getKeySpyPanel() {
        return keySpyPanel;
    }

    public class KeySpyPanel {

        private final Insets bottomInsets = new Insets(10, 10, 10, 10);
        private final Insets normalInsets = new Insets(10, 10, 0, 10);

        private JPanel panel;

        private JTextField keyCodeField;
        private JTextField keyTextField;
        private JTextField keyCharField;
        private JTextField keyLocationField;
        private JTextField modifiersField;
        private JTextField extModifiersField;
        private JTextField rawCodeField;
        private JTextField primaryLevelUnicodeField;
        private JTextField scancodeField;
        private JTextField extendedKeyCodeField;

        public KeySpyPanel() {
            createPartControl();
        }

        private void createPartControl() {
            panel = new JPanel();
            panel.setLayout(new GridBagLayout());

            int gridy = 0;

            JLabel anyKeyLabel = new JLabel("Press any key");
            anyKeyLabel.setFont(anyKeyLabel.getFont().deriveFont(36F));
            anyKeyLabel.setHorizontalAlignment(JLabel.CENTER);
            addComponent(panel, anyKeyLabel, 0, gridy++, 2, 1, normalInsets,
                    GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL);

            JLabel keyCodeLabel = new JLabel("KeyCode:");
            addComponent(panel, keyCodeLabel, 0, gridy, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            keyCodeField = new JTextField(20);
            keyCodeField.setEditable(false);
            addComponent(panel, keyCodeField, 1, gridy++, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            JLabel keyTextLabel = new JLabel("KeyText:");
            addComponent(panel, keyTextLabel, 0, gridy, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            keyTextField = new JTextField(20);
            keyTextField.setEditable(false);
            addComponent(panel, keyTextField, 1, gridy++, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            JLabel keyCharLabel = new JLabel("KeyChar:");
            addComponent(panel, keyCharLabel, 0, gridy, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            keyCharField = new JTextField(20);
            keyCharField.setEditable(false);
            addComponent(panel, keyCharField, 1, gridy++, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            JLabel keyLocationLabel = new JLabel("KeyLocation:");
            addComponent(panel, keyLocationLabel, 0, gridy, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            keyLocationField = new JTextField(20);
            keyLocationField.setEditable(false);
            addComponent(panel, keyLocationField, 1, gridy++, 1, 1,
                    normalInsets, GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            JLabel modifiersLabel = new JLabel("Modifiers:");
            addComponent(panel, modifiersLabel, 0, gridy, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            modifiersField = new JTextField(20);
            modifiersField.setEditable(false);
            addComponent(panel, modifiersField, 1, gridy++, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            JLabel extModifiersLabel = new JLabel("ExtModifiers:");
            addComponent(panel, extModifiersLabel, 0, gridy, 1, 1,
                    normalInsets, GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            extModifiersField = new JTextField(20);
            extModifiersField.setEditable(false);
            addComponent(panel, extModifiersField, 1, gridy++, 1, 1,
                    normalInsets, GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            JLabel rawCodeLabel = new JLabel("RawCode:");
            addComponent(panel, rawCodeLabel, 0, gridy, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            rawCodeField = new JTextField(20);
            rawCodeField.setEditable(false);
            addComponent(panel, rawCodeField, 1, gridy++, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            JLabel primaryLevelUnicodeLabel = new JLabel("PrimaryLevelUnicode:");
            addComponent(panel, primaryLevelUnicodeLabel, 0, gridy, 1, 1,
                    normalInsets, GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            primaryLevelUnicodeField = new JTextField(20);
            primaryLevelUnicodeField.setEditable(false);
            addComponent(panel, primaryLevelUnicodeField, 1, gridy++, 1, 1,
                    normalInsets, GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            JLabel scancodeLabel = new JLabel("Scancode:");
            addComponent(panel, scancodeLabel, 0, gridy, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            scancodeField = new JTextField(20);
            scancodeField.setEditable(false);
            addComponent(panel, scancodeField, 1, gridy++, 1, 1, normalInsets,
                    GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            JLabel extendedKeyCodeLabel = new JLabel("ExtendedKeyCode:");
            addComponent(panel, extendedKeyCodeLabel, 0, gridy, 1, 1,
                    bottomInsets, GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);

            extendedKeyCodeField = new JTextField(20);
            extendedKeyCodeField.setEditable(false);
            addComponent(panel, extendedKeyCodeField, 1, gridy++, 1, 1,
                    bottomInsets, GridBagConstraints.LINE_START,
                    GridBagConstraints.HORIZONTAL);
        }

        private void addComponent(Container container, Component component,
                int gridx, int gridy, int gridwidth, int gridheight,
                Insets insets, int anchor, int fill) {
            GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
                    gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0,
                    0);
            container.add(component, gbc);
        }

        public JPanel getPanel() {
            return panel;
        }

        public void setKeyPressed(KeyEvent event) {
            String s = event.toString();

            keyCodeField.setText(getValue("keyCode", s));
            keyTextField.setText(getValue("keyText", s));
            keyCharField.setText(getValue("keyChar", s));
            keyLocationField.setText(getValue("keyLocation", s));
            modifiersField.setText(getValue("modifiers", s));
            extModifiersField.setText(getValue("extModifiers", s));
            rawCodeField.setText(getValue("rawCode", s));
            primaryLevelUnicodeField
                    .setText(getValue("primaryLevelUnicode", s));
            scancodeField.setText(getValue("scancode", s));
            extendedKeyCodeField.setText(getValue("extendedKeyCode", s));
        }

        private String getValue(String key, String line) {
            int sPos = line.indexOf(key);
            if (sPos >= 0) {
                int nPos = sPos + key.length() + 1;
                int ePos = line.indexOf(",", nPos);
                if (ePos < 0) {
                    ePos = line.indexOf("]", nPos);
                }
                if (ePos >= 0) {
                    return line.substring(nPos, ePos);
                }
            }

            return "";
        }

    }

    public class KeyPressedListener extends KeyAdapter {

        private KeySpy keySpyFrame;

        public KeyPressedListener(KeySpy keySpyFrame) {
            this.keySpyFrame = keySpyFrame;
        }

        @Override
        public void keyPressed(KeyEvent event) {
            keySpyFrame.getKeySpyPanel().setKeyPressed(event);
        }
    }

}
package com.ggl.testing;
导入java.awt.Component;
导入java.awt.Container;
导入java.awt.GridBagConstraints;
导入java.awt.GridBagLayout;
导入java.awt.Insets;
导入java.awt.event.KeyAdapter;
导入java.awt.event.KeyEvent;
导入java.awt.event.WindowAdapter;
导入java.awt.event.WindowEvent;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.JTextField;
导入javax.swing.SwingUtilities;
公共类keypy实现了Runnable{
私有密钥PyPanel密钥PyPanel;
公共静态void main(字符串[]args){
invokeLater(新的KeySpy());
}
@凌驾
公开募捐{
最终JFrame=新JFrame(“关键间谍”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(真);
frame.addWindowFocusListener(新的WindowAdapter(){
公共无效windowGainedFocus(WindowEvent e){
frame.requestFocusInWindow();
}
});
keySpyPanel=新的keySpyPanel();
add(keySpyPanel.getPanel());
frame.addKeyListener(新的KeyPressedListener(this));
frame.pack();
frame.setVisible(true);
}
public KeySpyPanel getKeySpyPanel(){
返回面板;
}
公共类密钥共享面板{
私人最终插图底部插图=新插图(10,10,10,10);
私有最终插图normalInsets=新插图(10,10,0,10);
私人JPanel小组;
私有JTextField keyCodeField;
私有JTextField keyTextField;
私有JTextField-keyCharField;
私有JTextField keyLocationField;
私有JTextField修饰符字段;
私有JTextField extModifiersField;
私有JTextField-rawCodeField;
私有JTextField一级UnicoreField;
私有JTextField scancodeField;
私有JTextField扩展keycodefield;
公共密钥共享面板(){
createPartControl();
}
私有void createPartControl(){
panel=新的JPanel();
panel.setLayout(新的GridBagLayout());
int gridy=0;
JLabel anyKeyLabel=新的JLabel(“按任意键”);
anyKeyLabel.setFont(anyKeyLabel.getFont().deriveFont(36F));
anyKeyLabel.setHorizontalAlignment(JLabel.CENTER);
addComponent(面板,任意键标签,0,gridy++,2,1,法线插入,
GridBagConstraints.CENTER,GridBagConstraints.HORIZONTAL);
JLabel keyCodeLabel=新的JLabel(“KeyCode:”);
addComponent(面板、keyCodeLabel、0、网格、1、1、法线插入、,
GridBagConstraints.LINE_开始,
(b)水平的;
keyCodeField=新的JTextField(20);
keyCodeField.setEditable(false);
addComponent(面板,keyCodeField,1,gridy++,1,1,法线插入,
GridBagConstraints.LINE_开始,
(b)水平的;
JLabel keyTextLabel=新的JLabel(“KeyText:”);
addComponent(面板、keyTextLabel、0、网格、1、1、法线插入、,
GridBagConstraints.LINE_开始,
(b)水平的;
keyTextField=新的JTextField(20);
keyTextField.setEditable(假);
addComponent(面板,keyTextField,1,gridy++,1,1,法线插入,
GridBagConstraints.LINE_开始,
(b)水平的;
JLabel keyCharLabel=新的JLabel(“KeyChar:”);
addComponent(面板、keyCharLabel、0、网格、1、1、法线插入、,
GridBagConstraints.LINE_开始,
(b)水平的;
keyCharField=新的JTextField(20);
keyCharField.setEditable(false);
addComponent(面板、keyCharField、1、gridy++、1、1、法线插入、,
GridBagConstraints.LINE_开始,
(b)水平的;
JLabel keyLocationLabel=新的JLabel(“KeyLocation:”);
addComponent(面板,keyLocationLabel,0,网格,1,1,法线插入,
GridBagConstraints.LINE_开始,
(b)水平的;
keyLocationField=新的JTextField(20);
keyLocationField.setEditable(假);
addComponent(面板,keyLocationField,1,gridy++,1,1,
法线插入,GridBagConstraints.LINE\u开始,
(b)水平的;
JLabel修饰符标签=新的JLabel(“修饰符:”);
addComponent(面板、修改器标签、0、网格、1、1、法线插入、,
GridBagConstraints.LINE_开始,
(b)水平的;
modifiersField=新的JTextField(20);
modifiersField.setEditable(false);
addComponent(面板,修改器字段,1,gridy++,1,1,法线插入,
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));

}
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class MediaKeys */

#ifndef _Included_MediaKeys
#define _Included_MediaKeys
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     MediaKeys
 * Method:    songPrevious
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_commands_MediaKeys_songPrevious
  (JNIEnv *, jclass);

/*
 * Class:     MediaKeys
 * Method:    songNext
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_commands_MediaKeys_songNext
  (JNIEnv *, jclass);

/*
 * Class:     MediaKeys
 * Method:    songPlayPause
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_commands_MediaKeys_songPlayPause
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif
JNIEXPORT void JNICALL Java_{package_name}_{class_name}_{method_name}
  (JNIEnv *, jclass);
//standard dependencies for C and the JNI Library
#include <jni.h>
#include <stdio.h>
#include "MediaKeys.h"

//dependencies required to hit the media keys
#define WINVER 0x0500
#include <windows.h>


//hits the previous track key
JNIEXPORT void JNICALL Java_commands_MediaKeys_songPrevious (JNIEnv *env, jobject thisObj) {

    KEYBDINPUT kbi;

    //specific keycode
    kbi.wVk = VK_MEDIA_PREV_TRACK; //this can be changed depending on the key

    kbi.wScan = 0;
    kbi.dwFlags = 0;
    kbi.time = 0;
    kbi.dwExtraInfo = (ULONG_PTR) GetMessageExtraInfo();

    INPUT input;
    input.type = INPUT_KEYBOARD;
    input.ki   = kbi;

    SendInput(1, &input, sizeof(INPUT));

    return;

}


//hits the next track key
JNIEXPORT void JNICALL Java_commands_MediaKeys_songNext (JNIEnv *env, jobject thisObj) {

    KEYBDINPUT kbi;

    //specific keycode
    kbi.wVk = VK_MEDIA_NEXT_TRACK;

    kbi.wScan = 0;
    kbi.dwFlags = 0;
    kbi.time = 0;
    kbi.dwExtraInfo = (ULONG_PTR) GetMessageExtraInfo();

    INPUT input;
    input.type = INPUT_KEYBOARD;
    input.ki   = kbi;

    SendInput(1, &input, sizeof(INPUT));

    return;

}


//hits the play/pause key
JNIEXPORT void JNICALL Java_commands_MediaKeys_songPlayPause (JNIEnv *env, jobject thisObj) {

    KEYBDINPUT kbi;

    //specific keycode
    kbi.wVk = VK_MEDIA_PLAY_PAUSE;

    kbi.wScan = 0;
    kbi.dwFlags = 0;
    kbi.time = 0;
    kbi.dwExtraInfo = (ULONG_PTR) GetMessageExtraInfo();

    INPUT input;
    input.type = INPUT_KEYBOARD;
    input.ki   = kbi;

    SendInput(1, &input, sizeof(INPUT));

    return;

}

JNIEXPORT void JNICALL Java_{package_name}_{class_name}_{method_name} (JNIEnv *env, jobject thisObj) {

    //specific code goes here
    return;

}