Jump to content

HotKey UDF


Yashied
 Share

Recommended Posts

I use this excellent UDF :oops: in my program HRC. Now some users reported that HRC always stopped working after some hotkeys have been pressed. I always wondered what the reason might be, and I now assume it's this one:

Global hooks getting lost on Windows 7

My (hopefully working :bye:) solution for HRC was to use the hotkey events only to set flags. These flags then trigger working functions (in my case the change of the display resolution) in the programs main loop. This way the hooks should always return in less than 300ms.

Link to comment
Share on other sites

What flags are you using? By default, the user-defined function calls via PostMessage() function. This means that it does not affect the completion of the hook procedure. If you are using a $HK_FLAG_WAIT flag, then the hook procedure will not be completed until the user-defined function is finished. Here's a simple test for these two modes:

#Include <HotKey.au3>

Global Const $VK_ESCAPE = 0x1B
Global Const $VK_F11 = 0x7A
Global Const $VK_F12 = 0x7B

_HotKey_Assign($VK_ESCAPE, 'Quit')

_HotKey_Assign($VK_F11, 'Test', BitOR($HK_FLAG_NOOVERLAPCALL, $HK_FLAG_NOREPEAT))
_HotKey_Assign($VK_F12, 'Test', BitOR($HK_FLAG_WAIT, $HK_FLAG_NOREPEAT))

While 1
    Sleep(1000)
WEnd

Func Test()
    ConsoleWrite('+' & @CR)
    Sleep(1000)
    ConsoleWrite('-' & @CR)
EndFunc   ;==>Test

Func Quit()
    Exit
EndFunc   ;==>Quit

Strange, but on my Windows 7 x86 the hook stays firmly in any mode, and for any sleep.

:oops:

Edited by Yashied
Link to comment
Share on other sites

Just tested your code on my Win7 64bit (running AutoIt as 32bit). Start the code and press F12 fast 10 to 20 times repeatedly, the hotkeys are dead for me after that. Though it does not seem to happen on F11, nifty trick with the PostMessage, hadn't seen that one before :oops:...

Edit: And the flags I use in HRC are BitOR($HK_FLAG_DEFAULT, $HK_FLAG_EXTENDEDCALL) and none (defaulting to $HK_FLAG_DEFAULT)...hmmm, according to your test script above this should not be the reason why some users report failing hotkeys...

Edited by KaFu
Link to comment
Share on other sites

  • 3 months later...

HI all iam new in this Forum.

Iam here because i need HotKeys Global or for a defined window. It works only with Yashied´s HotKey_20b.au3.

But there are some "bugs" - u cant send anything(a hotkey call a function that send('hallo') didnt work correct).

@Yashied: can u change your script that this work correct? - i have some ideas to get this work.

mfg D2thunder

Link to comment
Share on other sites

HI again

Its a very nice UDF.

I read all posts and have a Question:

Anyone tested if u can hook CTRL-ALT-DEL? that windows dont get it. I mean block CTRL-ALT-DEL.

i know if u do normal with a hook it dont work.

BUt what is when u compile script and let it run as Service with system privilegs?

I would test it but _Service_UDF from arcker didndt work on my Windows7 x64.

if it work it would be great.

mfg

Dominic

Link to comment
Share on other sites

  • 1 month later...

I was wondering if with this UDF it can detect when the hotkey is being held down ?

HotKeySet("{a}", "TogglePause")

while 1
sleep(250)
WEnd

Func TogglePause()
ConsoleWrite("A")
EndFunc ;==>TogglePause

For example if i hold a, togglepause function re-runs but hotkey cant work for f12 whereas this UDF can. :(

**EDIT Incase someone comes across this same question i think the answer is this. ie $HK_FLAG_NOUNHOOK

#Include <HotKey.au3>

_HotKey_Assign(0xAE, 'VolumeUP', $HK_FLAG_NOUNHOOK)

While 1
Sleep(250)
WEnd

Func VolumeUP()
ConsoleWrite("A")
EndFunc ;==>Quit
Edited by IanN1990
Link to comment
Share on other sites

One last question has maybe i am being a little slow. In the first post it states this UDF can detect but not block Ctrl+Alt+Delete.

I would like to detect and not block it, but the following code doesn't seam to work..

#Include 'HotKey.au3'

_HotKey_Assign(BitAND($CK_CONTROL, $CK_ALT, 0x2E), 'Test', $HK_FLAG_NOBLOCKHOTKEY)

sleep(5000)

Func Test()
ConsoleWrite("A")
EndFunc

Have i done something wrong ?

Edited by IanN1990
Link to comment
Share on other sites

Hi,

I also have some problems with the UDF. Maybe you can help me, would be very cool.

OK, i'm using the UDF for an Interface which is created just with this UDF. I noticed that I can just release hotkeys with a function, which isn't called by a hotkey, that was really annoying lol. So, I use a variable to store a bool value which says "release or not" and an adlibregister() function which checks this var and if true, it releases all hotkeys. This should work I think, but it doesn't. If I don't use $hk_flag_noerror to assign the hotkeys and leave the flag in _hotkey_release() blank, it returns 0, @error =1 and @extended =-1. So I tryed with $hk_flag_noerror in _hotkey_release(), but no $hk_flag_noerror in the _hotkey_assign()-function. it returns 1 and @error =0, but after that it crashes in _hk_wm_hotkey() in line 734. If i use $hk_flag_noerror in both cases, _hotkey_assign() and _hotkey_release(), it says 1 too, but my new hotkeys won't work correctly (some work, some don't). So, how the hell, can I fix this problem? I just need to release my hotkeys, because first of all I have a menu, after that I have to use other hotkeys and also the same but for other things... I don't know, what can I do? Please, if you know how, help me. Lol. Oh, i've forgotten to tell you that i'm using the beta 2.0b, because I need the $hk_flag_postcall feature. So, please give me some hints.

best regards,

Timtam

Link to comment
Share on other sites

hi guys - having a problem with Obfuscator again (1.0.30.1 and previous version) Edited by iCode

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Link to comment
Share on other sites

  • 2 weeks later...

don't know what's going on here, but _Hotkey_Assign() is failing for me.

it was failing only when GUISetAccelerators() was used, but now it fails all the time

in the script i'm working with i need the structure to be as below where the GUI is in a user func and the _Hotkey_Assign() is in another user func

steps...

1) run script - _assignHk() runs with no errors as expected

2) press 'ESC' to hide the GUI (or the button apparently)

3) press F1 to show the GUI

4) press 'ESC' (or button) again - _assignHk() runs again, but this time _Hotkey_Assign() fails and it will fail every time thereafter

#include "[path]\HotKey\_Hotkey.au3"
#include "[path]\_HotkeyInput.au3"
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUICloseOnESC", 0)

Global $hotkey = "0x0070"

_assignHk()

_showGUI()

While 1
    Sleep(1000)
WEnd

Func _showGUI()

    ;_Hotkey_Release() ; doesn't help
    ;_Hotkey_Disable() ; doesn't help

    Local $Form = GUICreate("", 250, 100, -1, -1)
    GUICtrlCreateLabel("Press ESC, then F1 - Repeat", 8, 12, 200, 21)
    Local $Button = GUICtrlCreateButton("IGNORE ME", 8, 40, 75, 25)
    Local $Form1_AccelTable[1][2] = [["{ESC}", $Button]]
    GUISetAccelerators($Form1_AccelTable)

    GUISetState(@SW_SHOW)

    While 1
        Switch GUIGetMsg()

            Case $GUI_EVENT_CLOSE

                Exit

            Case $Button

                _GUICtrlHKI_Release()

                _assignHk()

                GUIDelete($Form)

                ;_Hotkey_Enable() ; doesn't help

                ExitLoop

        EndSwitch
    WEnd

EndFunc

Func _assignHk()

    _Hotkey_Assign($hotkey, "_Hk_showUi", $HK_FLAG_DEFAULT)

    If @error Then
        MsgBox(48, "", "Unable to set hot-key!")
        Exit
    EndIf

EndFunc

Func _Hk_showUi() ; not supposed to introduce significant delays in func called from _Hotkey_Assign(), so we call _showGUI() from this func

    _showGUI()

EndFunc

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Link to comment
Share on other sites

here is latest example of _HotKey_Assign() failing

this is on 7 x64 with 3.3.8.1 and 3.3.9.4 beta

#include "F:AutoIt_Funcs_OtherHotKey_Hotkey.au3"
#include "F:AutoIt_Funcs_OtherHotKey_HotkeyInput.au3"
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUICloseOnESC", 0)

Global $err, $in_Hk
Global $Hk = 112 ; F1

_AssignHk($Hk, 0) ; hotkey to assign, whether to unassign old one [1/0]

_ShowGUI()

While 1

    Sleep(1000)

WEnd

Func _ShowGUI()

    ConsoleWrite(@LF & "::: FUNC _ShowGUI() :::" & @LF)

    $in_Hk = $Hk

    ;$err = _HotKey_Disable() ; FAILS ON 2nd RUN OF GUI, BUT HOTKEY STILL WORKS
    ;ConsoleWrite("_HotKey_Disable=" & $err & @LF)
    ;ConsoleWrite("_HotKey_Disable (@extended)=" & @extended & @LF)

    Local $Form = GUICreate("", 200, 200, -1, -1)
    GUICtrlCreateLabel("1) assign new HK" & @LF & "2) press ESC or Save" & @LF & "3) press new HK to show GUI" & @LF & "4) keep repeating" & @LF & @LF & _
        "_HotKey_Assign( ) fails on 2nd attempt and does not set @error to non-0. Also the previously assigned HK still works.", 8, 12, 190, 120)
    Local $Input_Hk = _GUICtrlHKI_Create("", 12, 130, 121, 21)
    Local $Button = GUICtrlCreateButton("Save", 8, 160, 75, 25)
    Local $Form_AccelTable[1][2] = [["{ESC}", $Button]]
    GUISetAccelerators($Form_AccelTable)

    _GUICtrlHKI_SetHotKey($Input_Hk, $Hk)

    GUISetState(@SW_SHOW)

    While 1

        Switch GUIGetMsg()

            Case $GUI_EVENT_CLOSE

                Exit

            Case $Button

                ExitLoop

        EndSwitch

    WEnd

    ConsoleWrite("$in_Hk=" & $in_Hk & @LF)
    ;ConsoleWrite("GUICtrlRead($Input_Hk)=" & GUICtrlRead($Input_Hk) & @LF)
    $Hk = _GUICtrlHKI_GetHotKey($Input_Hk)
    ConsoleWrite("$Hk=" & $Hk & @LF)

    _GUICtrlHKI_Release()

    GUIDelete($Form)

    If $in_Hk <> $Hk Then
        _AssignHk($Hk, 1) ; hotkey to assign, whether to unassign old one [1/0]
    EndIf

    ;$err = _HotKey_Enable() ; FAILS ON 2nd EXIT OF GUI, BUT HOTKEY STILL WORKS
    ;ConsoleWrite("_HotKey_Enable=" & $err & @LF)

EndFunc

Func _AssignHk($iHotkey, $iClear) ; hotkey to assign, whether to unassign old one [1/0]

    ConsoleWrite(@LF & "::: FUNC _AssignHk() :::" & @LF)

    If $iClear = 1 Then
        ConsoleWrite("clearing old HK: " & $in_Hk & @LF)
        $err = _HotKey_Assign($in_Hk) ; clear the old one
        ;ConsoleWrite("_HotKey_Assign ($err)=" & $err & @LF)
        ;ConsoleWrite("_HotKey_Assign (@error)=" & @error & @LF)
    Endif

    If $iHotkey > 0 Then ; 0=None
        ConsoleWrite("assigning new HK: " & $iHotkey & @LF)
        $err = _HotKey_Assign($iHotkey, "_ShowGUI", $HK_FLAG_DEFAULT) ; FAILS TO ASSIGN NEW HOTKEY ON 3rd RUN OF GUI AND THEREAFTER
        ConsoleWrite("_HotKey_Assign ($err)=" & $err & @LF)
        ConsoleWrite("_HotKey_Assign (@error)=" & @error & @LF)

        If @error Then
            MsgBox(48, "", "@error is non-0 -- Unable to set hot-key!")
        EndIf

        If $err <> 1 Then
            MsgBox(48, "", "$err is not 1 (@error should be non-0) -- Unable to set hot-key!")
        EndIf
    EndIf

EndFunc
Edited by iCode

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Link to comment
Share on other sites

  • 2 weeks later...

Baahh, I can only get it to work on a single keystroke, not with ALT or CTRL.

For example

#include

_HotKey_Assign(BitOR(0x11,0x4B), 'toggleNumLock')

While 1
Sleep(50)
WEnd

Func toggleNumLock()
TrayTip('title','text', 3)
EndFunc

No worky worky :guitar:

Tried on win7 x64 and XP virtual machine. Using version 1.7b. It also breaks Send functionality it seems.

Edited by Inverted
Link to comment
Share on other sites

Link to comment
Share on other sites

  • 2 weeks later...

the reason _HotKey_Assign() is failing in my examples above, as i finally figured out, is that apparently you cannot open the GUI which is used to assign hot-keys using a hot-key.

if you do so, and try to change a hot-key, _HotKey_Assign() fails every time

so what i cannot figure out is how to make this work...

1 - have a gui where you can assign hot-keys where one of them will be used for opening the gui itself

2 - if opening the gui with the assigned hot-key, and then change any of the others, code it so _HotKey_Assign() doesn't fail

i've tried _HotKey_Release, Enable and Disable, as well as _HotKey_Assign($iKey) when the gui func runs, but nothing works

i am also running the gui function from another function when it is called by the hot-key and that doesn't work. i.e...

hot-key is assigned to _GUIPrep() with $HK_FLAG_DEFAULT + $HK_FLAG_EXTENDEDCALL + $HK_FLAG_POSTCALL

Func _GUIPrep($iKey)

If $iKey < 0 Then

_GUI()

EndIf

EndFunc

Func _GUI()

; do stuff

EndFunc

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

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