我今天正在尝试制作一个arduino项目,该项目可以基于Java应用程序处理的串行通信来“复制”或仿真键盘。
因此,我正在尝试编写一个与Arduino Mega接口自身的Java应用程序,以“复制”键盘,并且我需要Java应用程序将接收到的串行字节“转换”为击键,以发送给OS本身。问题是无论我如何尝试在连续读取字节保持不变的情况下保持按下键的状态,都会陷入单一按下键的情况或在几乎没有时间的情况下发送无限次的击键循环({{3 }},game I'm currently doing this project for,where the idea came from,expected behaviour ...)
我也想说我知道我可以对Arduino固件进行重新编程,使其成为另一个“桥”芯片,以使键盘库正常工作,但是我也希望能够将该板用于其他项目,并且只需重新编译并上传我每次需要的草图并在一分钟内切换项目对我来说是必须的,因为我只买了一块板子,用廉价的设备而不是“ Fabriano的”节省了一些妈妈的学校预算。 “一个(我居住的知名,好品牌而不是便宜品牌)...
这是我的Arduino代码(如果需要,但是可以完美地完成它的工作):
const int SECOND = 1000;
const int Hz = 2; // change to 250Hz~1000Hz when ready to use
const int firstPin = A6;
const int secondPin = A7;
void setup() {
pinMode(13, OUTPUT);
pinMode(firstPin, INPUT);
pinMode(secondPin, INPUT);
analogWrite(firstPin, LOW); // initialize to LOW both pins to overcome
analogWrite(secondPin, LOW); // incorrect read values in the future
}
void sendData() {
if (analogRead(firstPin) > 300) {
Serial.write(0b00001011);
}
else {
Serial.write(0b00001010);
}
if (analogRead(secondPin) > 300) {
Serial.write(0b00010101);
}
else {
Serial.write(0b00010100);
}
}
int connectionSetup() {
Serial.begin(9600);
while(!Serial.available()) {
;
}
if(Serial.read()==0b10101010) {
Serial.write(0b10101010);
Serial.write(0b01010101);
while(!Serial.available()) {
;
}
if(Serial.read()==0b01010101) {
return 0;
}
}
return -1;
}
void loop() {
if(connectionSetup() == 0) {
while(true) {
sendData();
delay (SECOND / Hz);
}
}
}
这是我的越野车Java代码:
package ArduinoSerialKeyboard;
import com.fazecast.jSerialComm.*;
import java.awt.AWTException;
import java.io.IOException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
/**
*
* @author BJPGameVideosITA
*/
public class Startup {
private static SerialPort sp = SerialPort.getCommPort("COM5"); // device name, change it if needed
private static int key1 = KeyEvent.VK_Q, key2 = KeyEvent.VK_W; // change if needed
private static boolean key1state = false, key2state = false;
private static Thread t;
private static int setup() throws IOException, InterruptedException {
sp.setComPortParameters(9600, 8, 1, 0); // default connection settings for Arduino
sp.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 0, 0); // block until bytes can be read
t = new Thread(new Runnable() {
@Override
public void run() {
pressKey.setup(key1, key2);
}
});
t.start();
if (sp.openPort()) {
System.out.println("Port opened!");
Thread.sleep(1000); // waits for the serial connection to be established and ready
sp.getOutputStream().write(0b10101010);
sp.getOutputStream().flush();
while(sp.getInputStream().available() < 1) {
;
}
if(sp.getInputStream().read()==0b10101010) {
while(sp.getInputStream().available() < 1) {
;
}
if(sp.getInputStream().read()==0b01010101) {
sp.getOutputStream().write(0b01010101);
sp.getOutputStream().flush();
return 0;
}
}
System.out.println("Error in synchronization!");
return -1;
}
else {
System.out.println("Error in opening port!");
return -1;
}
}
private static int read() throws IOException {
while(sp.getInputStream().available() < 1) {
;
}
int b = sp.getInputStream().read();
return b;
}
private static void keyboard() throws IOException {
boolean exit = false;
do {
int read = read();
if(read == -1) {
break;
}
switch (read) {
case 10: if(!key1state) {pressKey.stop(1); key1state=!key1state;} break;
case 11: if(key1state) {pressKey.start(1); key1state=!key1state;} break;
case 20: if(!key2state) {pressKey.stop(2); key2state=!key2state;} break;
case 21: if(key2state) {pressKey.start(2); key2state=!key2state;} break;
case 0: exit = true; break;
}
} while (!exit);
}
private static int close() {
if (sp.closePort()) {
System.out.println("Port closed!");
}
else {
System.out.println("Error in closing port!");
return -1;
}
return 0;
}
public static void main(String[] args) throws IOException, InterruptedException {
setup();
keyboard();
close();
}
}
class pressKey {
private static boolean toPress[] = {false, false};
private static int key1, key2;
public static void setup(int k1, int k2) {
key1 = k1;
key2 = k2;
}
private static void run(int key) {
try {
Robot robot = new Robot();
if(key == key1) {
while (toPress[0]) {
robot.keyPress(key1);
}
robot.keyRelease(key1);
}
if(key == key2) {
while (toPress[1]) {
robot.keyPress(key2);
}
robot.keyRelease(key2);
}
} catch (AWTException e) {
e.printStackTrace();
}
}
public static void start(int key) {
toPress[key-1] = true;
if(key == 1)
run(key1);
if(key == 2)
run(key2);
}
public static void stop(int key) {
toPress[key-1] = false;
}
}
在此先感谢大家,感谢您的帮助。
PS:我发现了其他StackOverflow问题,但是无论如何,它们都与按下按键有关,而我需要像按下键一样将其按下,并由操作系统处理(这意味着它必须停下来第一次按下后不久,然后每隔TimeoutSetByTheOperatingSystem秒按下一次,而不是像视频所示那样粉碎超过9000次,因为前面提到的该游戏需要按下键,而无需粉碎任何键来按住“滑块”,如“想法来自“视频...有关游戏本身的更多信息current behaviour。),我已经尝试了所有方法,但均未成功……谢谢!