功能說明影片: https://youtu.be/Q_RJu8zXVt8
材料:
arduino leonardo 開發板 (https://tw.shp.ee/V3ByXeL)
五色按鈕模組(https://tw.shp.ee/hXaFgy4)
遊戲搖桿XY軸(https://tw.shp.ee/dANwsf3)
2024/08/19
ZoomIt 快捷鍵焊接示範影片:
1. 五色按鈕焊接示範影片 https://youtu.be/kXjivW_kw6Q
2. 控制板焊接示範影片 https://youtu.be/oMr3748ipLM
五個快速鍵對照ZoomIt微軟螢幕畫筆功能:
第1顆按鈕(接至開發板P2),螢幕放大+畫筆(相當於按下Ctrl+1),再按一次結束畫筆
第2顆按鈕(接至開發板P3),藍色畫筆(相當於按下Ctrl+2 -> b鍵),再按一次結束畫筆
第3顆按鈕(接至開發板P4),紅色畫筆(相當於按下Ctrl+2 -> r鍵),再按一次結束畫筆
第4顆按鈕(接至開發板P5),黃色畫筆(相當於按下Ctrl+2 -> y鍵),再按一次結束畫筆
第5顆按鈕(接至開發板P6),返回上一步(相當於按下Ctrl+z)
遊戲搖桿X軸(VRX接至開發板A0),滑鼠水平移動
遊戲搖桿Y軸(VRY接至開發板A1),滑鼠垂直移動
按下遊戲搖桿(SW接至開發板P15),開啟/關閉 遊戲搖桿的滑鼠功能
Arduino 程式碼:
#include "Mouse.h"
#include "Keyboard.h"
char Bnumber=0;
int buttonState1,buttonState2,buttonState3,buttonState4,buttonState5;
int lastButtonState1,lastButtonState2,lastButtonState3,lastButtonState4,lastButtonState5;
unsigned long lastDebounceTime1,lastDebounceTime2,lastDebounceTime3,lastDebounceTime4,lastDebounceTime5;
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
#define butPin1 2 //ZoomIt 放大+畫筆:Ctrl + 1
#define butPin2 3 //ZoomIt 藍色畫筆:Ctrl + 2 -> b
#define butPin3 4 //ZoomIt 紅色畫筆:Ctrl + 2 -> r
#define butPin4 5 //ZoomIt 黃色畫筆:Ctrl + 2 -> y
#define butPin5 6 //ZoomIt 回上一步:Ctrl + z
const int ledPin = 9; // Mouse control LED
const int switchPin = 15; // input pin for the mouse pushButton
const int xAxis = A0; // joystick X axis
const int yAxis = A1; // joystick Y axis
int range = 12; // output range of X or Y movement
int responseDelay = 5; // response delay of the mouse, in ms
int threshold = range / 4; // resting threshold
int center = range / 2; // resting position value
bool mouseIsActive = true; // whether or not to control the mouse
int lastSwitchState = LOW; // previous switch state
int color1,color2,color3 ;
void setup() {
Serial.begin(9600);
pinMode(butPin1, INPUT);
pinMode(butPin2, INPUT);
pinMode(butPin3, INPUT);
pinMode(butPin4, INPUT);
pinMode(butPin5, INPUT);
Keyboard.begin();
pinMode(switchPin, INPUT_PULLUP); // the switch pin
pinMode(ledPin, OUTPUT); // the LED pin
// take control of the mouse:
Mouse.begin();
}
void loop() {
int switchState = digitalRead(switchPin);
// if it's changed and it's high, toggle the mouse state:
if (switchState != lastSwitchState) {
Serial.println(switchState);
if (switchState == HIGH) {
mouseIsActive = !mouseIsActive;
// turn on LED to indicate mouse state:
digitalWrite(ledPin, mouseIsActive);
}
} lastSwitchState = switchState;
// read and scale the two axes:
int xReading = readAxis(A0);
int yReading = readAxis(A1);
// if the mouse control state is active, move the mouse:
if (mouseIsActive) {
Mouse.move(xReading, yReading, 0);
delay(responseDelay);
}
int reading1 = digitalRead(butPin1);
int reading2 = digitalRead(butPin2);
int reading3 = digitalRead(butPin3);
int reading4 = digitalRead(butPin4);
int reading5 = digitalRead(butPin5);
if (reading1 != lastButtonState1) lastDebounceTime1 = millis();
if (reading2 != lastButtonState2) lastDebounceTime2 = millis();
if (reading3 != lastButtonState3) lastDebounceTime3 = millis();
if (reading4 != lastButtonState4) lastDebounceTime4 = millis();
if (reading5 != lastButtonState5) lastDebounceTime5 = millis();
if ((millis() - lastDebounceTime1) > debounceDelay) {
if (reading1 != buttonState1) {
buttonState1 = reading1;
if (buttonState1 == HIGH) {
Bnumber=1;
}}}
if ((millis() - lastDebounceTime2) > debounceDelay) {
if (reading2 != buttonState2) {
buttonState2 = reading2;
if (buttonState2 == HIGH) {
Bnumber=2;
}}}
if ((millis() - lastDebounceTime3) > debounceDelay) {
if (reading3 != buttonState3) {
buttonState3 = reading3;
if (buttonState3 == HIGH) {
Bnumber=3;
}}}
if ((millis() - lastDebounceTime4) > debounceDelay) {
if (reading4 != buttonState4) {
buttonState4 = reading4;
if (buttonState4 == HIGH) {
Bnumber=4;
}}}
if ((millis() - lastDebounceTime5) > debounceDelay) {
if (reading5 != buttonState5) {
buttonState5 = reading5;
if (buttonState5 == HIGH) {
Bnumber=5;
}}}
lastButtonState1 = reading1;
lastButtonState2 = reading2;
lastButtonState3 = reading3;
lastButtonState4 = reading4;
lastButtonState5 = reading5;
if(Bnumber==1) { //ctrl+1
Bnumber=99;
Keyboard.press(KEY_LEFT_CTRL);
delay(50);
Keyboard.press(49); //1的ascii code=49
delay(100);
Keyboard.releaseAll();
Serial.println(1);
}
if(Bnumber==2) { //藍色畫筆
Bnumber=99;
color1=1-color1;
color2=0;color3=0;
if (color1==1){
Keyboard.press(KEY_LEFT_CTRL);
delay(20);
Keyboard.press(50); //2的ascii code=50 => ctrl+2
delay(20);
Keyboard.releaseAll();
delay(100);
Keyboard.press(98); //b的ascii code=98
delay(20);
Keyboard.releaseAll();
}else{
Keyboard.press(KEY_ESC); //ESC的ascii code=27
delay(100);
Keyboard.releaseAll();
}
Serial.println(2);
}
if(Bnumber==3) { //紅色畫筆
Bnumber=99;
color2=1-color2;
color1=0;color3=0;
if (color2==1){
Keyboard.press(KEY_LEFT_CTRL);
delay(20);
Keyboard.press(50); //2的ascii code=50 => ctrl+2
delay(20);
Keyboard.releaseAll();
delay(100);
Keyboard.press(114); //r的ascii code=114
delay(20);
Keyboard.releaseAll();
}else{
Keyboard.press(KEY_ESC); //ESC的ascii code=27
delay(100);
Keyboard.releaseAll();
}
Serial.println(3);
}
if(Bnumber==4) { //黃色畫筆
Bnumber=99;
color3=1-color3;
color1=0;color2=0;
if (color3==1){
Keyboard.press(KEY_LEFT_CTRL);
delay(20);
Keyboard.press(50); //2的ascii code=50 => ctrl+2
delay(20);
Keyboard.releaseAll();
delay(100);
Keyboard.press(121); //y的ascii code=121
delay(20);
Keyboard.releaseAll();
}else{
Keyboard.press(KEY_ESC); //ESC的ascii code=27
delay(100);
Keyboard.releaseAll();
}
Serial.println(4);
}
if(Bnumber==5) { //ctrl+z
Bnumber=99;
Keyboard.press(KEY_LEFT_CTRL);
delay(20);
Keyboard.press(122); //z的ascii code=122
delay(20);
Keyboard.releaseAll();
Serial.println(5);
}
}
int readAxis(int thisAxis) {
// read the analog input:
int reading = analogRead(thisAxis);
// map the reading from the analog input range to the output range:
reading = map(reading, 0, 1023, 0, range);
// if the output reading is outside from the rest position threshold, use it:
int distance = reading - center;
if (abs(distance) < threshold)
distance = 0;
// return the distance for this axis:
return distance;
}
2025/01/19補充:快捷鍵(D12)登入Google帳號
#include "Keyboard.h"
const int buttonPin = 12; // input pin for pushbutton
int previousButtonState = HIGH; // for checking the state of a pushButton
int counter = 0; // button push counter
void setup() {
pinMode(buttonPin, INPUT_PULLUP );
delay(5000); //等待5秒才開始執行,避免錯誤引發動作,無法再次燒錄
Keyboard.begin();
}
void loop() {
int buttonState = digitalRead(buttonPin);
if ((buttonState != previousButtonState) && (buttonState == LOW )) {
//1.WIN+D: 回桌面
Keyboard.press(KEY_LEFT_GUI);
Keyboard.press('d');
delay(100);
Keyboard.releaseAll();
//2.CTRL-ALT-C: 執行Chrome捷徑(需先設定)
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press('c');
delay(500);
Keyboard.releaseAll();
//3.自動輸入Google登入頁面(從無痕視窗取得登入網址)
//https://accounts.google.com/v3/signin/identifier?&continue=https%3A%2F%2Fwww.google.com%2F&flowName=GlifWebSignIn&flowEntry=AddSession&dsh=S-1255249990%3A1737214937282063&ddm=1
Keyboard.println("https://reurl.cc/Q5OGmO");
delay(2500);
//4.自動輸入Google帳號
Keyboard.println("xxx@xxx.hcc.edu.tw");
delay(5000);
}
previousButtonState = buttonState;
}