Emulate sleep Key on Arduino mico ATmega32u4

Hi,

i’d like to build an energy saving module which send the computer (Windows) into energy saving mode, when an IR sensor doesn’t detect movement or a user body.

I wired up the arduino with a simple push button to test the keyboard output.
I used the keyboard.h library and this code to send ctrl+alt+del to the computer.

#include <Keyboard.h>

int previousButtonState = HIGH;   // for checking the state of a pushButton
char strgKey = KEY_LEFT_CTRL;
char altKey = KEY_LEFT_ALT;
char delKey = KEY_DELETE;

void setup() {
  // make the pushButton pin an input:
  pinMode(4, INPUT_PULLUP);
  // initialize control over the keyboard:
  Keyboard.begin();
}

void loop() {
   // read the pushbutton:
  int buttonState = digitalRead(4);
  // if the button state has changed,
  if ((buttonState != previousButtonState)
      // and it's currently pressed:
      && (buttonState == HIGH)) {
     
    delay(500);
    Keyboard.press(sleep);
    Keyboard.press(altKey);
    Keyboard.press(delKEY);
    delay(1000);
    Keyboard.releaseAll();
    delay(1000);
  }
  
  previousButtonState = buttonState;

}

Until here everything is fine.
But I want to send a ‘Sleep Key’ to the computer like it’s implemented on some Keyboards.

To do this a tried to edit the keyboard.h but i don’t know the hex value of this key.

#ifndef KEYBOARD_h
#define KEYBOARD_h

#include "HID.h"

#if !defined(_USING_HID)

#warning "Using legacy HID core (non pluggable)"

#else
#define KEY_LEFT_CTRL   0x80
#define KEY_LEFT_SHIFT    0x81
#define KEY_LEFT_ALT    0x82
#define KEY_LEFT_GUI    0x83
#define KEY_RIGHT_CTRL    0x84
#define KEY_RIGHT_SHIFT   0x85
#define KEY_RIGHT_ALT   0x86
#define KEY_RIGHT_GUI   0x87
#define KEY_SLEEP   ????
...

Does anyone know the value of the key or do I have to go another way?

The keyword is "scan code"... https://www.google.com/search?q=usb+scan+codes

Bear in mind there is some filtering in the Keyboard Library that may make handling certain keys difficult or even impossible. You may have to modify the library to get what you want.

The Keyboard library implements the Keyboard/Keypad Page (0x07) of the Universal Serial Bus HID Usage Tables. "System Sleep" is Usage ID 0x82 of the Generic Desktop Page (0x01). I guess you will need to create a library that can send "Page 0x01" reports instead of "Page 0x07" reports. See: http://www.usb.org/developers/hidpage/Hut1_12v2.pdf

Hi,

if someone is interestet in a soloution for my problem, I found one, which was much easier than i thought. I switched from Arduino Micro to the Arduino compatible Teensy 3.2 board. This board understands the Key I needed (KEY_SYSTEM_SLEEP) natively without adding any libraries.

Thanks for your answer posts.