Skip to content

Instantly share code, notes, and snippets.

@tokyoff
Last active July 22, 2020 11:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokyoff/5dd819ea4265ef2382d6c6de7f2ecb03 to your computer and use it in GitHub Desktop.
Save tokyoff/5dd819ea4265ef2382d6c6de7f2ecb03 to your computer and use it in GitHub Desktop.
キースイッチを押す回数によって動作が変えられるワンボタンキーボード用ソースコード
//--------------------------------------------------
// キースイッチを押す回数によって動作が変えられるキーボード
// 1回押し:コピー(ctrl + c)
// 2回押し:ペースト(ctrl + v)
// 3回押し:カット(ctrl + x)
//
// https://www.one-button-key.com/
//--------------------------------------------------
#include "Keyboard.h"
#define PIN_KEYSW (9)
int prevKeyState;
int currKeyState;
int numPush; // キースイッチが押された回数
unsigned long prevPushTime; // 前回キースイッチが押された時刻
void setup() {
pinMode(PIN_KEYSW, INPUT_PULLUP);
prevKeyState = HIGH;
currKeyState = HIGH;
Keyboard.begin();
numPush = 0;
prevPushTime = millis();
}
void loop() {
currKeyState = digitalRead(PIN_KEYSW);
// キースイッチが押された
if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
numPush++;
prevPushTime = millis();
}
if (numPush != 0) {
// 前回押されてから200ms以上経過
if (millis() - prevPushTime > 200) {
// 押された回数に応じてキー操作を実行
switch (numPush) {
case 1:
// コピー(ctrl + c)
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('c');
break;
case 2:
// ペースト(ctrl + v)
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('v');
break;
default:
// カット(ctrl + x)
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('x');
break;
}
delay(10);
Keyboard.releaseAll();
numPush = 0;
}
}
prevKeyState = currKeyState;
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment