#include "Mouse.h" #include "Keyboard.h" //There are a few errors in this code, I will upload the corrected one later if I get the time // Set pin numbers for the buttons: const int upButton = 0; const int downButton = 1; const int leftButton = 2; const int rightButton = 3; const int mouseButton = 4; const int rightClickButton = 5; // New pin for right click const int scrollUpButton = 6; // New pin for scroll up const int scrollDownButton = 7; // New pin for scroll down const int shortcutButton1 = 8; // New pins for shortcut buttons const int shortcutButton2 = 9; const int shortcutButton3 = 10; const int shortcutButton4 = 11; const int buzzer = 12; // New pin for buzzer int range = 3; // Decreased for slower mouse movement int responseDelay = 10; long buttonPressTime; // To track long press duration const long longPressDuration = 3000; // 3 seconds for long press void setup() { // Initialize the buttons' inputs: pinMode(upButton, INPUT_PULLUP); pinMode(downButton, INPUT_PULLUP); pinMode(leftButton, INPUT_PULLUP); pinMode(rightButton, INPUT_PULLUP); pinMode(mouseButton, INPUT_PULLUP); pinMode(rightClickButton, INPUT_PULLUP); // Initialize new button pinMode(scrollUpButton, INPUT_PULLUP); pinMode(scrollDownButton, INPUT_PULLUP); pinMode(shortcutButton1, INPUT_PULLUP); pinMode(shortcutButton2, INPUT_PULLUP); pinMode(shortcutButton3, INPUT_PULLUP); pinMode(shortcutButton4, INPUT_PULLUP); pinMode(buzzer, OUTPUT); Mouse.begin(); Keyboard.begin(); } void loop() { // Existing code for mouse movement and left click... // read the buttons: int upState = digitalRead(upButton); int downState = digitalRead(downButton); int rightState = digitalRead(rightButton); int leftState = digitalRead(leftButton); int clickState = digitalRead(mouseButton); // calculate the movement distance based on the button states: int xDistance = (leftState - rightState) * range; int yDistance = (upState - downState) * range; // if X or Y is non-zero, move: if ((xDistance != 0) || (yDistance != 0)) { Mouse.move(xDistance, yDistance, 0); } // if the mouse button is pressed: if (clickState == LOW) { Mouse.press(MOUSE_LEFT); } // else the mouse button is not pressed: else { // if the mouse is pressed, release it: if (clickState == HIGH) { Mouse.release(MOUSE_LEFT); } } // Handle right click checkRightClick(); // Handle scroll checkScroll(); // Handle shortcut buttons checkShortcuts(); delay(responseDelay); } void checkRightClick() { static bool rightClickHeld = false; int rightClickState = digitalRead(rightClickButton); if (rightClickState == LOW) { if (!rightClickHeld) { buttonPressTime = millis(); // Start timing rightClickHeld = true; Mouse.press(MOUSE_RIGHT); } else if (millis() - buttonPressTime >= longPressDuration) { // Long press detected Mouse.release(MOUSE_RIGHT); // Toggle right click hold state playSound(rightClickHeld); // Play sound rightClickHeld = !rightClickHeld; } } else { if (rightClickHeld) { Mouse.release(MOUSE_RIGHT); rightClickHeld = false; } } } void checkScroll() { static bool lastScrollUpState = HIGH; static bool lastScrollDownState = HIGH; int scrollUpState = digitalRead(scrollUpButton); int scrollDownState = digitalRead(scrollDownButton); // Check for scroll up button press if (scrollUpState == LOW && lastScrollUpState == HIGH) { Mouse.move(0, 0, 1); // Scroll up } lastScrollUpState = scrollUpState; // Check for scroll down button press if (scrollDownState == LOW && lastScrollDownState == HIGH) { Mouse.move(0, 0, -1); // Scroll down } lastScrollDownState = scrollDown } void checkShortcuts() { const int debounceDelay = 50; // Check each shortcut button and execute the corresponding key combination if (digitalRead(shortcutButton1) == LOW) { Keyboard.press(KEY_LEFT_ALT); Keyboard.press(KEY_LEFT_ARROW); Keyboard.releaseAll(); delay(debounceDelay); // Delay for debouncing } if (digitalRead(shortcutButton2) == LOW) { Keyboard.press(KEY_LEFT_ALT); Keyboard.press(KEY_RIGHT_ARROW); Keyboard.releaseAll(); delay(debounceDelay); // Delay for debouncing } if (digitalRead(shortcutButton3) == LOW) { Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_LEFT_ALT); Keyboard.press('Y'); Keyboard.releaseAll(); delay(debounceDelay); // Delay for debouncing } if (digitalRead(shortcutButton4) == LOW) { Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(KEY_LEFT_ALT); Keyboard.press('A'); Keyboard.releaseAll(); delay(debounceDelay); // Delay for debouncing } } void playSound(bool state) { // Play different tones based on the state if (state) { tone(buzzer, 1000); // Frequency for lock } else { tone(buzzer, 500); // Frequency for unlock } delay(100); // Duration of the sound noTone(buzzer); }