2

I want to use SendInput via C++ to lock the computer (Windows+L). I have created simple keyDown / keyUp functions in which I use SendInput to send a VK. On keyUp, it adds the flag 0x0002

I can simulate my tab key, my windows key and now I try to lock my computer with a simulated key stroke. I send the following messages:

key down:  0x5B (win key)
key down:  0x4C (L)
key up:    0x4C (L)
key up:    0x5B (win key)

My problem: Nothing happens :-(

Does someone know whats the solution?

Nathan
  • 1,135
  • 2
  • 12
  • 27
kpko
  • 131
  • 11
  • 1
    Um, why not simply call the [Win32 function designed specifically for locking a system](http://msdn.microsoft.com/en-us/library/windows/desktop/aa376875(v=vs.85).aspx)? – Michael Petrotta Jul 27 '12 at 22:08
  • I use this to control a specific application which is controllable via key strokes on a remote computer. I used SendInput for everything in this application, the only function left is to lock the screen after the use. I tried to do all of this via SendInput and I hoped that there is no need to add another mechanism. Though, if SendInput doesn't work, I'll use the Win32 call you mentioned. But I wonder why it doesn't work. – kpko Jul 27 '12 at 22:13
  • Ok. You might find [this related question](http://stackoverflow.com/questions/2906179/low-level-keyboard-hooks-sendinput-with-winkeyl-possible-workstation-lockout) helpful. – Michael Petrotta Jul 27 '12 at 22:17

1 Answers1

2

If I am not mistaken, you will not be able to do this with SendInput() (or keybd_event()) because it simply injects the keys into the keyboard input buffer, but special key sequences like CTRL+ALT+DEL, WIN+L, etc operate at a lower layer that are interpretted by the OS before keys are put in the input buffer.

The correct way to lock the computer is to use the LockWorkStation() function instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770