Jump to content

HotKey UDF


Yashied
 Share

Recommended Posts

I'm trying to use HotKey.au3 to use Win-L to lock the computer and run the screen saver based on a post from Lej.

After pressing Win-L the computer locks and the screen saver runs. The problem is after I unlock the computer the keyboard locks up until I hit a modifier key (shift, ctrl, alt, or win).

Try this to recreate:

  • Have Notepad open when running the script
  • Run the script
  • Press Win-L
  • Unlock the computer
  • Try typing in Notepad
  • Typing "l" the first time will allow all the alphanumeric keys to work. Typing "l' the second time will lock the computer without pressing the Win key.
#include "HotKey.au3"
#include "vkConstants.au3"

HotKeySet("{Esc}", "_Quit")
_HotKeyAssign(BitOr($CK_WIN, $VK_L), "ScreenSaver")

While 1
    Sleep(10)
WEnd

Func ScreenSaver()
    Local Const $SC_SCREENSAVE = 0xF140
    DllCall('user32.dll', 'int', 'LockWorkStation')
     _SendMessage(_WinAPI_GetDesktopWindow(), $WM_SYSCOMMAND, $SC_SCREENSAVE, 0)
EndFunc   ;==>ScreenSaver

Func _Quit()
    Exit
EndFunc   ;==>_Quit

The problem isn't specific to Win-L. The same thing happens when I change the script to use Win-K (hitting "k" the second time will lock the computer)

Any ideas on what I'm doing wrong?

--Jeff

On XP everything works fine for me. What are your system?
Link to comment
Share on other sites

I'm using AutoIt 3.3.0.0 (the latest non-beta version) and XP Professional with Service Pack 3.

I just tried running the same script in a Virtual PC running a clean build of XP Profession with Service Pack 2 and had the same problem. Same problem after compiling with 3.3.1.1 and even after rebooting.

No I'm really confused. Is anyone else able to duplicate the problem?

--Jeff

Link to comment
Share on other sites

I'm using AutoIt 3.3.0.0 (the latest non-beta version) and XP Professional with Service Pack 3.

I just tried running the same script in a Virtual PC running a clean build of XP Profession with Service Pack 2 and had the same problem. Same problem after compiling with 3.3.1.1 and even after rebooting.

No I'm really confused. Is anyone else able to duplicate the problem?

--Jeff

Yes, I caught this bug. I think that is to blame Lock Workstation. Apparently stops working hook or something like that. I would think.

EDIT:

When I wrote this UDF, I not thought that it could be used for this purpose.

Edited by Yashied
Link to comment
Share on other sites

Try this.

#Include 'HotKey.au3'
#Include 'vkConstants.au3'

HotKeySet('{Esc}', '_Quit')
_HotKeyAssign(BitOR($CK_WIN, $VK_L), 'ScreenSaver')

While 1
    Sleep(10)
WEnd

Func ScreenSaver()
    Local Const $SC_SCREENSAVE = 0xF140
    Local $Timer = TimerInit()
    DllCall('user32.dll', 'int', 'LockWorkStation')
    Do
        Sleep(10)
        If TimerDiff($Timer) > 1000 Then
            Return
        EndIf
    Until _IsWorkstationLocked()
    _SendMessage(_WinAPI_GetDesktopWindow(), $WM_SYSCOMMAND, $SC_SCREENSAVE, 0)
    While _IsWorkstationLocked()
        Sleep(10)
    WEnd
    DllCall('user32.dll', 'int', 'keybd_event', 'int', $VK_L, 'int', 0, 'int', 2, 'ptr', 0)
    DllCall('user32.dll', 'int', 'keybd_event', 'int', $VK_LWIN, 'int', 0, 'int', 2, 'ptr', 0)
    DllCall('user32.dll', 'int', 'keybd_event', 'int', $VK_RWIN, 'int', 0, 'int', 2, 'ptr', 0)
EndFunc   ;==>ScreenSaver

Func _IsWorkstationLocked()
    Local Const $DESKTOP_SWITCHDESKTOP = 0x0100
    Local $Result = False
    $hDesktop = DllCall('user32.dll', 'hwnd', 'OpenDesktop', 'str', 'Default', 'int', 0, 'int', 0, 'int', $DESKTOP_SWITCHDESKTOP)
    If Not @error Then
        $Result = DllCall('user32.dll', 'int', 'SwitchDesktop', 'hwnd', $hDesktop[0])
        $Result = Not $Result[0]
        DllCall('user32.dll', 'int', 'CloseDesktop', 'hwnd', $hDesktop[0])
    EndIf
    Return $Result
EndFunc   ;==>_IsWorkstationLocked

Func _Quit()
    Exit
EndFunc   ;==>_Quit

EDIT:

This, incidentally, solves the Lej problem.

Edited by Yashied
Link to comment
Share on other sites

According to UDF help-file for _ispressed() ALT = 12 => $VK_MENU = 0x12. msdn says "VK_MENU (0x12) (ALT key)".

I know this

Bat : _HotKeyAssign(BitOR($VK_MENU, $VK_RIGHT), 'Message', BitOR($HK_FLAG_DEFAULT, $HK_FLAG_EXTENDEDCALL)) does not want to work

_HotKeyAssign(BitOR($СK_MENU, $VK_RIGHT), 'Message', BitOR($HK_FLAG_DEFAULT, $HK_FLAG_EXTENDEDCALL)) too.

Can you give example for the use of Buttons' Alt ', together with any button ?

Link to comment
Share on other sites

Maybe something like this?

_HotKeyAssign(0x12+0x27, 'Message', BitOR($HK_FLAG_DEFAULT, $HK_FLAG_EXTENDEDCALL))

Still not working

By the way :

HotKeys 'Ctrl' and 'Shift' working only with : $CK_CONTROL and $CK_SHIFT (bat no working with $VK_CONTROL and $VK_SHIFT )

Link to comment
Share on other sites

Still not working

By the way :

HotKeys 'Ctrl' and 'Shift' working only with : $CK_CONTROL and $CK_SHIFT (bat no working with $VK_CONTROL and $VK_SHIFT )

I suggest you read VERY CAREFULLY the documentation for _HotKeyAssign() within a UDF.

_HotKeyAssign(BitOR($CK_ALT, $VK_RIGHT), 'Message')
Link to comment
Share on other sites

  • 4 weeks later...

Hi, I want to create a double click hotkey with your hotkeyset

And here's how I do it now.

It works but i want to know is there a better way to do this?

$iFlag = BitOR($iFlag, $HK_FLAG_EXTENDEDCALL)
Assign("HotkeyFunc" & $iKey, $sFunc, 2)
_HotKeyAssign($iKey, "HotkeyDouble", $iFlag)

Func HotkeyDouble($iKey)
    if $iPriorKey = $iKey and TimerDiff($iPriorKeyTime) < 250 then Call(Eval("HotkeyFunc" & $iKey))
    $iPriorKey = $iKey
    $iPriorKeyTime = TimerInit()
EndFunc
Link to comment
Share on other sites

Hi, I want to create a double click hotkey with your hotkeyset

And here's how I do it now.

It works but i want to know is there a better way to do this?

$iFlag = BitOR($iFlag, $HK_FLAG_EXTENDEDCALL)
Assign("HotkeyFunc" & $iKey, $sFunc, 2)
_HotKeyAssign($iKey, "HotkeyDouble", $iFlag)

Func HotkeyDouble($iKey)
    if $iPriorKey = $iKey and TimerDiff($iPriorKeyTime) < 250 then Call(Eval("HotkeyFunc" & $iKey))
    $iPriorKey = $iKey
    $iPriorKeyTime = TimerInit()
EndFunc

I would have done the following:

#Include <HotKey_17_beta.au3>

Global Const $VK_F12 = 0x7B

Global $Timer = 0

_HotKeyAssign($VK_F12, 'Message', BitOR($HK_FLAG_DEFAULT, $HK_FLAG_EXTENDEDCALL, $HK_FLAG_POSTCALL))

While 1
    Sleep(10)
WEnd

Func Message($iKey)
    If $iKey > 0 Then
        If ($Timer) And (TimerDiff($Timer) < 250) Then
            MsgBox(0, 'Hotkey Test Message', 'Hotkey has been pressed!')
            $Timer = 0
            Return
        EndIf
        $Timer = TimerInit()
    EndIf
EndFunc   ;==>Message

HotKey_17_beta.au3 you can download here.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...