Question for MobaTools.h

so thanks to members on here i completed the code for my 5 key keyboard.

i have couple extra keys left and i was woindering if its possible to setup three "profile" keys that would change the set button commands.

will this be possible with mobatools.h?

#include "Keyboard.h"
#define MAX8BUTTONS   // only to save storage space if not using more than 8 buttons
#include <MobaTools.h>

const byte buttonPin[] = { 2, 3, 4, 5, 6 };
const byte buttonCount = sizeof( buttonPin ); // buttonPin must be declared as byte to work correctly
MoToButtons buttons( buttonPin, buttonCount, 20, 800 ); // 20ms debounce time, 500ms longpress ( not used here )

void setup() {
  Keyboard.begin();
}

void loop() {
  buttons.processButtons();
  
  if (buttons.longPress(0) ) {  // sleep pc
    // first button ( pin 2 ) pressed
    Keyboard.press(KEY_LEFT_CTRL); 
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('k');
    delay(100);
    Keyboard.releaseAll(); 
  }

  if (buttons.pressed(0) ) {   // kill internet
    // second button ( pin 2 ) pressed
    Keyboard.press(KEY_LEFT_CTRL); 
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('m');
    delay(100);
    Keyboard.releaseAll(); 
  }

  if (buttons.longPress(1) ) {   // show monitor profile dropdown
    // second button ( pin 3 ) pressed
    Keyboard.press(KEY_LEFT_CTRL); 
    Keyboard.press(KEY_LEFT_SHIFT);
    Keyboard.press('F4');
    delay(100);
    Keyboard.releaseAll(); 

  }

  if (buttons.pressed(1) ) {   // toggle monitor profile three and one
    // second button ( pin 3 ) pressed
    Keyboard.press(KEY_LEFT_CTRL); 
 Keyboard.press(KEY_LEFT_ALT); 
      Keyboard.press('y');
    delay(100);
    Keyboard.releaseAll(); 
  }

  if (buttons.longPress(2) ) {   // turn on led
    // third button ( pin 4 ) pressed
    Keyboard.press(KEY_LEFT_CTRL); 
       Keyboard.press('F5');
    delay(100);
    Keyboard.releaseAll(); 
  }

 if (buttons.pressed(2) ) {   // change LED Profile
    // third button ( pin 4 ) pressed
    Keyboard.press(KEY_LEFT_CTRL); 
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('p');
    delay(100);
    Keyboard.releaseAll(); 
  }
  if (buttons.longPress(3) ) {  // record last 5min
    // 4th button ( pin 5 ) pressed
    Keyboard.press(KEY_LEFT_CTRL); 
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('F9');
    delay(100);
    Keyboard.releaseAll(); 
  }
  if (buttons.pressed(3) ) {  // record on/off
    // 4th button ( pin 5 ) pressed
    Keyboard.press(KEY_LEFT_CTRL); 
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('F10');
    delay(100);
    Keyboard.releaseAll(); 

  if (buttons.longPress(4) ) {  //
    // 5th button ( pin 6 ) pressed
    Keyboard.press(KEY_LEFT_CTRL); 
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('d');
    delay(100);
    Keyboard.releaseAll(); 
  }
  if (buttons.pressed(4) ) {
    // 5th button ( pin 6 ) pressed
    Keyboard.press(KEY_LEFT_CTRL); 
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press('d');
    delay(100);
    Keyboard.releaseAll(); 
  }
}

I'm not quite shure if I understand correctly what you want to do. How will you change the set button commands? If an additional key is pressed simultaneously - like a shift key?

I am also a little bit confused that you use pressed() and longPress() together. Does this work for you as expected? Normaly you should use shortPress() together with longPress() to distinguish between a long and a short press of the button.
pressed() is always fired at the moment you press the button - no matter how long you hold it down.

sorry let me explain better.

with the same 5 keys i want to be able to toggle change their function as if toggling hotkey profiles.

i want to use separate 3 keyboard buttons to select the different profiles.

i tested the code when i had only 1 key setup (both short and long press and it worked. unless i got confused before finnishing it and messed it up.

il test again.

Than you could use a variable that holds the active profile, and change this variable with these separate buttons. When one of your 5 keys is pressed, you select the profile according to the value of that variable.

N.B. It would greatly improve the programme overview if you gave the buttons names and not just use the numbers.

thanks for pointing me to the right direction.

and you were right about shortpress. i fixed it now and its working

:+1:t3:

still trying to figure out the the correct code for adding the variable and how select the variable when button is pressed..

just need to find a example code, much easier for me to understand the code that way.

Please explain exactly what you want your additional buttons to do. Is the 'profile' common to all set buttons and how many different profiles do you need?
Or is there a different profile for each set button which you want to change independently from each other?
Without an detailed descrition what you want to do, no detailed help is possible.

each profile will be for all set buttons (5 button). i want three profiles, that changes the "Keyboard.press" of each 5 button.

to select the profiles i want to nominate three buttons one for each profile.

my apologies if its still confusing as english isn't my first language and coding term just escalates the difficulties.

no need to bother if i'm still not explaining my self correct.
thanks anyways.

I think I got it now :wink: . So you need one variable, that holds the aktive profile ( three values ). Every press of the profile-buttons changes the value accordingly.
For the set buttons a switch statement would be appropriate to select the correct profile

yes sounds about right.

I managed to get help from a reddit user i contacted for help and he came up with this code i can modify for my own.
im very happy right now because i can study the code and learn from it much more affectively than trying to put the code together from scratch.

/* This is not tested code, debug it thoroughly.
 * ---
 * If you try to run this code without modyfying it, a bunch of errors will pop up!
 * It's alright, I added these to remind you that you need to fill some code sections.
 * If you want to run a single mode without finishing the rest, just delete these error statements (#error)
 * ---
 * I added constants for each button pin, button index, and modes of operation.
 * Additionally, I created the skeleton for the mode-choosing buttons, 
 * but the buttons weren't added, it's your task!
 * --- 
 * Read the comments carefully! Good luck!
 */

#include "Keyboard.h"
#define MAX8BUTTONS   // only to save storage space if not using more than 8 buttons
#include <MobaTools.h>

/* -------------------------------------------------------------------------------------- */

/* These are the modes of the buttons, you can change 
 * them to have more indicative names (expl: MINIMALIST_MODE ..)
 */
enum{MODE_1 = 0, MODE_2, MODE_3}; // Create three constants having the values 0, 1, 2

/* Buttons' indexes are better when put into constants, 
 * also it's better to give buttons meaningful names like
 * KILL_SWITCH, ESCAPE, CHANGE_MODE, ...
 */
enum{BUTTON_0 = 0, BUTTON_1, BUTTON_2, BUTTON_3, BUTTON_4};

/* Buttons' pins are better when put into constants.
 * enum counts up from the first value, if you want to define the values yourself,
 * you can make :: expl: BUTTON_3_PIN = 13
 */
enum{BUTTON_0_PIN = 2, BUTTON_1_PIN, BUTTON_2_PIN, BUTTON_3_PIN, BUTTON_4_PIN};

/* This is the varibale that would hold the mode (and you'll update it 
 * according to which button of the remaining 3 is pressed.
 */
byte which_mode = MODE_1; // Default mode is MODE_1

/* -------------------------------------------------------------------------------------- */

/* Buttons variables */
const byte buttonPin[] = {BUTTON_0_PIN, BUTTON_1_PIN, BUTTON_2_PIN, BUTTON_3_PIN, BUTTON_4_PIN};
const byte buttonCount = sizeof( buttonPin ); // buttonPin must be declared as byte to work correctly
MoToButtons buttons( buttonPin, buttonCount, 20, 800 ); // 20ms debounce time, 500ms longpress ( not used here )

void setup() {
  Keyboard.begin();
}

void loop() {
  buttons.processButtons();

  /* -------------------------------------------------------------------------------------- */
  
  /* Here you choose the mode and update the value of which_mode according to the pressed button */

  /*

    THIS IS PSEUDO CODE, THE BUTTONS HAVEN'T BEEN ADDED .. It's your task !
  
    if(buttons.shortPress(MODE_1_BUTTON){ // Or maybe longPress()
      which_mode = MODE_1;
    }
  
    if(buttons.shortPress(MODE_2_BUTTON){ // Or maybe longPress()
      which_mode = MODE_2;
    }
  
    if(buttons.shortPress(MODE_3_BUTTON){ // Or maybe longPress()
      which_mode = MODE_3;
    }
  */
  
  /* -------------------------------------------------------------------------------------- */
  
  if (buttons.longPress(BUTTON_0) ) {
    
    /* Depending on the value of which_mode, we either execute the code under MODE_1, MODE_2 or MODE_3 */
    switch(which_mode){ 
      
      case MODE_1: /* Default mode */
        // sleep pc ( pin 2 ) 
        Keyboard.press(KEY_LEFT_CTRL); 
        Keyboard.press(KEY_LEFT_ALT);
        Keyboard.press('k');
      break;

      case MODE_2: /* Second mode (you fill it) */
        
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
      
      break;

      case MODE_3: /* Third mode (you fill it) */
      
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
        
      break;
    }

    /* Since no matter what presses are performed these are mandatory, we keep them out of the switch block */
    delay(100);
    Keyboard.releaseAll(); 
  }

  if (buttons.shortPress(BUTTON_0) ) {

    /* Depending on the value of which_mode, we either execute the code under MODE_1, MODE_2 or MODE_3 */
    switch(which_mode){ 
      case MODE_1: /* Default mode */
        // kill internet ( pin 2 ) 
        Keyboard.press(KEY_LEFT_CTRL); 
        Keyboard.press(KEY_LEFT_ALT);
        Keyboard.press('m');
      break;

      case MODE_2: /* Second mode (you fill it) */
        
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
      
      break;

      case MODE_3: /* Third mode (you fill it) */
      
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
        
      break;
    }
    
    /* Since no matter what presses are performed these are mandatory, we keep them out of the switch block */
    delay(100);
    Keyboard.releaseAll(); 
  }

  if (buttons.longPress(BUTTON_1) ) {  

    /* Depending on the value of which_mode, we either execute the code under MODE_1, MODE_2 or MODE_3 */
    switch(which_mode){ 
      case MODE_1: /* Default mode */
        // show monitor profile dropdown( pin 3 ) 
        Keyboard.press(KEY_LEFT_CTRL); 
        Keyboard.press(KEY_LEFT_SHIFT);
        Keyboard.press(KEY_F4);
      break;

      case MODE_2: /* Second mode (you fill it) */
        
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
      
      break;

      case MODE_3: /* Third mode (you fill it) */
      
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
        
      break;
    }

    /* Since no matter what presses are performed these are mandatory, we keep them out of the switch block */
    delay(100);
    Keyboard.releaseAll(); 

  }

  if (buttons.shortPress(BUTTON_1) ) {   

    /* Depending on the value of which_mode, we either execute the code under MODE_1, MODE_2 or MODE_3 */
    switch(which_mode){ 
      case MODE_1: /* Default mode */
        // toggle monitor profile three and one ( pin 3 ) 
        Keyboard.press(KEY_LEFT_CTRL); 
        Keyboard.press(KEY_LEFT_ALT); 
        Keyboard.press('y');
      break;

      case MODE_2: /* Second mode (you fill it) */
        
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
      
      break;

      case MODE_3: /* Third mode (you fill it) */
      
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
        
      break;
    }
    
    /* Since no matter what presses are performed these are mandatory, we keep them out of the switch block */
    delay(100);
    Keyboard.releaseAll(); 
  }

  if (buttons.longPress(BUTTON_2) ) {   

    /* Depending on the value of which_mode, we either execute the code under MODE_1, MODE_2 or MODE_3 */
    switch(which_mode){ 
      case MODE_1: /* Default mode */
        // turn on led( pin 4 ) 
        Keyboard.press(KEY_LEFT_CTRL); 
        Keyboard.press(KEY_F5);
      break;

      case MODE_2: /* Second mode (you fill it) */
        
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
      
      break;

      case MODE_3: /* Third mode (you fill it) */
      
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
        
      break;
    }
    
    /* Since no matter what presses are performed these are mandatory, we keep them out of the switch block */
    delay(100);
    Keyboard.releaseAll(); 
  }

 if (buttons.shortPress(BUTTON_2) ) {   
    /* Depending on the value of which_mode, we either execute the code under MODE_1, MODE_2 or MODE_3 */
    switch(which_mode){ 
      case MODE_1: /* Default mode */
        // change LED Profile ( pin 4 ) 
        Keyboard.press(KEY_LEFT_CTRL); 
        Keyboard.press(KEY_LEFT_ALT);
        Keyboard.press('p');
      break;

      case MODE_2: /* Second mode (you fill it) */
        
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
      
      break;

      case MODE_3: /* Third mode (you fill it) */
      
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
        
      break;
    }
    
    /* Since no matter what presses are performed these are mandatory, we keep them out of the switch block */
    delay(100);
    Keyboard.releaseAll(); 
  }
  
  if (buttons.longPress(BUTTON_3) ) {  

    /* Depending on the value of which_mode, we either execute the code under MODE_1, MODE_2 or MODE_3 */
    switch(which_mode){ 
      case MODE_1: /* Default mode */
        // record last 5min ( pin 5 ) 
        Keyboard.press(KEY_LEFT_CTRL); 
        Keyboard.press(KEY_LEFT_ALT);
        Keyboard.press(KEY_F9);
      break;

      case MODE_2: /* Second mode (you fill it) */
        
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
      
      break;

      case MODE_3: /* Third mode (you fill it) */
      
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
        
      break;
    }
    
    /* Since no matter what presses are performed these are mandatory, we keep them out of the switch block */
    delay(100);
    Keyboard.releaseAll(); 
  }
  
  if (buttons.shortPress(BUTTON_3) ) {  

    /* Depending on the value of which_mode, we either execute the code under MODE_1, MODE_2 or MODE_3 */
    switch(which_mode){ 
      case MODE_1: /* Default mode */
        // record on/off( pin 5 ) 
        Keyboard.press(KEY_LEFT_CTRL); 
        Keyboard.press(KEY_LEFT_ALT);
        Keyboard.press(KEY_F10);
      break;

      case MODE_2: /* Second mode (you fill it) */
        
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
      
      break;

      case MODE_3: /* Third mode (you fill it) */
      
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
        
      break;
    }
    
    /* Since no matter what presses are performed these are mandatory, we keep them out of the switch block */
    delay(100);
    Keyboard.releaseAll(); 
  } // <-- I added this after removing the one down
  
  if (buttons.longPress(BUTTON_4) ) {  

    /* Depending on the value of which_mode, we either execute the code under MODE_1, MODE_2 or MODE_3 */
    switch(which_mode){ 
      case MODE_1: /* Default mode */
        // 5th button ( pin 6 ) pressed
        Keyboard.press(KEY_LEFT_CTRL); 
        Keyboard.press(KEY_LEFT_ALT);
        Keyboard.press('d');
      break;

      case MODE_2: /* Second mode (you fill it) */
        
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
      
      break;

      case MODE_3: /* Third mode (you fill it) */
      
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
        
      break;
    }
    
    /* Since no matter what presses are performed these are mandatory, we keep them out of the switch block */
    delay(100);
    Keyboard.releaseAll(); 
  }
  
  if (buttons.shortPress(BUTTON_4) ) {

    /* Depending on the value of which_mode, we either execute the code under MODE_1, MODE_2 or MODE_3 */
    switch(which_mode){ 
      case MODE_1: /* Default mode */
        // 5th button ( pin 6 ) pressed
        Keyboard.press(KEY_LEFT_CTRL); 
        Keyboard.press(KEY_LEFT_ALT);
        Keyboard.press('d');
      break;

      case MODE_2: /* Second mode (you fill it) */
        
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
      
      break;

      case MODE_3: /* Third mode (you fill it) */
      
        /* Added these error messages so that the code would only run 
         * when you remove all of these and replace them with code 
         */
        #error "You still didn't finish this"
        
      break;
    }
    
    /* Since no matter what presses are performed these are mandatory, we keep them out of the switch block */
    delay(100);
    Keyboard.releaseAll(); 
  }
  //}  <-- I commented this and moved it to close the button 3 _ if statement
}


thank you for your time.

Yes, that looks like a good base for your enhancements.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.