4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ESP32 の BleKeyboard ライブラリを使って Ctrl + V とかする方法

Posted at

ESP32 の BleKeyboard ライブラリ

ESP32をArduinoでプログラムするときにBleKeyboardライブラリを使うことで、キーボード入力の動きをさせることができます。

#include <BleKeyboard.h>

キー押下イベントの送信

ライブラリのREADMEに記載されている通り、以下のように書くことで、Bluetooth接続されたPCやタブレット等に対してキーボード入力されたイベントを擬似的に発生させることができます。

キーコードを送信する:write

// RETURNキー押下イベントを発生させるとき
bleKeyboard.write(KEY_RETURN);

文字を入力する:print

// Hello worldという文字を入力させるとき
bleKeyboard.print("Hello world");

Ctrl+Alt+Deleteの例:press

押したままの状態にしたいとき(同時押しをさせたい時)はpressを使用します。
押した状態を解除する時にはreleaseAllをすれば良いです。

// Ctrl+Alt+Delete を発生させる ※気をつけて使用すること!
bleKeyboard.press(KEY_LEFT_CTRL);
bleKeyboard.press(KEY_LEFT_ALT);
bleKeyboard.press(KEY_DELETE);
delay(100);
bleKeyboard.releaseAll();

では、Ctrl+V とかは?

これが少しだけハマったのでこちらに投稿することにしました。
こう書けば良いです。Alt+Aとかでも同様に書けます。

// Ctrl+V
bleKeyboard.press(KEY_LEFT_CTRL);
bleKeyboard.press('v');
delay(50);
bleKeyboard.releaseAll();

bleKeyboard.press('v'); というのがポイントですね。
Ctrl+Alt+Deleteの例と同様、delayを入れないとうまく動作しません。READMEの方は100msでしたが、50msでも動作したのでそう書いています。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?