Hello, everyone!
I have an Arduino Leonardo that interfaces with a rotary encoder that when pressed changes modes. Is there a way for the Arduino to know what window it is in and execute a given command based on that?
This is my code:
#include <HID-Project.h>
// Rotary Encoder Inputs
#define inputCLK 1
#define inputDT 2
#define inputSW 3
int currentStateCLK;
int previousStateCLK;
int mode = 1;
int maxModes = 3;
void setup() {
// Set encoder pins as inputs
pinMode (inputCLK,INPUT);
pinMode (inputDT,INPUT);
pinMode(inputSW, INPUT_PULLUP);
// Setup Serial Monitor
Serial.begin (9600);
Consumer.begin();
Keyboard.begin();
// Read the initial state of inputCLK
// Assign to previousStateCLK variable
previousStateCLK = digitalRead(inputCLK);
}
void loop() {
// Read the current state of inputCLK
currentStateCLK = digitalRead(inputCLK);
// If the previous and the current state of the inputCLK are different then a pulse has occured
if (currentStateCLK != previousStateCLK){
// If the inputDT state is different than the inputCLK state then
// the encoder is rotating counterclockwise
if (digitalRead(inputDT) != currentStateCLK) {
rotateRight();
} else {
rotateLeft();
}
}
// Update previousStateCLK with the current state
previousStateCLK = currentStateCLK;
if(!digitalRead(inputSW)){
changeMode();
delay(300);
}
}
void changeMode(){
mode = (mode % maxModes)+ 1;
}
void rotateRight() {
switch(mode){
case 1:
//Ctrl + Tab
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_TAB);
Keyboard.releaseAll();
break;
case 2:
//Desplazarse entre pestañas de IntelliJ/Webstorm
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_RIGHT_ARROW);
Keyboard.releaseAll();
break;
case 3:
//Desplazarse en YT
Keyboard.write(KEY_RIGHT_ARROW);
break;
}
}
void rotateLeft() {
switch(mode){
case 1:
//Ctrl + SHIFT + Tab
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(KEY_LEFT_SHIFT);
Keyboard.press(KEY_TAB);
Keyboard.releaseAll();
break;
case 2:
//Desplazarse entre pestañas de IntelliJ/Webstorm
Keyboard.press(KEY_LEFT_ALT);
Keyboard.press(KEY_LEFT_ARROW);
Keyboard.releaseAll();
break;
case 3:
//Desplazarse en YT
Keyboard.write(KEY_LEFT_ARROW);
break;
}
}