HotKey UDF

181 posts in this topic

Posted (edited)

LAST VERSION - 1.8 / 2.1b

25-Dec-13

The library allows to set hotkeys by using the low-level keyboard hook. Below are the main differences between HotKey UDF from the native HotKeySet() function:

  • To assign a hotkeys are used integer values ​​instead of strings as in the HotKeySet() function. This is useful, for example, to save the hotkey's values in the registry.
  • Ability to set any hotkeys including CTRL+ALT+DEL, F12, WIN+*, etc. The only exceptions are special keys such as "Fn" which do not have their own scan code.
  • Ability to utilize or pass on the specified hotkeys for other applications.
  • Ability to set hotkeys that already used by other applications.
  • Ability to set hotkeys only for the specified window(s).
  • Ability to prevent re-activation of the hotkeys when it is held down.
  • Ability to disable previously installed hotkeys without removing the hook from the hook chain, ie without losing priority.
  • Ability to block call user-defined function associated with the hotkey if the previous call has not been completed.

Here is what you can't do by using this library:
  • Set hotkey only for the CTRL, ALT, SHIFT, WIN, and any combination of this keys. Any hotkey should always include one function key. For example, ALT+A, CTRL+SHIFT+F10, etc.
  • Set hotkey for a mouse buttons.
  • Prevent using the hotkey like CTRL+ALT+DEL, CTRL+SHIFT+ESC (Windows Vista+), and similar.
  • Use more than one function key in the hotkey. For example, ALT+A+B, F1+F2, etc.
  • Use "Fn" key or any other keys that do not have their own scan code.

Available functions

_HotKey_Assign

_HotKey_Enable

_HotKey_Disable

_HotKey_Release

HotKey UDF Library v1.8

Previous downloads: 5027

HotKey.au3

HotKey UDF Library v2.1b (Read >here for more information)

Previous downloads: 1791

HotKey_21b.au3

Virtual-Key (VK) Code Constants (Optional)

Previous downloads: 1576

vkConstants.au3

Example1

#Include <HotKey.au3>

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

; Assign "F12" with Message() and set extended function call
_HotKey_Assign($VK_F12, 'Message', BitOR($HK_FLAG_DEFAULT, $HK_FLAG_EXTENDEDCALL))

; Assign "CTRL-ESC" with Quit()
_HotKey_Assign(BitOR($CK_CONTROL, $VK_ESCAPE), 'Quit')

While 1
    Sleep(10)
WEnd

Func Message($iKey)
    MsgBox(0, 'Hot key Test Message', 'F12 (0x' & Hex($iKey, 4) & ') has been pressed!')
EndFunc   ;==>Message

Func Quit()
    Exit
EndFunc   ;==>Quit

Example2

#Include <HotKey.au3>

Global Const $VK_OEM_PLUS = 0xBB
Global Const $VK_OEM_MINUS = 0xBD

Global $Form, $Label
Global $i = 0

$Form = GUICreate('MyGUI', 200, 200)
$Label = GUICtrlCreateLabel($i, 20, 72, 160, 52, 0x01)
GUICtrlSetFont(-1, 32, 400, 0, 'Tahoma')
GUISetState()

; Assign "CTRL-(+)" with MyFunc1() and "CTRL-(-)" with MyFunc2() for created window only
_HotKey_Assign(BitOR($CK_CONTROL, $VK_OEM_PLUS), 'MyFunc1', 0, $Form)
_HotKey_Assign(BitOR($CK_CONTROL, $VK_OEM_MINUS), 'MyFunc2', 0, $Form)

Do
Until GUIGetMsg() = -3

Func MyFunc1()
    $i += 1
    GUICtrlSetData($Label, $i)
EndFunc   ;==>MyFunc1

Func MyFunc2()
    $i -= 1
    GUICtrlSetData($Label, $i)
EndFunc   ;==>MyFunc2
Edited by Yashied

Share this post


Link to post
Share on other sites



Posted

What's wrong with HotKeySet() ? This is much easier :P

Share this post


Link to post
Share on other sites

Posted (edited)

What's wrong with HotKeySet() ? This is much easier :P

Differences HotKeySet() from HotKeyAssign():

Removed. (See first post)

Edited by Yashied

Share this post


Link to post
Share on other sites

Posted

Very nice idea. :thumsup:

Unfortunately the script crashes after it exits. I'm using Vista SP1, AutoIt 3.2.12.1 :P

Share this post


Link to post
Share on other sites

Posted

OK, with the description, i see the advantages :P Great work.

Share this post


Link to post
Share on other sites

Posted (edited)

Thanks for your reply. I specifically used as a hotkey code number, not the string value in HotKeySet (). When writing a complete application where necessary to keep the hotkeys code (such as the registry), it is better to use the number.

Edited by Yashied

Share this post


Link to post
Share on other sites

Posted

Very nice idea. :thumsup:

Unfortunately the script crashes after it exits. I'm using Vista SP1, AutoIt 3.2.12.1 :P

Try AutoIt v3.3.0.0.

Share this post


Link to post
Share on other sites

Posted

Who are interested, I updated the HotKey.au3.

:P

Share this post


Link to post
Share on other sites

Posted

OMG, thank you, I have been looking for this for a long time, I was about to write a program from scratch.

I am try to make a my own hotkeys, such as Win+Alt+F calling a function which sends a Shift+Right. Using HotKeySet the alt key would randomly get passed to the active window 50% of the time. So far, using your program the alt key only gets passed if I have an Office2007 app or OpenOffice as the active window and press the windows key first.

Do you know of any way to fix this small problem, other than training myself to press the alt key first? :D

Also, is there a way to use your program to capture the windows key so that it does not open the StartMenu, but still works for the hot keys? I tried capturing using VK_LWIN but it blocked the hot keys that used CK_WIN.

I will be trying to set approximately 90 hot keys, including the blocked ones (Win+K, Win+U). I will reply to this thread with my findings.

Again, thank you, this is a vast improvement.

BTH

Share this post


Link to post
Share on other sites

Posted

This library has been updated.

:D

Share this post


Link to post
Share on other sites

Posted

OMG, thank you, I have been looking for this for a long time, I was about to write a program from scratch.

I am try to make a my own hotkeys, such as Win+Alt+F calling a function which sends a Shift+Right. Using HotKeySet the alt key would randomly get passed to the active window 50% of the time. So far, using your program the alt key only gets passed if I have an Office2007 app or OpenOffice as the active window and press the windows key first.

Do you know of any way to fix this small problem, other than training myself to press the alt key first? ^_^

Also, is there a way to use your program to capture the windows key so that it does not open the StartMenu, but still works for the hot keys? I tried capturing using VK_LWIN but it blocked the hot keys that used CK_WIN.

I will be trying to set approximately 90 hot keys, including the blocked ones (Win+K, Win+U). I will reply to this thread with my findings.

Again, thank you, this is a vast improvement.

BTH

Hi, BrammerTheHammer. I completely rewrote this library. Now she is working as I thought from the beginning. I suggest first to read the comments in the file. I have made many changes and additions.

;)

Share this post


Link to post
Share on other sites

Posted

Looks like very strong UDF.  :D

But internal code is quite complicated.

Thanks for sharing anyway Yashied.

Share this post


Link to post
Share on other sites

Posted

Looks like very strong UDF.  :D

But internal code is quite complicated.

Thanks for sharing anyway Yashied.

Thank you, Zedna. I tried to ease this UDF, but most have no choice (if only at the expense of his work). I wrote a long time it, and then repairs, and then completely rewrote this library, and now it works exactly as I designed from the outset.

:D

Share this post


Link to post
Share on other sites

Posted

I have no use for it now but I like such educative scripts

where you can learn something new and see what all is possible to do in Win32API/AutoIt :-)

Share this post


Link to post
Share on other sites

Posted

Thanks, Works Great!

Share this post


Link to post
Share on other sites

Posted

Looks great.

Share this post


Link to post
Share on other sites

Posted

Can i set on textbox or editbox? (Like when input something, press enter go to function)

Share this post


Link to post
Share on other sites

Posted

#Include <HotKey.au3>

Global Const $VK_RETURN = 0x0D

Global $Edit, $hEdit

GUICreate('Test', 400, 400)
$Edit = GUICtrlCreateEdit('', 10, 10, 380, 380)
$hEdit = GUICtrlGetHandle($Edit)
GUISetState()

_HotKeyAssign($VK_RETURN, 'MyFunc', $HK_FLAG_NOBLOCKHOTKEY)

Do
Until GUIGetMsg() = -3

Func MyFunc()
    If _WinAPI_GetFocus() <> $hEdit Then
        Return
    EndIf
    ConsoleWrite('OK' & @CR)
EndFunc   ;==>MyFunc

Share this post


Link to post
Share on other sites

Posted

#Include <HotKey.au3>

Global Const $VK_RETURN = 0x0D

Global $Edit, $hEdit

GUICreate('Test', 400, 400)
$Edit = GUICtrlCreateEdit('', 10, 10, 380, 380)
$hEdit = GUICtrlGetHandle($Edit)
GUISetState()

_HotKeyAssign($VK_RETURN, 'MyFunc', $HK_FLAG_NOBLOCKHOTKEY)

Do
Until GUIGetMsg() = -3

Func MyFunc()
    If _WinAPI_GetFocus() <> $hEdit Then
        Return
    EndIf
    ConsoleWrite('OK' & @CR)
EndFunc   ;==>MyFunc
Thanks, but i tried your example but nothing happened, console has no output.

Share this post


Link to post
Share on other sites

Posted

nice... i've been working allday just to find a solution for setting hotkey to a specified window, and heres the answer. ty

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