Author Topic: Screen Locker  (Read 9846 times)

kwyjib0

  • Newbie
  • *
  • Posts: 4
Screen Locker
« on: January 23, 2013, 01:58:06 pm »
Using digispark and a distance measuring sensor (HC-SR04) I'd like to lock my Windows Laptop when I walk away from it.


So all ok while I'm sitting 1cm to 150cm away from the screen but as soon as the HC-SR04 notices that there's nobody sitting in front of the laptop digispark should send WIN Key + l to lock the screen.


According to http://www.usb.org/developers/devclass_docs/Hut1_11.pdf the left win key should be 0xE3 so I'm sending



DigiKeyboard.sendKeyStroke(0xE3, KEY_L);


but nothing is happening.


Also tried it with the right win key (0xE7) but no luck either.


Any ideas?

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: Screen Locker
« Reply #1 on: January 24, 2013, 07:24:03 pm »
Aren't you supposed to put the modifier as the second argument?
So DigiKeyboard.sendKeyStroke(KEY_L, 0xE3);

Also from the keyboard example, did you add this before?
  DigiKeyboard.update();
  DigiKeyboard.sendKeyStroke(0); //this is generally not necessary but with some older systems it seems to prevent missing the first character after a delay


SmeeAgain

  • Newbie
  • *
  • Posts: 10
Re: Screen Locker
« Reply #2 on: January 24, 2013, 10:01:03 pm »
Yep, first the key, then the modifier. Also, Win Key is MOD_GUI_LEFT which is (1<<3) as defined in DigiKeyboard.h
Try the following code, it's working for me to lock every 30 seconds:
Code: [Select]
void loop() {
 
  DigiKeyboard.update();
  DigiKeyboard.sendKeyStroke(0);
  DigiKeyboard.sendKeyStroke(KEY_L, MOD_GUI_LEFT);
  delay(30000);
}
T

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: Screen Locker
« Reply #3 on: January 25, 2013, 05:06:07 am »
I wonder why the modifiers don't match the key strokes defined in the hid doc.

kwyjib0

  • Newbie
  • *
  • Posts: 4
Re: Screen Locker
« Reply #4 on: January 25, 2013, 02:32:34 pm »
semicolo, SmeeAgain


thanks for your help, it's working now as expected :)

SmeeAgain

  • Newbie
  • *
  • Posts: 10
Re: Screen Locker
« Reply #5 on: January 25, 2013, 03:05:38 pm »
Cool, glad things are working :)
 
As for the keys being different in the spec. I would guess that the spec calls out the actual key values, i.e. you could call DigiKeyboard.sendKeyStroke(0xE3); and it would send a single windows key press that opens your windows start menu. I haven't tested this though, so don't quote me on it.
The modifiers on the other hand need to be individual bits, so you can combine them. If you look at the code (I added the binary values as comments):
 
Code: [Select]
#define MOD_CONTROL_LEFT    (1<<0)   //00000001
 #define MOD_SHIFT_LEFT      (1<<1)  //00000010
 #define MOD_ALT_LEFT        (1<<2)  //00000100
 #define MOD_GUI_LEFT        (1<<3)  //00001000
 #define MOD_CONTROL_RIGHT   (1<<4)  //00010000
 #define MOD_SHIFT_RIGHT     (1<<5)  //00100000
 #define MOD_ALT_RIGHT       (1<<6)  //01000000
 #define MOD_GUI_RIGHT       (1<<7)  //10000000

So for example if you want to send Ctrl and Alt together, you can send MOD_CONTROL_LEFT | MOD_ALT_LEFT, which will send the Modifier key 0x05, or in binary 00000101. By making each modifier a separate bit you can combine them as you like. I couldn't find anything about sending modifier keys in the spec, so it might up to each device how they implement it.
Hope that makes sense,
T

semicolo

  • Full Member
  • ***
  • Posts: 137
Re: Screen Locker
« Reply #6 on: January 26, 2013, 10:03:10 am »
To continue off topic, on a real keyboard isn't it down/up events that are sent?
So when you press the windows key, the computer receives a key down event, then a key down when L is pressed and then key up events when the keys are released (at least on ps2 keyboards, not sure about usb).
Isn't it limiting to only be able to send keystrokes?