Simulate windowskey+L in visual basic?

前端 未结 1 1435
[愿得一人]
[愿得一人] 2020-12-18 10:36

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. Ca

相关标签:
1条回答
  • 2020-12-18 11:00

    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.

    0 讨论(0)
提交回复
热议问题