Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
923 views
in Technique[技术] by (71.8m points)

windows - Simulate windowskey+L in visual basic?

I want to simulate WindowsKey + L (The short cut to lock the console) in visual basic and bind it to a function. So when this function is called it will lock the console. Can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Simulating the hotkey is the wrong approach. All you need to do is call the LockWorkStation function. This has the same result as pressing Ctrl+Alt+Del and selecting "Lock Workstation", or using the Win+L hotkey, except that you can do it programmatically through code.

To call this function from a VB application, you'll need to write a declaration, like so:

Private Declare Function LockWorkStation Lib "user32.dll" () As Long

You'll want to place that declaration at the top of your module file, before any procedures are defined. Then, inside one of the procedures, you can call the function. For example:

Private Sub LockComputer()
    LockWorkStation
End Sub

Even better code would check the return value of LockWorkStation for an error code. A return value of 0 indicates an error. The standard way of checking for Win32 errors in VB, Err.LastDllError, will give you more information about what exactly went wrong.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...