Digistump Forums
The Digispark => Digispark Libraries => Topic started by: gmarion on January 17, 2018, 02:45:19 pm
-
Hi!
I'm trying to build a usb button that will be used as a shortcut for a Alt+Numpad key combo on windows. I'm using a digispark and a push button. When the button is pressed, I would like to send to the computer the combo ALT+0183 using the digikeyboard librairy. The problem is that I can't figure how to hold ALT key pressed while sending the keystrokes for the numbers.
Is there anybody here that could have an idea to help me?
Regards
-
Using DigiKeyboard.sendKeyStroke()? It accepts two parameters, the keystroke and then the modifier. e.g. Alt+Arrow Down
DigiKeyboard.sendKeyStroke(KEY_ARROW_DOWN, MOD_ALT_LEFT);
The full list of all the keys that can be called that way is in the DigiKeyboard.h file (https://github.com/digistump/DigisparkArduinoIntegration/blob/master/libraries/DigisparkKeyboard/DigiKeyboard.h)...
Only thing I'm not sure if there is how you get it to hold the alt key down whilst entering the four digits... The other thing is you may need to send the correct keystrokes for the number pad keys? so 0 = 98, 1=89, 8=96 & 3=91 (from the USB.org HID Usage table - chapter 10 keyboard (http://www.usb.org/developers/hidpage/Hut1_12v2.pdf))
-
I found by myself how to do that :
First, I needed to define the following variable to send the numpad keys and not the ones which are above the QWERTY keys :
#define KEYPAD_1 ( 89 | 0xF000 )
#define KEYPAD_2 ( 90 | 0xF000 )
#define KEYPAD_3 ( 91 | 0xF000 )
#define KEYPAD_4 ( 92 | 0xF000 )
#define KEYPAD_5 ( 93 | 0xF000 )
#define KEYPAD_6 ( 94 | 0xF000 )
#define KEYPAD_7 ( 95 | 0xF000 )
#define KEYPAD_8 ( 96 | 0xF000 )
#define KEYPAD_9 ( 97 | 0xF000 )
#define KEYPAD_0 ( 98 | 0xF000 )
Then I used this code :
DigiKeyboard.update();
DigiKeyboard.sendKeyPress(KEYPAD_0,MOD_ALT_LEFT);
DigiKeyboard.sendKeyPress(KEYPAD_1,MOD_ALT_LEFT);
DigiKeyboard.sendKeyPress(KEYPAD_8,MOD_ALT_LEFT);
DigiKeyboard.sendKeyPress(KEYPAD_3,MOD_ALT_LEFT);
DigiKeyboard.sendKeyPress(0,0);
DigiKeyboard.delay(100);
The "trick" was to use "sendKeyPress()" instead of "sendKeyStroke()" :
- sendKeyPress(keyid,mod) : press the keys "keyid" with the modifier "mod" but release none of them
- sendKeyStroke(keyid,mod) : press the keys "keyid" with the modifier "mod" and release all of them
BUT : in order to keep ALT pressed during the whole operation, it is important to use it as a modifier in each line
At the end, you can use sendKeyPress(0,0) to release all the keys.
I hope that it will help some people in the future :)
G
-
Too bad this does not work with repeating ALT numbers:
DigiKeyboard.sendKeyPress(KEYPAD_4,MOD_ALT_LEFT);
DigiKeyboard.sendKeyPress(KEYPAD_4,MOD_ALT_LEFT);
DigiKeyboard.sendKeyPress(0); // Release all keys
The above will not work because the keypad numbers are also not released between "presses" …
A workaround is this:
DigiKeyboard.sendKeyPress(KEYPAD_4,MOD_ALT_LEFT);
DigiKeyboard.sendKeyPress(0,MOD_ALT_LEFT); // Release all keys except "ALT"
DigiKeyboard.sendKeyPress(KEYPAD_4,MOD_ALT_LEFT);
DigiKeyboard.sendKeyPress(0); // Release all
Regards,
Photubias