【问题标题】:Simulate the pressing of the "Command" key on a Mac在 Mac 上模拟按下“Command”键
【发布时间】:2017-06-13 05:57:25
【问题描述】:

我目前正在尝试在 java 中创建一个 tamogatchi 游戏,但是,我需要一种方法来清除输出屏幕,以便健康栏/食物栏在保持相同位置时下降。我发现按 Command+L(在 Mac 上)将清除输出。但是,我找不到它的 KeyEvent 命令。这是我目前拥有的

package tamogatchu;

import java.util.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class Tamogatchu {

    public static void main(String[] args) throws InterruptedException, AWTException {
        initialize();
    }

    public static void initialize() throws InterruptedException, AWTException {
        int full = 10;
        int health = 10;
        int happy = 10;
        int idk = 0;
        int poop = 0;
        String[] food = new String[10];
        String[] healthbar = new String[10];
        String[] happyness = new String[10];

        for (int i = 0; i < 10; i++) {
            food[i] = "◼";
        }
        for (int i = 0; i < 10; i++) {
            healthbar[i] = "◼";
        }
        for (int i = 0; i < 10; i++) {
            happyness[i] = "◼";
        }
        Print(food, full, healthbar, health, idk, happyness, happy, poop);

    }

    public static void tamogatchu(String[] food, int full, String[] healthbar, int health, int idk, String[] happyness, int happy, int poop) throws InterruptedException, AWTException {
        //int timer = 0;
        Scanner input = new Scanner(System.in);
        String care = "";
        String hungery = "";
        String healthery = "";
        String happery = "";

        while (health > 0) {
            if (full != 0) {
                Thread.sleep(10000);
            } else {
                Thread.sleep(5000);
            }

            Robot r = new Robot();
            r.keyPress(KeyEvent.VK_L);
            // the command⌘ key should be pressed here r.keyPress(KeyEvent.);
            //r.keyRelease(KeyEvent.);
            r.keyRelease(KeyEvent.VK_L);

            String Line = Arrays.toString(food);
            String result = Line.replaceAll("[-+.^:, ]", "");
            hungery = "Hunger: " + result;
            System.out.println(hungery);

            String Line2 = Arrays.toString(healthbar);
            String result2 = Line2.replaceAll("[-+.^:, ]", "");
            healthery = "Health: " + result2;
            System.out.println(healthery);

            String Line3 = Arrays.toString(happyness);
            String result3 = Line3.replaceAll("[-+.^:, ]", "");
            happery = "Happieness: " + result3;
            System.out.println(happery);

            if (idk == 0) {

                System.out.println("  .^._.^.\n"
                        + "  | . . |\n"
                        + " (  ---  )\n"
                        + " .'     '.\n"
                        + " |/     \\|\n"
                        + "  \\ /-\\ /\n"
                        + "   V   V");
                idk = 1;

            } else {

                System.out.println("  .^._.^.\n"
                        + "  | . . |\n"
                        + " (  ---  )\n"
                        + " |/     \\|\n"
                        + "  \\ /-\\ /\n"
                        + "   V   V");
                idk = 0;
            }
            System.out.println("Poops: " + poop);

            System.out.println("take care of your pet?  -type 'feed' to feed or 'clean' to get rid of poops-");
            care = input.nextLine();
            if (care.equalsIgnoreCase("feed")) {
                full += 5;
                if (full > 10) {
                    full = 10;
                }
            } else if (care.equalsIgnoreCase("clean")) {
                poop = 0;
            }
            if (full % 2 == 0) {
                poop++;
                happy -= poop;
            }
            if (full > 7 && health < 10 || full > 5 || poop == 0) {
                if (full > 7 && health < 10) {
                    health++;
                    if (health > 10) {
                        health = 10;
                    }
                }
                if (full > 5) {
                    happy++;
                    if (happy > 10) {
                        happy = 10;
                    }
                }
                if (poop == 0) {
                    happy++;
                    if (happy > 10) {
                        happy = 10;
                    }
                }
            }
            if (full != 0) {
                full--;
                if (full <= 5) {
                    happy--;
                }

                if (full == 0) {
                    happy--;
                    health--;
                } else if (happy == 0) {
                    health--;
                }

                Print(food, full, healthbar, health, idk, happyness, happy, poop);
            }

            if (full == 0 || happy == 0) {

                if (full == 0) {
                    happy--;
                    health--;
                } else if (happy == 0) {
                    health--;
                }
                Print(food, full, healthbar, health, idk, happyness, happy, poop);

            }


        }

        System.out.println("Hunger: [◻◻◻◻◻◻◻◻◻◻]");
        System.out.println("Health: [◻◻◻◻◻◻◻◻◻◻]");
        System.out.println("Happieness: [◻◻◻◻◻◻◻◻◻◻]");
        System.out.println("  .^._.^.\n"
                + "  | x x |\n"
                + " (  ---  )\n"
                + " .'     '.\n"
                + " |/     \\|\n"
                + "  \\ /-\\ /\n"
                + "   V   V");

        System.out.println("YOU KILLED IT");
        System.exit(0);
    }

    public static void Print(String[] food, int full, String[] healthbar, int health, int idk, String[] happyness, int happy, int poop) throws InterruptedException, AWTException {
        for (int i = 0; i < full; i++) {
            food[i] = "◼";
        }
        if (-1 + full >= 0) {
            for (int i = 9; i > -1 + full; i--) {
                food[i] = "◻";
            }
        } else {
            for (int i = 0; i < 10; i++) {
                food[i] = "◻";
            }
        }

        for (int i = 0; i < health; i++) {
            healthbar[i] = "◼";
        }
        for (int i = 9; i > -1 + health; i--) {
            healthbar[i] = "◻";
        }

        for (int i = 0; i < happy; i++) {
            happyness[i] = "◼";
        }
        if (-1 + happy >= 0) {
            for (int i = 9; i > -1 + happy; i--) {
                happyness[i] = "◻";
            }
        } else {
            for (int i = 0; i < 10; i++) {
                happyness[i] = "◻";
            }
        }
        tamogatchu(food, full, healthbar, health, idk, happyness, happy, poop);
    }

}

【问题讨论】:

    标签: java macos


    【解决方案1】:

    在Robot 类中,Command(⌘) 的名称是KeyEvent.VK_META。您的代码应如下所示:

    Robot r = new Robot();
    r.keyPress(KeyEvent.VK_META); //press command
    r.keyPress(KeyEvent.VK_L); //press l
    r.keyRelease(KeyEvent.VK_META); //release command
    r.keyRelease(KeyEvent.VK_L); //release l
    

    这会按下 Command 键,按下 L 键,然后同时释放它们。我已经对此进行了测试,它可以正常工作。

    注意:使用Robot 会抛出AWTException,因此它需要您用try-catch 块包围,或者在方法头中抛出AWTException

    【讨论】:

    猜你喜欢
    • 2021-05-09
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    相关资源
    最近更新 更多