Jump to content

HotKey UDF


Yashied
 Share

Recommended Posts

 

Hi there!

I'm having a problem here that the assigned hotkeys are not recognised until I unfocus and refocus the GUI window that should process them. I'm running Win 7 x64.

Minimal example:

#include <GUIConstantsEx.au3>
#include <HotKey.au3>
#include <vkConstants.au3>

Global $hWnd

$hWnd = GUICreate("HotKeys", 800, 600, -1, -1, BitOR($WS_CAPTION, $WS_MINIMIZEBOX, $WS_MAXIMIZEBOX, $WS_SYSMENU, $DS_CONTEXTHELP, $WS_SYSMENU, $WS_VISIBLE, $WS_CLIPCHILDREN, $WS_THICKFRAME))
GUISetState(@SW_ENABLE, $hWnd)

_HotKey_Assign($VK_RETURN, "PressedEnter", $HK_FLAG_NOBLOCKHOTKEY, $hWnd)

While GUIGetMsg() <> $GUI_EVENT_CLOSE
Sleep(10)
WEnd

GUIDelete($hWnd)
Exit

Func PressedEnter()
ConsoleWrite("Pressed enter")
EndFunc

I don't get the "Pressed enter" message (i.e. hotkey is not recognised) until I click on another window and then back to this one. Am I using your UDF wrong?  :ermm:

 

 

Tested the above mentioned example on Win8.1 x64 - same result. The hotkey doesn't work until I refocus the window. Any suggestions? :(

Edited by Armag3ddon
Link to comment
Share on other sites

works fine after i changed a couple things - you used '$WS_SYSMENU' twice and other styles you chose are breaking it - see the help file for gui control styles

#include <GUIConstantsEx.au3>
#include "F:\AutoIt\Resources\Funcs\_other\HotKey_Yashid\HotKey_2.1b.au3"
#include "F:\AutoIt\Resources\Funcs\_other\HotKey_Yashid\HotKey_vkConstants.au3"

Global $hWnd

$hWnd = GUICreate("HotKeys", 800, 600) ;, -1, -1, BitOR($WS_CAPTION, $WS_MINIMIZEBOX, $WS_MAXIMIZEBOX, $WS_SYSMENU, $DS_CONTEXTHELP, $WS_SYSMENU, $WS_VISIBLE, $WS_CLIPCHILDREN, $WS_THICKFRAME))
GUISetState(@SW_SHOW, $hWnd)

_HotKey_Assign($VK_RETURN, "PressedEnter", $HK_FLAG_NOBLOCKHOTKEY, $hWnd)
ConsoleWrite("error=" & @error & @LF)

While GUIGetMsg() <> $GUI_EVENT_CLOSE
Sleep(10)
WEnd

GUIDelete($hWnd)
Exit

Func PressedEnter()
    ConsoleWrite("Pressed enter")
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

Thanks for the tip with the styles. Never saw that I typed SYSMENU twice ;) Well, didn't do any harm anyway, it's all BitOR'ed. Looking at the help file isn't of much use, it's not written in there which style breaks this UDF...

After some trial'n'error I found out that $WS_VISIBLE is breaking this. I need to set the state @SW_NORMAL now but then it works.

Link to comment
Share on other sites

  • 3 weeks later...

@Yashied

i posted this before, but in case you missed it - line 211 in 2.1b...

If $HK_RID Then
    GUIRegisterMsg($HK_WM_INPUT, 'HK_WM_INPUT')
    If Not __HK_Raw() Then

    EndIf
EndIf

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

Link to comment
Share on other sites

  • 4 months later...
  • 5 weeks later...


I've been playing with this for a couple days and I'm wondering if this is a known issue or just an issue I'm having.  When the hotkey is activated it works as it should but then my keyboard is locked up and I can't type.  If I hit any of 4 specific keys then it returns to normal until the next time a hotkey is used. The keys that release the keyboard and let it work again are any one of the following 4 keys... Shift, Ctrl, Space, Alt.

This problem occurs on 2 different Win7 boxes and a win8.1 box at work that I have tried.  On my Home win8.1 box the code works fine and does not have this issue.  All machines tested on have been built by me so I'm not sure what I'm running at work that I don't also run at home.

If I have notepad open and type its fine.  When I  hit 1 the hotkey triggers and I get the popup.  I click OK.  But I can no longer type in notepad until I hit one of the 4 keys listed above.  It does not matter if I change the hotkey to something else or change the action it does.  The key is triggered.  Does its action fine but then I can't type at all until I fix it by using one of the 4 keys.

Is this a known issue?  What should I be looking for as I'm sure the issue is on my end somewhere.

Here is the test code I'm using.

#include <HotKey.au3>
Global Const $VK_1 = 0x31


HotKeySet("{ESC}", "Terminate")
Func Terminate()
    Exit 0
EndFunc   ;==>Terminate



$t = _HotKey_Assign ( $vk_1, 'hia')


While 1
    sleep(10000)
Wend
Exit


Func hia()
    MsgBox(0, @ScriptName,  'Line ' & @ScriptLineNumber & @CRLF & '--- in hia ---')
EndFunc
Link to comment
Share on other sites

You can disable RID module inside the library (not recommended):

 

Global Const $HK_RID = False

But the best way is not to use MsgBox() or similar functions within user-defined function. This library was not designed for this purposes.

Link to comment
Share on other sites

Changing that does make it work.   I use Msgbox and similar stuff all the time in functions during construction for testing.   Another simple thing that fails on my boxes is a simple    Send('a')     command so it not just the msgbox.

I don't use like to use general hotkeys often as they can interfere with other stuff.  I was hoping this could replace regular hotkeys completely as I really like the way it can be easily limited to a certain window without me having to check within my function if it should trigger or pass the key through.

I see where your Constant is used but I don't know what its doing.  If its not recommended to change this then I'll tend to not want to change it.  If its a quick answer then what is the danger in changing this to "False"?   If its a long answer then I'll take your word for it.

Thanks.

Link to comment
Share on other sites

RID (Raw Input Data) module has been added in latest version. This is to prevent silent removal hook from the hook chain in Windows 7 and later (read this for more information). Nevertheless, it was added as an additional protection but not a necessity. If you have everything works fine without RID, you can leave FALSE.

Anyway, you can use a more elegant method:

 

#Include <HotKey.au3>

Global Const $VK_1 = 0x31

Global $Int = False

AdlibRegister('Timer', 100)

_HotKey_Assign($VK_1, 'hia')

While 1
    Sleep(1000)
WEnd

Func hia()
    $Int = True
EndFunc   ;==>hia

Func Timer()
    If $Int Then
        MsgBox(0, @ScriptName, 'Line ' & @ScriptLineNumber & @CRLF & '--- in hia ---')
        $Int = False
    EndIf
EndFunc   ;==>Timer

Alternatively, you can use >NotifyBox UDF instead of MsgBox() but by default it will not suspend your script.

Another way:

#Include <HotKey.au3>
#Include <NotifyBox.au3>

Global Const $VK_1 = 0x31

_HotKey_Assign($VK_1, 'hia')

While 1
    Sleep(1000)
WEnd

Func hia()
    _MsgBox(0, @ScriptName, 'Line ' & @ScriptLineNumber & @CRLF & '--- in hia ---')
EndFunc   ;==>hia

Func _MsgBox($iFlags, $sTitle, $sText, $iTimeOut = 0, $hParent = 0, $hInstance = 0, $iIcon = 0)

    Local $hWnd = _NotifyBox($iFlags, $sTitle, $sText, $iTimeOut, $hParent, $hInstance, $iIcon)

    If Not $hWnd Then
        Return 0
    EndIf
    While WinExists($hWnd)
        Sleep(100)
    WEnd
    Return 1
EndFunc   ;==>_MsgBox
Edited by Yashied
Link to comment
Share on other sites

Thank you for the info.  I tried that first method and it works fine for me while leaving the constant set to true.

After reading the link you provided I see what your trying to prevent.   I will leave the extra protection in place and use your more elegant method.   Thank you.

I had not seen that udf before.  Looks interesting.  I currently use Notify for alerts when I don't want to stop the script.

lol... its written by Melba23 and a credit to you and UEZ.  Looks like I'm already benefiting from your work before this hotkey udf.   So thanks again.

Cal.

Link to comment
Share on other sites

Okay this UDF is just awesome. Especially with the HotKeyInput.au3!  Except one big issue. 
I tried to write a macro with it which sends keystrokes if the hotkeys are pressed. First I've created a GUI with some "hotkeyinputs". When a button "apply" is hit the script assigns they user defined keys or key combinations with the functions. Everything works fine but the big problem is if someone sets a hotkey to e.g. ctrl.+ 1 it does not work. This is because the window where the hotkey is applied to uses ctrl as a modifier. So if u press ctrl in the window the keys will have other funcs. then they normally do.So when the user presses the scripts hotkey (ctrl. + 1) it will send all the keystrokes but with ctrl still pressed by the user.

You could fix that with just a short sleeptime right before the keys are sent so the user has time to release ctrl. but thats not how i want it to work.
Is there a way to fix this?

And if its not. Is there a way to block ctrl + and alt + anything in a hotkeyinput?

thx in advance. hope u understand my problem :P 

Link to comment
Share on other sites

If I understand you correctly you need to keep order of hooks in the hook chain. The hook from HotKey UDF must be loaded first and should never be unloaded until your script is running. Use $HK_FLAG_NOUNHOOK flag in the appropriate functions. This guarantees that the hook from HotKeyInput UDF will is always the first hook in the hook chain. Anyway, could you write a little script reproducing the issue?

Link to comment
Share on other sites

Thx for quick reply.
I made up a script which is basicly the same with just less functions/hotkeyinputs and an other send method.
 

#include <HotKey.au3>
#include <HotKeyInput.au3>
#include <GUIConstantsEx.au3>
$InvokeItMainForm = GUICreate("Test", 296, 310, 192, 164, -1, BitOR($WS_EX_APPWINDOW, $WS_EX_TOOLWINDOW, $WS_EX_WINDOWEDGE))
$ColdSnapHotKeyInput = _GUICtrlHKI_Create(0, 155, 31, 97, 21)
$ApplyButton = GUICtrlCreateButton("Apply", 8, 272, 283, 33)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ApplyButton
            $hWnd = WinGetHandle("WindowName")
            _HotKey_Release() ; I need to this reset all hotkeys set before
            _HotKey_Assign(_GUICtrlHKI_GetHotKey($ColdSnapHotKeyInput), 'Function1', $HK_FLAG_NOUNHOOK , $hWnd)
            ;i my script there are about 10 of this with other functions
    EndSwitch
WEnd

Func Function1()
    Send("KEYS"); I use postmessage in my script
EndFunc
Edited by Nachtigeil
Link to comment
Share on other sites

You must call once _HotKey_Assign() before use _GUICtrlHKI_Create(). This guarantees the hook priority for HotKeyInput UDF if $HK_FLAG_NOUNHOOK flag will be used in _HotKey_Release().

 

#Include <HotKey.au3>
#Include <HotKeyInput.au3>
#Include <GUIConstantsEx.au3>

_HotKey_Assign(0x00FF, 'Function1')
_HotKey_Release($HK_FLAG_NOUNHOOK)

$InvokeItMainForm = GUICreate("Test", 296, 310, 192, 164, -1, BitOR($WS_EX_APPWINDOW, $WS_EX_TOOLWINDOW, $WS_EX_WINDOWEDGE))
$ColdSnapHotKeyInput = _GUICtrlHKI_Create(0, 155, 31, 97, 21)
$ApplyButton = GUICtrlCreateButton("Apply", 8, 272, 283, 33)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ApplyButton
            $hWnd = WinGetHandle("WindowName")
            _HotKey_Release($HK_FLAG_NOUNHOOK)
            _HotKey_Assign(_GUICtrlHKI_GetHotKey($ColdSnapHotKeyInput), 'Function1', $HK_FLAG_DEFAULT, $hWnd)
    EndSwitch
WEnd

Func Function1()
    Send("KEYS")
EndFunc   ;==>Function1
Link to comment
Share on other sites

  • 3 months later...

@

Yashied

Please see this image. Why this is happening ?

post-89644-0-93082800-1426364055_thumb.j

Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Link to comment
Share on other sites

How to use same Hotkey in two applications. Since English is not my first language, i can't understand the explanation from Yashied'd HotKey_21b.au3 file. In that file he explained about Flags. But i couldn't get the point properly. My hotkey will operate the same function in both application. But i wrap each application with my code. That means, thease apps are running with my script. So i need to make two identical script for both these apps. In this context, how can i use same hot key ?

Edited by kcvinu
Spoiler

My Contributions

Glance GUI Library - A gui library based on Windows api functions. Written in Nim programming language.

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

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