Arduino Leonardoでキーボード操作する
Arduino Leonardo( https://www.switch-science.com/catalog/968/ )では、キーボードとマウスの操作を発生させることができます。
これで勝手にマウス操作したり、ジョイスティックのジャンク品使って別の操作したりとか夢ひろがりまくりんぐです。
以下はWindowsでPower Point2013を操作するサンプル。
#define BAUD_RATE 9600 #define DETECT_INTERVAL 1000 int lastPin = 0; unsigned long detectTimer = 0; void setup(){ Serial.begin(BAUD_RATE); Serial.println("start"); Keyboard.begin(); Mouse.begin(); pinMode(3, INPUT_PULLUP); pinMode(4, INPUT_PULLUP); pinMode(5, INPUT_PULLUP); pinMode(6, INPUT_PULLUP); pinMode(7, INPUT_PULLUP); pinMode(8, INPUT_PULLUP); pinMode(9, INPUT_PULLUP); pinMode(10, INPUT_PULLUP); pinMode(11, INPUT_PULLUP); pinMode(12, INPUT_PULLUP); } void loop() { if (millis() - detectTimer > DETECT_INTERVAL) { boolean updateFlg = false; for (int i=3;i<13;i++) { if (lastPin != i && digitalRead(i) == LOW) { lastPin = i; updateFlg = true; break; } } if (updateFlg == false) { return; } if (digitalRead(3) == LOW) { newSlide(); } else if (digitalRead(4) == LOW) { upArrow(); } else if (digitalRead(5) == LOW) { downArrow(); } else if (digitalRead(6) == LOW) { changePenMode(); changeArrowMode(); } else if (digitalRead(7) == LOW) { changePenMode(); } else if (digitalRead(8) == LOW) { changePenMode(); changeEraserMode(); } else if (digitalRead(9) == LOW) { changePenMode(); changeHighlighterMode(); } else if (digitalRead(10) == LOW) { copy(); } else if (digitalRead(11) == LOW) { cut(); } else if (digitalRead(12) == LOW) { paste(); } detectTimer = millis(); } } void newSlide() { Serial.println("newSlide"); Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_LEFT_SHIFT); Keyboard.press('n'); delay(100); Keyboard.releaseAll(); } void upArrow() { Serial.println("upArrow"); Keyboard.press(KEY_UP_ARROW); delay(100); Keyboard.releaseAll(); } void downArrow() { Serial.println("downArrow"); Keyboard.press(KEY_DOWN_ARROW); delay(100); Keyboard.releaseAll(); } void changeArrowMode() { Serial.println("changeArrowMode"); Keyboard.press(KEY_LEFT_CTRL); Keyboard.press('a'); delay(100); Keyboard.releaseAll(); } void changePenMode() { Serial.println("changePenMode"); Keyboard.press(KEY_LEFT_CTRL); Keyboard.press('p'); delay(100); Keyboard.releaseAll(); } void changeEraserMode() { Serial.println("changeEraserMode"); Keyboard.press(KEY_LEFT_CTRL); Keyboard.press('e'); delay(100); Keyboard.releaseAll(); } void changeHighlighterMode() { Serial.println("changeHighlighterMode"); Keyboard.press(KEY_LEFT_SHIFT); Keyboard.press(KEY_F10); Keyboard.press('o'); Keyboard.press('h'); delay(100); Keyboard.releaseAll(); } void copy() { Serial.println("copy"); Keyboard.press(KEY_LEFT_CTRL); Keyboard.press('c'); delay(100); Keyboard.releaseAll(); } void cut() { Serial.println("cut"); Keyboard.press(KEY_LEFT_CTRL); Keyboard.press('x'); delay(100); Keyboard.releaseAll(); } void paste() { Serial.println("paste"); Keyboard.press(KEY_LEFT_CTRL); Keyboard.press('v'); delay(100); Keyboard.releaseAll(); } // char buf[100]; // snprintf(buf, sizeof(buf)-1, "%d", millis()/1000); // Keyboard_write(buf); void Keyboard_write(const char *p) { while (*p > 0) { if (*p == '\n') { Keyboard.write(KEY_RETURN); } else { Keyboard.write(*p); } p++; } }
※ちなみにMacで操作する場合はKEY_LEFT_CTRLをKEY_LEFT_GUI(Macのコマンドキー / WindowsのWindowsキー)に変更すれば大体ok。.
参考:
http://n.mtng.org/ele/arduino/excel-j.html
http://arduino.cc/en/Reference/MouseKeyboard
http://garretlab.web.fc2.com/arduino_reference/language/functions/leonardo_specific/index.html
http://garretlab.web.fc2.com/arduino_reference/language/functions/leonardo_specific/keyboard/keyboard_modifiers.html