How to press the Windows button programmatically u

2019-02-17 01:24发布

Basically I want to simulate in code a user clicking on the windows button. I know there is SendKeys which allows me to send key presses to windows if I get a handle to them, but what I can't figure out is what I need to get a handle on in order to send Windows button commands. E.g. Windows Button + L. Having read into this a bit it appears that CTRL-ESC should pop up the Start Menu also but not sure how to tell it to send the keys to Windows (if this is even possible). Any help would be much appreciated.

Cheers!

3条回答
Rolldiameter
2楼-- · 2019-02-17 01:52

Some of the things that a user would do via a WinKey shortcut can be done programmatically in other ways. To take your WinKey+L example, you could instead just use the following statement:

Process.Start("rundll32.exe", "user32.dll,LockWorkStation");

If you could elaborate on what exactly you're trying to accomplish, maybe there's a better way than keybd_event (as Dale has suggested).

查看更多
别忘想泡老子
3楼-- · 2019-02-17 01:55

You need to use a global keyboard hook to hook into keyboards outside your application. There's an article on how to do it here.

查看更多
爷、活的狠高调
4楼-- · 2019-02-17 02:09

I don't think you can do this using SendKeys, you will need to p/invoke to an API function instead, probably keybd_event to send either CTRL+ESC or the Windows key.

Here is an example of opening the start menu this way in VB and here is keybd_event with its C# signature on pinvoke.net.

查看更多
登录 后发表回答