我正在为我的朋友制作一个机器人,他正在抽搐,他忘了从他的" brb"现场对他的游戏"在xsplit的场景,所以他想做一些mods可以改变或控制他的计算机上的东西,如果他忘了。很容易制作机器人。
代码很容易制作,就像这样。
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.jibble.pircbot.*;
public class Twitchbot extends PircBot {
public Twitchbot() {
this.setName("Rex__Bot");
}
public void onMessage(String channel, String sender, String login, String hostname, String message) {
if(message.equals("Something")) {
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_Something);
r.delay(300);
r.keyRelease(KeyEvent.VK_Something);
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
}
我想知道是否有办法让GUI将消息等于的字母和keyevent.VK_Something更改为与GUI不同的内容,这样他就可以轻松编辑它。
答案 0 :(得分:0)
因此,首先,您需要一些方法来捕获所需的信息。基本上,您需要消息和击键。击键由虚拟键和修饰符组成。
以下示例基本上提供了一种用户可以键入消息文本和键击的方法。捕获窗格使用KeyListener
来监视关键事件,并通过从关键事件中提取各种值,将存储所需的信息并向用户显示关键字符。
您可以将此信息保存到Properties
文件中,以后可以加载...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.Border;
public class Test {
public static void main(String[] args) {
new Test ();
}
public Test () {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new ConfigurationPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ConfigurationPane extends JPanel {
private KeyPressPane keyPressPane;
private JTextField name;
public ConfigurationPane() {
name = new JTextField(10);
keyPressPane = new KeyPressPane();
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.HORIZONTAL;
add(name, gbc);
add(keyPressPane, gbc);
JButton save = new JButton("Save");
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Properties p = new Properties();
p.setProperty("name", name.getText());
p.setProperty("keyCode", Integer.toString(keyPressPane.getKeyCode()));
p.setProperty("modifiers", Integer.toString(keyPressPane.getModifiers()));
try (OutputStream os = new FileOutputStream(new File("Config.cfg"))) {
p.store(os, "Key config");
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
add(save);
}
}
public static class KeyPressPane extends JPanel {
protected static final Border FOCUSED_BORDER = BorderFactory.createLineBorder(UIManager.getColor("List.selectionBackground"));
protected static final Border UNFOCUSED_BORDER = UIManager.getBorder("TextField.border");
private JLabel label;
private int keyCode;
private int modifiers;
private char key;
public KeyPressPane() {
setBackground(UIManager.getColor("TextField.background"));
setLayout(new GridBagLayout());
label = new JLabel(" ");
label.setFont(UIManager.getFont("Label.font").deriveFont(48f));
add(label);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
keyCode = e.getKeyCode();
modifiers = e.getModifiers();
}
@Override
public void keyTyped(KeyEvent e) {
char key = e.getKeyChar();
label.setText(Character.toString(key));
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
requestFocusInWindow();
}
});
addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
setBorder(FOCUSED_BORDER);
}
@Override
public void focusLost(FocusEvent e) {
System.out.println("unfocused");
setBorder(UNFOCUSED_BORDER);
}
});
setBorder(UNFOCUSED_BORDER);
setFocusable(true);
}
public int getKeyCode() {
return keyCode;
}
public int getModifiers() {
return modifiers;
}
}
}
然后,您需要加载Properties
文件并创建新的KeyStroke
...
Properties p = new Properties();
try (InputStream is = new FileInputStream(new File("Config.cfg"))) {
p.load(is);
String name = p.getProperty("name");
int keyCode = Integer.parseInt(p.getProperty("keyCode"));
int modifiers = Integer.parseInt(p.getProperty("modifiers"));
KeyStroke ks = KeyStroke.getKeyStroke(keyCode, modifiers);
System.out.println(ks);
} catch (IOException exp) {
exp.printStackTrace();
}