前置き
- micro:bit駆け出しエンジニアです。超初心者。
はじめに
- microbit-pxt-blehidを使えばHIDをmicro:bitで作成できるっぽい。
- 上記ライブラリを用いて擬似的なキーボードとマウスを作成し、実際に動かしてみる。
- 実装については20分程度で作れる規模感です。
一応Arduinoを使って実装する方法については先駆者あり。
↓参考
microbit-pxt-blehidについて
- micro:bit V2である必要あり。
- windows/macOSについて動作確認済み
- Android/iOSについては機能制限あり。
基本的な使い方は上記ページを参照すればあらかた理解できそう。
使ったもの
micro:bitについてはV2.21を使用した。下記リンクのワンセットで揃った物品のみ使用した。
また、動作環境として使用したPCスペックは下記。
- macOS Ventura 13.0

プロジェクト設定について
歯車マーク→プロジェクト設定にて、No Pairring Required
を設定して実施した。

keyboard入力機能について
単純な文字を送る他にも、EnterキーやVolume upキーなどの特殊キーやCtrl+Aみたいな組み合わせも可能


実際に組み合わせる際には、文字の連結を行う。

マウス機能について
基本的には見ての通りの機能で、そのままブロックを使うことができる。
ただ、カーソル量のx,y軸とスクロール量は共に-127~127の値をとり、0を不動とした相対移動量を入れるため注意。

実装したコード
ブロックプログラミング故、超シンプル。すばらしい。
keyboard
ボタンAを押すとhttps://www.google.com/
とGoogleのページリンクがタイプされ、ボタンBを押すとEnterが入力される。また、ボタンAとボタンBを同時押しするとスクリーンショット画面になる。

一応pythonコードはこちら。
def on_button_pressed_a():
keyboard.send_string("https://www.google.com/")
input.on_button_pressed(Button.A, on_button_pressed_a)
def on_button_pressed_ab():
keyboard.send_string("" + keyboard.modifiers(keyboard._Modifier.APPLE) + keyboard.modifiers(keyboard._Modifier.SHIFT) + "5")
input.on_button_pressed(Button.AB, on_button_pressed_ab)
def on_button_pressed_b():
keyboard.send_string(keyboard.keys(keyboard._Key.ENTER))
input.on_button_pressed(Button.B, on_button_pressed_b)
keyboard.start_keyboard_service()
キーボード入力動作確認
ちゃんと動いた。キー組み合わせのスクショ機能もちゃんと動いてる。
だいたい10分位でつくったけど、結構便利な感じ。
マウス操作
micro:bitを傾けると傾けた方向にマウスカーソルが移動する。また、ボタンAを押すと左クリック、ボタンBを押すと右クリック、AボタンとBボタン同時押しで中クリックになる。

def on_button_pressed_a():
mouse.click()
input.on_button_pressed(Button.A, on_button_pressed_a)
def on_button_pressed_ab():
mouse.middle_click()
input.on_button_pressed(Button.AB, on_button_pressed_ab)
def on_button_pressed_b():
mouse.right_click()
input.on_button_pressed(Button.B, on_button_pressed_b)
mouse.start_mouse_service()
def on_forever():
mouse.movexy(input.rotation(Rotation.ROLL) / 360 * 127,
input.rotation(Rotation.PITCH) / 360 * 127)
basic.forever(on_forever)
マウス動作確認
動いた。マウス操作はちょっとおもしろい。
ちなみに、今回のマウスは着手して10分程度でつくれた。
おわりに
- Bluetoothを介した簡単なキー入力、マウス操作をmicro:bit上から行った。
- microbit-pxt-blehidの導入が必要。
- HIDをまさかものの数分で作れるとはなぁ...