Keyboard.press()
描述
送出按下鍵盤按鍵的命令給已連接的電腦(相當於按著鍵盤按鍵不放),您也可以配合修飾鍵來使用。若要放開鍵盤按鍵,請呼叫 Keyboard.release() 函式,或者 Keyboard.releaseAll() 函式。
在呼叫 Keyboard.press()
函式之前,要先呼叫 Keyboard.begin() 函式。
語法
Keyboard.press(key)
參數
key
: 要按下的按鍵(char 型別)
回傳
無回傳值
範例
// 在 OSX 系統中使用的 ctrl 按鍵: char ctrlKey = KEY_LEFT_GUI; // 在 Windows 或 Linux 下使用的 ctrl 按鍵: // char ctrlKey = KEY_LEFT_CTRL; void setup() { // 將 pin 2 設定為 INPUT_PULLUP 狀態 // 這樣它就會在外部按鈕被按下時 // 呈現 LOW 的狀態 pinMode(2, INPUT_PULLUP); // 開始 HID 鍵盤模擬 Keyboard.begin(); } void loop() { while (digitalRead(2) == HIGH) { // 不做任何事情直到 pin 2 為 LOW delay(500); } delay(1000); // 在 IDE 上打開新的分頁 Keyboard.press(ctrlKey); Keyboard.press('n'); delay(100); Keyboard.releaseAll(); // 等待新的分頁開啟 delay(1000); }
See also
- Keyboard.begin()
- Keyboard.end()
- Keyboard.print()
- Keyboard.println()
- Keyboard.release()
- Keyboard.releaseAll()
- Keyboard.write()
The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.