gpt4 book ai didi

java - 模拟在 Mac 上按下 "Command"键

转载 作者:行者123 更新时间:2023-11-30 02:30:11 24 4
gpt4 key购买 nike

我目前正在尝试用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);
}

}

最佳答案

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 block 包围,或者在方法中抛出 AWTException标题

关于java - 模拟在 Mac 上按下 "Command"键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44480166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com