I've made an extension to the SendKeyStroke method of DigiKeyboard.h by adding a key down time parameter.
SendKeyStroke method now has a third parameter to allow the simulation of pressing time (in milliseconds) that the key remains pressed until is released.
void sendKeyStroke(byte keyStroke, byte modifiers, uint16_t key_down_time);
If 'key_down_time' is zero then don't release the key, it should be done manually by sendKeysStroke(0). As 'zero' means forever, the minimum press time time is 1ms and the maximum is 65535ms.
Examples:
DigiKeyboard.sendKeyStroke(KEY_B); // Send a B to host
DigiKeyboard.sendKeyStroke(KEY_A,0,300); // Send an A to host, takes 300ms until release
DigiKeyboard.sendKeyStroke(KEY_C,0,0); //Send a C to host and keep it pushed
DigiKeyboard.delay(2000); // wait 2 seconds
DigiKeyboard.sendKeyStroke(0); // now release any key pressed.
Tried to send a pull request to original project but without success, though. Either way the extended function is below:
void sendKeyStroke(byte keyStroke, byte modifiers, uint16_t key_down_time) {
while (!usbInterruptIsReady()) {
// Note: We wait until we can send keystroke
// so we know the previous keystroke was
// sent.
usbPoll();
_delay_ms(5);
}
memset(reportBuffer, 0, sizeof(reportBuffer));
reportBuffer[0] = modifiers;
reportBuffer[1] = keyStroke;
usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
while (!usbInterruptIsReady()) {
// Note: We wait until we can send keystroke
// so we know the previous keystroke was
// sent.
usbPoll();
_delay_ms(5);
}
// Pressing time (in milliseconds) that the key remains pressed
// until is released. If is zero don't release the key (it should be
// done manually by sendKeysStroke(0). Therefore minimum time is 1ms
// and the maximum is 65535ms.
if (key_down_time) {
while (key_down_time--) {
usbPoll();
_delay_ms(1);
}
// Now send 'no key' pressed
memset(reportBuffer, 0, sizeof(reportBuffer));
usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
}
}
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
Thank you so much for this library and function. It made my fix for keyboard possible. Original library doesn't have all of keys and possiblity of holding down key. I don't have sufficient knowledge yet to programm them myself. You really saved me. Great work men!
Are you sure? yes | no
Doing my best to use this for a Wheel of Fortune game. I have to hold down the Space Bar which spins the wheel. The Digikeyboard method doesn't function properly for this and it's frustrating. Can you help me implement this code? All I need is the Space Bar to be held down for as long as a button is pressed. TIA.
Are you sure? yes | no
You can check my #F.P.S: Foot Presto Switch project which does exactly like that, keeping a key pressed as long as a switch is held on.
Are you sure? yes | no