Jump to content

HotKey UDF


Yashied
 Share

Recommended Posts

Cool!

Thanks i'll try it.

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

  • 1 month later...

Thank you.

But the hotkeys do not work when they meet a loop in a funtion. Just simply try this:

#Include <HotKey.au3>


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

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

; Assign "CTRL-ESC" with Quit()
_HotKeyAssign(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!')
    while 1
        Sleep(100)
    WEnd
    
   
EndFunc   ;==>Message

Func Quit()
    Exit
EndFunc   ;==>Quit
Link to comment
Share on other sites

Are you sure that this should work? Have you read the description about $HK_FLAG_NOOVERLAPCALL flag? Be sure to read. Anyway, you did a loop where it can not be done. This function is an interrupt for the main program, you must create another code design.

Edited by Yashied
Link to comment
Share on other sites

Are you sure that this should work? Have you read the description about $HK_FLAG_NOOVERLAPCALL flag? Be sure to read. Anyway, you did a loop where it can not be done. This function is an interrupt for the main program, you must create another code design.

Sorry about that. Im just thinking if that can work, that will be great! ^^

And i got my new code design. Thanks.

#Include <HotKey.au3>


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


_HotKeyAssign($VK_F12, 'Start')
_HotKeyAssign($VK_F11, 'Pause')

; Assign "CTRL-ESC" with Quit()
_HotKeyAssign(BitOR($CK_CONTROL, $VK_ESCAPE), 'Quit')
Global $flag = False
Global $counter = 1

While 1
    if $flag then 
        MainFunc()
    Else
        Sleep(10)
    EndIf
WEnd


Func Pause()
    $flag = False
EndFunc

Func Start()
    ;init()
    $flag = true
EndFunc

Func MainFunc()
    Sleep(100)
    $counter=$counter+1
    ToolTip($counter)
EndFunc   ;==>Message

Func Quit()
    exit
EndFunc   ;==>Quit
Link to comment
Share on other sites

#Include <HotKey.au3>


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


_HotKeyAssign($VK_F12, 'Start')
_HotKeyAssign($VK_F11, 'Pause')

; Assign "CTRL-ESC" with Quit()
_HotKeyAssign(BitOR($CK_CONTROL, $VK_ESCAPE), 'Quit')
Global $flag = False
Global $counter = 1

While 1
    if $flag then 
        MainFunc()
    Else
        Sleep(10)
    EndIf
WEnd


Func Pause()
    $flag = False
EndFunc

Func Start()
    ;init()
    $flag = true
EndFunc

Func MainFunc()
    Sleep(100)
    $counter=$counter+1
    ToolTip($counter)
EndFunc   ;==>Message

Func Quit()
    exit
EndFunc   ;==>Quit

This is the right approach.
Link to comment
Share on other sites

  • 3 weeks later...

@Yashied

Love the UDF so far! One thing, I have a specific window I am assigning hotkeys for, like this:

Opt("WinTitleMatchMode", 2)

_HotKeyAssign($VK_F1, 'CWDate', $HK_FLAG_EXTENDEDCALL, 'ConnectWise')
_HotKeyAssign($VK_F2, 'CWDate', $HK_FLAG_EXTENDEDCALL, 'ConnectWise') 
_HotKeyAssign($VK_F3, 'CWDate', $HK_FLAG_EXTENDEDCALL, 'ConnectWise')
_HotKeyAssign($VK_F4, 'CWDate', $HK_FLAG_EXTENDEDCALL, 'ConnectWise')

; For testing
Func CWDate($iKey)
    MsgBox(0, "", Hex($iKey, 4))
EndFunc

If I create a notepad document with "ConnectWise" in the title, it works fine, but the hotkey does not run at all for the program I want to use it for. Here's the program's window info:

>>>> Window <<<<
Title:  My Activities - ConnectWise v8.0
Class:  WindowsForms10.window.8.app.0.378734a
Position:   1912, 86
Size:   1296, 1000
Style:  0x17CF0000
ExStyle:    0x00050100
Handle: 0x0000000000040840

>>>> Control <<<<
Class:  WindowsForms10.window.8.app.0.378734a
Instance:   93
ClassnameNN:    WindowsForms10.window.8.app.0.378734a93
Advanced (Class):   [CLASS:WindowsForms10.window.8.app.0.378734a; INSTANCE:93]
ID: 2689752
Text:   
Position:   158, 172
Size:   1110, 777
ControlClick Coords:    403, 278
Style:  0x56010000
ExStyle:    0x00010000
Handle: 0x0000000000290AD8

Running MsgBox(0, "", WinActive("ConnectWise")) as a test while on the window returns "1" like it should, and WinGetTitle("[ACTIVE]") returns "My Activities - ConnectWise v8.0" like it should, so I'm not sure why the hotkey is not working.

I am running the latest (3.3.0.0), and I'm on Windows 7 x64. Any ideas?

EDIT: It appears the issue is not the above code, but hotkeys for this program *in general*... I cannot get any hotkeys to respond on this window. I will investigate to see what else I can find, although if you have ideas, I'd love to hear them!

Edited by fisofo
Link to comment
Share on other sites

I figured out that the main issue was that of permissions, my lower-privileged script could not execute on higher privileged windows.

Despite that, I have run into problems with F1 - F4 not running the hotkeys assigned to them using this script when certain windows are open... I'm not sure why yet, but as soon as I use hotkeyset instead, they work fine. I'll have to dig into it more some other time as I've spent way too much time on this already, but I'll let you know if I ever figure out why... here's a snipet of the code I was testing though:

;~ _HotKeyAssign($VK_F1, 'CWDate')
;~ _HotKeyAssign($VK_F2, 'CWDate')
;~ _HotKeyAssign($VK_F3, 'CWDate')
;~ _HotKeyAssign($VK_F4, 'CWDate')
HotKeySet("{F1}", "CWDate")
HotKeySet("{F2}", "CWDate")
HotKeySet("{F3}", "CWDate")
HotKeySet("{F4}", "CWDate")

The hotkeyset work all the time, the hotkeyassign only seem to work on specific windows for just these specific keys (F1 to F4)

Link to comment
Share on other sites

  • 1 month later...

Why is there no "del" key?

#include <HotKey.au3>

Global Const $VK_DELETE = 0x2E

_HotKeyAssign(BitOR($CK_CONTROL,$CK_ALT ,$VK_DELETE), 'MyFunc')

While 1
    Sleep(10)
WEnd

Func MyFunc()
    MsgBox(0, 'Test', 'pressed!')
EndFunc   ;==>MyFunc
Link to comment
Share on other sites

Why is there no "del" key?

#include <HotKey.au3>

Global Const $VK_DELETE = 0x2E

_HotKeyAssign(BitOR($CK_CONTROL,$CK_ALT ,$VK_DELETE), 'MyFunc')

While 1
    Sleep(10)
WEnd

Func MyFunc()
    MsgBox(0, 'Test', 'pressed!')
EndFunc   ;==>MyFunc

You can not block CTRL-ALT-DEL.
Link to comment
Share on other sites

Link to comment
Share on other sites

Fyi, I'm guessing you know this, but in case you don't, compiling a script on the most recent autoit gives me this:

HotKey.au3(194,56) : ERROR: Opt() called with illegal argument 1: "OnExitFunc"
Global $OnHotKeyExit = Opt('OnExitFunc', 'OnHotKeyExit')

EDIT: seems to work fine otherwise though!

Edited by fisofo
Link to comment
Share on other sites

  • 5 weeks later...

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