當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Arduino Keyboard.write()用法及代碼示例

說明

向連接的計算機發送擊鍵。這類似於按下和釋放鍵盤上的鍵。您可以發送一些 ASCII 字符或附加的 keyboard modifiers and special keys

僅支持鍵盤上的 ASCII 字符。例如,ASCII 8(退格)可以工作,但 ASCII 25(替換)不行。發送大寫字母時,Keyboard.write() 發送一個 shift 命令加上所需的字符,就像在鍵盤上打字一樣。如果發送數字類型,它將作為 ASCII 字符發送(例如 Keyboard.write(97) 將發送 'a')。

有關 ASCII 字符的完整列表,請參閱 ASCIITable.com

用法

Keyboard.write(character)

參數

character :要發送到計算機的字符或 int。可以以字符可接受的任何表示法發送。例如,以下所有內容都是可接受的,並發送相同的值,65 或 ASCII A:

Keyboard.write(65);         // sends ASCII value 65, or A
Keyboard.write('A');            // same thing as a quoted character
Keyboard.write(0x41);       // same thing in hexadecimal
Keyboard.write(0b01000001); // same thing in binary (weird choice, but it works)

返回

發送的字節數。數據類型:size_t

示例代碼

#include <Keyboard.h>

void setup() {
  // make pin 2 an input and turn on the
  // pullup resistor so it goes high unless
  // connected to ground:
  pinMode(2, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  //if the button is pressed
  if (digitalRead(2) == LOW) {
    //Send an ASCII 'A',
    Keyboard.write(65);
  }
}

注意事項和警告

當您使用 Keyboard.write() 命令時,Arduino 將接管您的鍵盤!在使用該命令之前,請確保您有控製權。切換鍵盤控製狀態的按鈕有效。

相關用法


注:本文由純淨天空篩選整理自arduino.cc大神的英文原創作品 Keyboard.write()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。