Arduino MKR 零键盘模拟不起作用

问题描述 投票:0回答:1

我买了一个新的Arduino MKR Zero,它通常被宣传为从SD卡(板上内置SD卡插槽)播放声音,但由于微控制器是

SAMD21
,我相信它支持键盘模拟。

我正在尝试使用以下代码模拟从 SD 卡打开文件

code.txt

CTRL ESC
delay(100)
STRING r
delay(100)
STRING notepad
ENTER

在 Arduino MKR Zero 上,我闪现了以下代码:

#include <SD.h>
#include <Keyboard.h>

File codeFile;
File logFile;

void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  delay(2000); // Allow time for the serial monitor to open

  Keyboard.begin();
  // Initialize the SD card
  if (!SD.begin()) { // Change the pin number according to your setup
    Serial.println("SD initialization failed!");
    return;
  }
  else {
    Serial.println("SD Initialized success!");
  }
  
  // Open the code file
  codeFile = SD.open("code.txt");
  if (!codeFile) {
    Serial.println("Failed to open code.txt");
    return;
  }
  else{
    Serial.println("code.txt found in SD Card.");
  }

  // Open the log file
  logFile = SD.open("log.txt", FILE_WRITE); // Open for writing (create if it doesn't exist)
  if (!logFile) {
    Serial.println("Failed to open log.txt");
    return;
  }
  else {
    Serial.println("log.txt opened in SD card.");
  }
  
  // Wait for the computer to recognize the USB connection
  delay(5000);

  // Execute the code from the file
  while (codeFile.available()) {
    char c = codeFile.read();
    Keyboard.write(c); // Send each character from the file as a keystroke
    logFile.write(c); // Write the character to the log file
    Serial.print(c); //print the character in Serial Monitor
    delay(20); // Adjust this delay as needed
  }
  
  // Close the files
  codeFile.close();
  logFile.close();

  Keyboard.end();
  Serial.println("\n\nEnd of code.");
  digitalWrite(LED_BUILTIN, HIGH);

}

void loop() {
  // Nothing to do here
}

代码成功上传到板上。现在,当我重置开发板时,我在串行监视器中得到以下输出。

SD Initialized success!
code.txt found in SD Card.
log.txt opened in SD card.
CTRL ESC
delay(100)
STRING r
delay(100)
STRING notepad
ENTER

End of code.

但是这段代码没有在桌面上执行任何操作。我在这里做错了什么?

arduino arduino-ide arduino-c++
1个回答
0
投票

我找到了解决方案,似乎是驱动程序问题。

我进入设备管理器并看到 2 个驱动程序,

Arduino MKR Zero (COM14)
代码 10
USB Serial Device (COM13)

我卸载了两个驱动程序,重新启动,现在 HID 工作正常。 一个问题是,HID 只是键入

code.txt
文件的内容而不是执行它(我看到它在 Windows 的记事本中键入
GUI r...
)。

我只需要弄清楚如何执行它。

© www.soinside.com 2019 - 2024. All rights reserved.