当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。