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


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

說明

調用時,Keyboard.press() 的作用就像在鍵盤上按下並按住一個鍵。使用 modifier keys 時很有用。要結束按鍵,請使用 Keyboard.release()Keyboard.releaseAll()

在使用 press() 之前必須調用 Keyboard.begin()

用法

Keyboard.press(key)

參數

key :要按下的鍵。允許的數據類型:char

返回

發送的按鍵次數。數據類型:size_t

示例代碼

#include <Keyboard.h>

// use this option for OSX:
char ctrlKey = KEY_LEFT_GUI;
// use this option for Windows and Linux:
//  char ctrlKey = KEY_LEFT_CTRL;

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);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
  while (digitalRead(2) == HIGH) {
    // do nothing until pin 2 goes low
    delay(500);
  }
  delay(1000);
  // new document:
  Keyboard.press(ctrlKey);
  Keyboard.press('n');
  delay(100);
  Keyboard.releaseAll();
  // wait for new window to open:
  delay(1000);
}

相關用法


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