Author Topic: DigiKeyboard.sendkey() Confusion  (Read 3251 times)

TotallyNotKsAlt

  • Newbie
  • *
  • Posts: 2
DigiKeyboard.sendkey() Confusion
« on: May 03, 2017, 02:32:46 pm »
I've been trying to get a simple keyboard prank working on my Digispark. I use the DigiKeyboard.h library to set it up, and it works for the most part.

Using the DigiKeyboard.sendkey function has me a bit confused. Can I only use one key at once? I am trying to use three at a time, and it won't work. I've tried it multiple ways, due to what other people have said. I can't find much on this and have been looking all over.

(MOD_ALT_LEFT, MOD_CONTROL_LEFT, KEY_ARROW_UP) [I modified the library to use the arrow keys] Won't work
(KEY_ARROW_UP, MOD_ALT_LEFT, MOD_CONTROL_LEFT) Somebody recommended that I put the key first. Won't work this way either.
(KEY_ARROW_UP | MOD_ALT_LEFT, MOD_CONTROL_LEFT) Saw a post claiming you have to put this operator between the mod and keys. Didn't work either.
(KEY_ARROW_UP, MOD_ALT_LEFT | MOD_CONTROL_LEFT) Did this as a combination of the last two. Didn't work either.

What am I doing wrong? I am just trying to rotate the screen (using a hotkey). Below is the full code:

_______________________________________________________________________________________________
#include "DigiKeyboard.h"

int loopn = 0;

void setup() {
  // don't need to set anything up to use DigiKeyboard
}


void loop() {
  if(loopn++ >= 5) {
    // this is generally not necessary but with some older systems it seems to
 
    DigiKeyboard.sendKeyStroke(0);

    DigiKeyboard.sendKeyStroke(KEY_ARROW_DOWN | MOD_CONTROL_LEFT, MOD_GUI_LEFT);
      DigiKeyboard.delay(1500);
    DigiKeyboard.sendKeyStroke(KEY_ARROW_LEFT | MOD_CONTROL_LEFT, MOD_GUI_LEFT);
      DigiKeyboard.delay(1500);
    DigiKeyboard.sendKeyStroke(KEY_ARROW_RIGHT | MOD_CONTROL_LEFT, MOD_GUI_LEFT);
      DigiKeyboard.delay(1500);
    DigiKeyboard.sendKeyStroke(KEY_ARROW_UP | MOD_CONTROL_LEFT, MOD_GUI_LEFT);
      DigiKeyboard.delay(1500);
    }
}
___________________________________________________________________________________________

Thanks

Ralf

  • Guest
Re: DigiKeyboard.sendkey() Confusion
« Reply #1 on: May 05, 2017, 05:45:09 am »
It's two years ago I played wirth the keyboard libary. If I remember correct, it works like this:

The DigiKeyboard library sends two bytes for every key. The first is the value, the second is the modifier.
The modifier byte is a combination of bits, each bit is a different modifier. You may see it in Digikeyboard.h:

Code: [Select]
#define MOD_CONTROL_LEFT    (1<<0)
#define MOD_SHIFT_LEFT      (1<<1)
#define MOD_ALT_LEFT        (1<<2)
#define MOD_GUI_LEFT        (1<<3)
#define MOD_CONTROL_RIGHT   (1<<4)
#define MOD_SHIFT_RIGHT     (1<<5)
#define MOD_ALT_RIGHT       (1<<6)
#define MOD_GUI_RIGHT       (1<<7)

So the correct version should be:
Code: [Select]
DigiKeyboard.sendKeyStroke(KEY_ARROW_DOWN, MOD_CONTROL_LEFT | MOD_GUI_LEFT);

KEY_ARROW_DOWN
 is the first byte, which has to be separated by a comma from the second byte, and

MOD_CONTROL_LEFT | MOD_GUI_LEFT
means that two bits are combined into the modifier byte.

Good luck,
Ralf

TotallyNotKsAlt

  • Newbie
  • *
  • Posts: 2
Re: DigiKeyboard.sendkey() Confusion
« Reply #2 on: May 05, 2017, 05:47:01 pm »
Yup, this fixed it! Here is the source code, so others can see it:

#include "DigiKeyboard.h"

int loopn = 0;

void setup() {
    DigiKeyboard.sendKeyStroke(0);

    DigiKeyboard.sendKeyStroke(KEY_S, MOD_GUI_LEFT);
    DigiKeyboard.delay(2000);
    DigiKeyboard.println("notepad");
    DigiKeyboard.delay(1000);
    DigiKeyboard.sendKeyStroke(KEY_ENTER);
    DigiKeyboard.delay(1500);
    DigiKeyboard.println("Hello there, today we'll be flipping your screen!");

}


void loop() {
    DigiKeyboard.sendKeyStroke(0);

    DigiKeyboard.sendKeyStroke(KEY_ARROW_DOWN, MOD_CONTROL_LEFT | MOD_ALT_LEFT);
    DigiKeyboard.delay(1500);
    DigiKeyboard.sendKeyStroke(KEY_ARROW_LEFT, MOD_CONTROL_LEFT | MOD_ALT_LEFT);
    DigiKeyboard.delay(1500);
    DigiKeyboard.sendKeyStroke(KEY_ARROW_RIGHT, MOD_CONTROL_LEFT | MOD_ALT_LEFT);
    DigiKeyboard.delay(1500);
    DigiKeyboard.sendKeyStroke(KEY_ARROW_UP, MOD_CONTROL_LEFT | MOD_ALT_LEFT);
    DigiKeyboard.delay(1500);
}