Sign in to follow this  
Followers 0
Richard Robertson

How does "HotKey" work?

17 posts in this topic

I've been wracking my brain trying to figure it out and the best i came up with was several WIN32 API methods that read the current keyboard state. Polling the keyboard every program loop however, is highly inefficient and does not "eat" the key. With AutoIt's "HotKey," it stops the key message from reaching anything else and doesn't appear to suck up memory. How does it work?

Share this post


Link to post
Share on other sites



Oh, thanks. I'll look into that function then.

Share this post


Link to post
Share on other sites

is there a different functionality that you need?

Lar.

How about setting a local hotkey? In other words, if your hotkey is pressed and your GUI is active, it gets the message, otherwise it gets ignored. Obviously it's possible to do that in a script, but a builtin function would be good.

ben

Share this post


Link to post
Share on other sites

How about setting a local hotkey? In other words, if your hotkey is pressed and your GUI is active, it gets the message, otherwise it gets ignored. Obviously it's possible to do that in a script, but a builtin function would be good.

ben

I dont see how the built in function to do what you are describing (assuming I understand fully what you are describing) would even work. The way to do what you are talking about is just have a little check to see if your GUI is active each time the GUI is polled. If it is active set the HotKey if not set the HotKey blank.

That isnt something that could just be written as it would cause an indefinite loop which would in turn make nothing run. In a different thread that would be possible, but AutoIt doesnt support multi-threading.

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

I dont see how the built in function to do what you are describing (assuming I understand fully what you are describing) would even work. The way to do what you are talking about is just have a little check to see if your GUI is active each time the GUI is polled. If it is active set the HotKey if not set the HotKey blank.

That isnt something that could just be written as it would cause an indefinite loop which would in turn make nothing run. In a different thread that would be possible, but AutoIt doesnt support multi-threading.

JS

Its called an accelerator table and it would probably take a half hour to write the functionality. I'm not saying I will write it, but I am saying that somebody who wants to add the feature can create a wrapper function in AutoIt (Core language, not user-defined function) around CreateAccelerateTable()/LoadAccelerators(). I would expect the CreateAcceleratorTable() wrapper function to take a 2 dimensional array where element 0 is a Send()-compatibil hotkey string and element 1 is the control ID to invoke. The CreateAcceleratorTable() should then automatically call LoadAccelerators() and as long as TranslateAccelerators() is called in the message pump, AutoIt GUI's will magically support accelerator keys.

Share this post


Link to post
Share on other sites

Its called an accelerator table and it would probably take a half hour to write the functionality. I'm not saying I will write it, but I am saying that somebody who wants to add the feature can create a wrapper function in AutoIt (Core language, not user-defined function) around CreateAccelerateTable()/LoadAccelerators(). I would expect the CreateAcceleratorTable() wrapper function to take a 2 dimensional array where element 0 is a Send()-compatibil hotkey string and element 1 is the control ID to invoke. The CreateAcceleratorTable() should then automatically call LoadAccelerators() and as long as TranslateAccelerators() is called in the message pump, AutoIt GUI's will magically support accelerator keys.

I really would like a "local" hotkey, so that only when the gui is active it will do something

HotKeySet("{Enter}", "onEnter")

func onenter()
    If WinActive($GUI) Then msgbox(0,"Active", "The gui is active and you hit enter"_
    Else
        HotKeySet("{Enter}");un-register hotkey
        Send("{enter}")
        HotKeySet("{Enter}", "EnterSends");register hotkey
    EndIf
endfunc

or put in your main loop

if WinActive ($GUI) then ; GUI gui's variable or its title

HotKeySet("{enter}", "onenter")

else

HotKeySet("{enter}")

endif

Share this post


Link to post
Share on other sites

Yep, and with @HotKeyPressed you can set it up to respond to a range of keys. I know this can be implemented in user code, it would just be nice to have a builtin function to do it.

ben

Share this post


Link to post
Share on other sites

Its called an accelerator table and it would probably take a half hour to write the functionality. I'm not saying I will write it, but I am saying that somebody who wants to add the feature can create a wrapper function in AutoIt (Core language, not user-defined function) around CreateAccelerateTable()/LoadAccelerators(). I would expect the CreateAcceleratorTable() wrapper function to take a 2 dimensional array where element 0 is a Send()-compatibil hotkey string and element 1 is the control ID to invoke. The CreateAcceleratorTable() should then automatically call LoadAccelerators() and as long as TranslateAccelerators() is called in the message pump, AutoIt GUI's will magically support accelerator keys.

Ah thank you. I dont know much about API calls or anything really. :lmao:

Would a properly written plug-in work?

Thanks for the input Valik,

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

Ah thank you. I dont know much about API calls or anything really. :lmao:

Would a properly written plug-in work?

Thanks for the input Valik,

JS

Not really. For one thing, the last I knew about the plug-in architecture, arrays weren't supported and specifying the accelerator table as anything other than a 2 dimensional array would be hell to write in AutoIt. Second, unless AutoIt already calls TranslateAccelerator() in it's message pump, it will need to so somebody has to make some sort of modification to the core language. I doubt AutoIt is calling TranslaterAccelerator() (But it might be, I haven't looked).

Share this post


Link to post
Share on other sites

Not really. For one thing, the last I knew about the plug-in architecture, arrays weren't supported and specifying the accelerator table as anything other than a 2 dimensional array would be hell to write in AutoIt. Second, unless AutoIt already calls TranslateAccelerator() in it's message pump, it will need to so somebody has to make some sort of modification to the core language. I doubt AutoIt is calling TranslaterAccelerator() (But it might be, I haven't looked).

Gotcha... I didnt know that about plug-ins and array's. I will keep that in mind.

JS


AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share this post


Link to post
Share on other sites

I wanted the functionality in a program that wasn't written in AutoIt. I wrote a .Net wrapper for the hotkey system if anyone is interested. It even unloads all left-over registered hotkeys when the AppDomain is unloaded.

Share this post


Link to post
Share on other sites

I wanted the functionality in a program that wasn't written in AutoIt. I wrote a .Net wrapper for the hotkey system if anyone is interested. It even unloads all left-over registered hotkeys when the AppDomain is unloaded.

I'M VERY INTERESTED!!

every time i use my script the hotkey never unloads and i have to restart explorer to get the shift or control or what ever hot key i used to release. here is my script:

Func Snd()

Send("{NUMPAD7 1000}[,0]")

Beep(400, 500)

EndFunc ;==>Snd

HotKeySet("!a", "Snd")

Opt("SendKeyDelay", 55)

While 1

Sleep(100)

WEnd

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Whenever you write a script using HotKeys you should unload them when the script exits.

HotKeySet("{ENTER}", "Green")
GUICreate("Test Window")
GUISetState()
While 1
    If GUIGetMsg() = -3 Then
        HotKeySet("{ENTER}");;  Unloads the hotkey
        Exit
    EndIf
WEnd

Func Green()
    GUISetBKcolor(0x00FF00)
    HotKeySet("{ENTER}", "Red")
EndFunc

Func Red()
    GUISetBkColor(0xFF0000)
    HotKeySet("{ENTER}", "Green")
EndFunc
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Whenever you write a script using HotKeys you should unload them when the script exits.

HotKeySet("{ENTER}", "Green")
GUICreate("Test Window")
GUISetState()
While 1
    If GUIGetMsg() = -3 Then
        HotKeySet("{ENTER}");;  Unloads the hotkey
        Exit
    EndIf
WEnd

Func Green()
    GUISetBKcolor(0x00FF00)
    HotKeySet("{ENTER}", "Red")
EndFunc

Func Red()
    GUISetBkColor(0xFF0000)
    HotKeySet("{ENTER}", "Green")
EndFunc

ok. so how do i incorporate the unloading of my hotkey in my script?

Func Snd()
    Send("{NUMPAD7 1000}[,0]")
    Beep(400, 500)
EndFunc   ;==>Snd

HotKeySet("!a", "Snd")
Opt("SendKeyDelay", 55)
While 1
    Sleep(100)
WEnd

Share this post


Link to post
Share on other sites

How are you exiting your script?

I see no exitpoint in there so you may have to use something like this

Func Snd()
    Send("{NUMPAD7 1000}[,0]")
    Beep(400, 500)
EndFunc   ;==>Snd

OnAutoItExitRegister("_Quit")
HotKeySet("!a", "Snd")
Opt("SendKeyDelay", 55)
While 1
    Sleep(100)
WEnd

Func _Quit()
    HotKeySet("!a");;  Unloads the hotkey
    Exit
EndFunc

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Hotkeys are automatically unregistered by the system. There's no need to manually unregister them. The problem ProfessorBean describes is caused by using Send() inside the callback for a hotkey. The state of the modifier keys are saved when calling Send() and restored upon completion of Send(). A human cannot release any modifier keys fast enough when invoking Send() via a HotKeySet() callback so Send() saves the modifier state as being pressed, the user releases the key, but Send() restores the modifier state back to pressed and thus a stuck key is created. It's easily cleared by simply pressing the key. It's prevented by ensuring the hotkey is released before invoking Send().

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

works ok. thank you for the advice :mellow:

Edited by ProfessorBean

Share this post


Link to post
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
Sign in to follow this  
Followers 0