Jump to content

[Solved] How to set a hotkey


Recommended Posts

HI, I have 2 hotkeys,

I would like to set those hotkeys when a window is active once & if window is not active, then release all hotkeys once.

I would also like to set the hotkeys like this:

_Hotkeys_activate_on_winactive(' - Mozilla Firefox', "{F7}", "_FF_Global_GO_Youtube", 'enabled', 'If FF is active, go youtube')

But the problem is, that if I declare 1 hotkey & then say this hotkey was now declared, it wont work, because variables will conflict. Of Course I can do a separate variable for each hotkey but if I make ton of hotkeys it will get really messy.

is there any way to keep this short & clean ?

All my hotkeys are also stored in the array like this.:

To put the hotkey into an array I use this code:

_HotKeySet("{F7}", "_FF_Glob_GO_Youtube", 'enabled', 'If FF is active, go youtube')

Posted Image

While 1
    Sleep(100)
    _IF_Winactive_events()
WEnd

Func _IF_Winactive_events()
;~  ---------------------------------------------------------
        _Hotkeys_activate_on_winactive(' - Mozilla Firefox', "{F7}", "_FF_Global_GO_Youtube", 'enabled', 'If FF is active, go youtube')
        _Hotkeys_activate_on_winactive(' - Mozilla Firefox', "{F6}", "_FF_Global_GO_Watchanimeon", 'enabled', 'If FF is active, go Watchanimeon')
;~  ---------------------------------------------------------
EndFunc


Func _Hotkeys_activate_on_winactive($Window, $HOTKEY, $FUNC, $ENABLED_DISABLED_STATE, $DESCRIPTION)

    If WinActive($Window) And $hotkeys_registered = False Then
        _HotKeySet($HOTKEY, $FUNC, $ENABLED_DISABLED_STATE, $DESCRIPTION)
        $hotkeys_registered = True
        ConsoleWrite('Hotkeys ReEgistered' & @CRLF)
    EndIf

    If Not WinActive($Window) And $hotkeys_registered = True Then
        HotKeySet($HOTKEY) ; release hotkey
        $hotkeys_registered = False
        ConsoleWrite('Hotkeys UnRegistered' & @CRLF)
    EndIf

EndFunc
Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

But the problem is, that if I declare 1 hotkey & then say this hotkey was now declared, it wont work, because variables will conflict.

That bit doesn't make sense to me. Maybe I understand what you want though. I wouldn't worry about things so much, I would do it like this

HotKeySet("{F7}", "goUT")
HotKeySet(... ;etc


Func goUT()
    If StringInStr(WinGetTitle(""), "Mozilla Firefox") Then
        dotheF7Thing()
    Else
        HotKeySet("{F7}")
        Send("{F7" })
        Sleep(50);?
        HotKeySet("{F7}", "goUT")
    EndIf
EndFunc

In other words I don't see the need to keep registering and unregistering the hotkeys.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Maybe I understand what you want though...

But I might have say 100 hotkeys eventually for each site & more sets of hotkeys to work within the current site. For example global ff hotkey for youtube is F7 & then I have F1-F11 to navigate to y.tube profile, favorites,inbox etc, & I might make 1 script for every site I visit. So eventually it might be load of hotkeys.

So I was worried it will slow down my script & consume more resources.

So it is okey if I just do it like this?:

If winactive('firefox')
Hotkeyset(F7,'tuve')
Hotkeyset(F6, fav)
...100more hotkeys
else
Hotkeyset(F7)
Hotkeyset(F7)
endif
Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

But I might have say 100 hotkeys eventually for each site & more sets of hotkeys to work within the current site. For example global ff hotkey for youtube is F7 & then I have F1-F11 to navigate to y.tube profile, favorites,inbox etc, & I might make 1 script for every site I visit. So eventually it might be load of hotkeys.

So I was worried it will slow down my script & consume more resources.

So it is okey if I just do it like this?:

If winactive('firefox')
Hotkeyset(F7,'tuve')
Hotkeyset(F6, fav)
...100more hotkeys
else
Hotkeyset(F7)
Hotkeyset(F7)
endif

Possibly I don't understand (very common) or I don't agree. If you do it the way you show then you have to keep checking for the current application and switch keys as needed. If you have each hot key call one function then you only need to make a check when a hotkey is pressed and I think the code can be much simpler which can mean faster.

HotKeySet("{F7}", "AllHK")
HotKeySet("{F11}", "AllHK");set all to same func

$F7Jobs = StringSplit("title1:FuncZV|title2:func36", '|');as many as you want
$F11Jobs = ...........

Func allHK()

    Local $WA = WinGetTitle("")
    Local $app


    $app = NumberFromTitle($WA);some func to decide app

    If $app <> 0 Then
        Switch @HotKeyPressed
            Case "F7"
                Call(StringTrimLeft($F7Jobs[$app], StringInStr($F7Jobs[$app], ':'))
            Case "F11"
                ;

        EndSwitch

    Else
        HotKeySet(@HotKeyPressed)
        Send(@HotKeyPressed)
        HotKeySet(@HotKeyPressed,"allHK")
    EndIf

EndFunc   ;==>allHK
Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

...

I do not understand your code.

So you are saying, put window names into an array like this:(col 4) And for each new winactive we go through this array & enable hotkeys that have corresponding window title in stringinstring?

Im going to try this.

Posted Image

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

I do not understand your code.

So you are saying, put window names into an array like this:(col 4) And for each new winactive we go through this array & enable hotkeys that have corresponding window title in stringinstring?

Im going to try this.

Posted Image

I rarely understand my code either.

The basic idea is that when a hot key is pressed you call a function which decides which app is running and then calls the relevant function for that app and that hot key.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I rarely understand my code either.

The basic idea is that when a hot key is pressed you call a function which decides which app is running and then calls the relevant function for that app and that hot key.

Would you be willing to take a look at my code? I almost got it to work but there is 1 little problem, it wont register my hotkeys. MY debug says in the console, that the keys are registered. but they are not. Everything else works tho.

Step 1) Run: Hotkey Manager 1.0.au3

Step 2) Activate Firefox & press F7

Step 3) In console it should say: Called Function:=> _FF_Glob_GO_Youtube (This will happen it the hotkeys are registered.)

Step 4) Press Esc or console will have unnecessary info

Files that may be causing he issue:

Includes\Hotkeyset.au3
Includes\Winactive_not_active_events.au3

Winactive_not_active_events.au3

This is where I say, ff is active, now register all hotkeys that have Mozilla Firefox entry in the array. Basically this just collects info like what window is active & say what hotkeys should be activated, deactivated.

Hotkeyset.au3

Hire we actually set those hotkeys.

Pressing F4 will show you all hotkeys in the array. Do not press Insert, it will close active window!

Hire are the files:

~link removed~

Edit, nvm & thank you I fixed it It turns out that the Function names were different. basically hotkey was calling a function that did not exist.

And this is how I solved the winactive issue, like you said , this worked, hire is a sample if anyone will have same issue

#Include <Array.au3>

Global  $ar_hotkeys[1][5]

_HotKeySet("{F6}", "_FF_Glob_GO_Watchanimeon",  'disabled', 'If FF is active, go Watchanimeon', '- Mozilla Firefox')
_HotKeySet("{F7}", "_FF_Glob_GO_Youtube",       'disabled', 'If FF is active, go youtube', '- Mozilla Firefox')
_HotKeySet("!{esc}", "_exit",                   'enabled', 'Exit', 'global')

Global $Lastwindow = ''

While 1

    $title = WinGetTitle("[active]")

    ;## Register hotkeys for this window
    If $Lastwindow <> $title Then
        If $title <> '' Then
            $Lastwindow = $title
            ConsoleWrite($title & @CRLF)
            _Unregister_hotkeys()
            _Register_hotkeys($title)
        EndIf
    EndIf


Sleep(150)
WEnd

Func _Register_hotkeys($title)
    For $i=1 To $ar_hotkeys[0][0]
        If StringInStr($title, $ar_hotkeys[$i][4]) Then
            _HotKeySet($ar_hotkeys[$i][0], $ar_hotkeys[$i][1], 'enabled', $ar_hotkeys[$i][3], $ar_hotkeys[$i][4])
        EndIf
    Next
;~ _ArrayDisplay($ar_hotkeys, "$ar_hotkeys")
EndFunc


Func _Unregister_hotkeys()
    ;## Unregister all hotkeys but global for this window
    For $i=1 To $ar_hotkeys[0][0]
        If $ar_hotkeys[$i][4] <> 'global' Then
            _HotKeySet($ar_hotkeys[$i][0], $ar_hotkeys[$i][1], 'disabled', $ar_hotkeys[$i][3], $ar_hotkeys[$i][4])
        EndIf
    Next
;~ _ArrayDisplay($ar_hotkeys, "$ar_hotkeys")
EndFunc



Func _exit()
    Exit
EndFunc


Func _FF_Glob_GO_Youtube()
    ConsoleWrite('_FF_Glob_GO_Youtube' & @CRLF)
EndFunc

Func _FF_Glob_GO_Watchanimeon()
    ConsoleWrite('_FF_Glob_GO_Watchanimeon' & @CRLF)
EndFunc



Func _HotKeySet($HOTKEY, $FUNC, $ENABLED_DISABLED_STATE, $DESCRIPTION, $WINDOW)

;## We can have multiple functions assigned to a single hotkey, but only 1 hotkey can be enabled at the same time.!
;## We can also have multiple hotkeys assigned to a single function

;~ If same hotkey & func exist, modify it
    For $i=1 To $ar_hotkeys[0][0]
        If $ar_hotkeys[$i][0] = $HOTKEY And $ar_hotkeys[$i][1] = $FUNC Then ; enable hotkey
            $ar_hotkeys[$i][0] = $HOTKEY
            $ar_hotkeys[$i][1] = $FUNC
            $ar_hotkeys[$i][2] = $ENABLED_DISABLED_STATE
            $ar_hotkeys[$i][3] = $DESCRIPTION
            $ar_hotkeys[$i][4] = $WINDOW

            If $ENABLED_DISABLED_STATE == 'disabled' Then
                HotKeySet($HOTKEY)
            Else
                HotKeySet($HOTKEY, $FUNC)
            EndIf
;~          ConsoleWrite('Hotkey '&$HOTKEY&' exists' & @CRLF)
            Return
        EndIf
    Next

    ;## add 1 empty row in the end of the array
        $iRow   = UBound($ar_hotkeys)       ;Nr of rows in current array
        $iCol   = UBound($ar_hotkeys, 2)    ;Nr of coluymns in current array
        ReDim $ar_hotkeys[$iRow + 1][$iCol] ;add more rows +1 ; ReDim $ARRAY[$iRow + 1][$iCol + 1] - Adds both
        $ar_hotkeys[0][0] = $iRow           ;new array size in [0][0]

    ;## Add values into the last row
        $lastrow = $ar_hotkeys[0][0]
        $ar_hotkeys[$lastrow][0] = $HOTKEY
        $ar_hotkeys[$lastrow][1] = $FUNC
        $ar_hotkeys[$lastrow][2] = $ENABLED_DISABLED_STATE
        $ar_hotkeys[$lastrow][3] = $DESCRIPTION
        $ar_hotkeys[$lastrow][4] = $WINDOW
        HotKeySet($HOTKEY, $FUNC)
EndFunc
Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

Would you be willing to take a look at my code? I almost got it to work but there is 1 little problem, it wont register my hotkeys. MY debug says in the console, that the keys are registered. but they are not. Everything else works tho.

Step 1) Run: Hotkey Manager 1.0.au3

Step 2) Activate Firefox & press F7

Step 3) In console it should say: Called Function:=> _FF_Glob_GO_Youtube (This will happen it the hotkeys are registered.)

Step 4) Press Esc or console will have unnecessary info

Files that may be causing he issue:

Includes\Hotkeyset.au3
Includes\Winactive_not_active_events.au3

Winactive_not_active_events.au3

This is where I say, ff is active, now register all hotkeys that have Mozilla Firefox entry in the array. Basically this just collects info like what window is active & say what hotkeys should be activated, deactivated.

Hotkeyset.au3

Hire we actually set those hotkeys.

Pressing F4 will show you all hotkeys in the array. Do not press Insert, it will close active window!

Hire are the files:

~link removed~

Edit, nvm & thank you I fixed it It turns out that the Function names were different. basically hotkey was calling a function that did not exist.

And this is how I solved the winactive issue, like you said , this worked, hire is a sample if anyone will have same issue

#Include <Array.au3>

Global  $ar_hotkeys[1][5]

_HotKeySet("{F6}", "_FF_Glob_GO_Watchanimeon",  'disabled', 'If FF is active, go Watchanimeon', '- Mozilla Firefox')
_HotKeySet("{F7}", "_FF_Glob_GO_Youtube",       'disabled', 'If FF is active, go youtube', '- Mozilla Firefox')
_HotKeySet("!{esc}", "_exit",                   'enabled', 'Exit', 'global')

Global $Lastwindow = ''

While 1

    $title = WinGetTitle("[active]")

    ;## Register hotkeys for this window
    If $Lastwindow <> $title Then
        If $title <> '' Then
            $Lastwindow = $title
            ConsoleWrite($title & @CRLF)
            _Unregister_hotkeys()
            _Register_hotkeys($title)
        EndIf
    EndIf


Sleep(150)
WEnd

Func _Register_hotkeys($title)
    For $i=1 To $ar_hotkeys[0][0]
        If StringInStr($title, $ar_hotkeys[$i][4]) Then
            _HotKeySet($ar_hotkeys[$i][0], $ar_hotkeys[$i][1], 'enabled', $ar_hotkeys[$i][3], $ar_hotkeys[$i][4])
        EndIf
    Next
;~ _ArrayDisplay($ar_hotkeys, "$ar_hotkeys")
EndFunc


Func _Unregister_hotkeys()
    ;## Unregister all hotkeys but global for this window
    For $i=1 To $ar_hotkeys[0][0]
        If $ar_hotkeys[$i][4] <> 'global' Then
            _HotKeySet($ar_hotkeys[$i][0], $ar_hotkeys[$i][1], 'disabled', $ar_hotkeys[$i][3], $ar_hotkeys[$i][4])
        EndIf
    Next
;~ _ArrayDisplay($ar_hotkeys, "$ar_hotkeys")
EndFunc



Func _exit()
    Exit
EndFunc


Func _FF_Glob_GO_Youtube()
    ConsoleWrite('_FF_Glob_GO_Youtube' & @CRLF)
EndFunc

Func _FF_Glob_GO_Watchanimeon()
    ConsoleWrite('_FF_Glob_GO_Watchanimeon' & @CRLF)
EndFunc



Func _HotKeySet($HOTKEY, $FUNC, $ENABLED_DISABLED_STATE, $DESCRIPTION, $WINDOW)

;## We can have multiple functions assigned to a single hotkey, but only 1 hotkey can be enabled at the same time.!
;## We can also have multiple hotkeys assigned to a single function

;~ If same hotkey & func exist, modify it
    For $i=1 To $ar_hotkeys[0][0]
        If $ar_hotkeys[$i][0] = $HOTKEY And $ar_hotkeys[$i][1] = $FUNC Then ; enable hotkey
            $ar_hotkeys[$i][0] = $HOTKEY
            $ar_hotkeys[$i][1] = $FUNC
            $ar_hotkeys[$i][2] = $ENABLED_DISABLED_STATE
            $ar_hotkeys[$i][3] = $DESCRIPTION
            $ar_hotkeys[$i][4] = $WINDOW

            If $ENABLED_DISABLED_STATE == 'disabled' Then
                HotKeySet($HOTKEY)
            Else
                HotKeySet($HOTKEY, $FUNC)
            EndIf
;~          ConsoleWrite('Hotkey '&$HOTKEY&' exists' & @CRLF)
            Return
        EndIf
    Next

    ;## add 1 empty row in the end of the array
        $iRow   = UBound($ar_hotkeys)       ;Nr of rows in current array
        $iCol   = UBound($ar_hotkeys, 2)    ;Nr of coluymns in current array
        ReDim $ar_hotkeys[$iRow + 1][$iCol] ;add more rows +1 ; ReDim $ARRAY[$iRow + 1][$iCol + 1] - Adds both
        $ar_hotkeys[0][0] = $iRow           ;new array size in [0][0]

    ;## Add values into the last row
        $lastrow = $ar_hotkeys[0][0]
        $ar_hotkeys[$lastrow][0] = $HOTKEY
        $ar_hotkeys[$lastrow][1] = $FUNC
        $ar_hotkeys[$lastrow][2] = $ENABLED_DISABLED_STATE
        $ar_hotkeys[$lastrow][3] = $DESCRIPTION
        $ar_hotkeys[$lastrow][4] = $WINDOW
        HotKeySet($HOTKEY, $FUNC)
EndFunc

I don't see the file "Hotkey Manager 1.0.au3" in the download. Should I?
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I don't see the file "Hotkey Manager 1.0.au3" in the download. Should I?

no its ok Hotkeys work I found the problem, its just the hotkeys were calling a func that did not exist & autoit did not give me error.

Now ill just rewrite the way I set hotkeys, so I can set hotkey & then use different hotkeys to call same function, but with different parameters, like navigate to a different url

And then I can do some beta testing

really thanx for ur help

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

no its ok Hotkeys work I found the problem, its just the hotkeys were calling a func that did not exist & autoit did not give me error.

Now ill just rewrite the way I set hotkeys, so I can set hotkey & then use different hotkeys to call same function, but with different parameters, like navigate to a different url

And then I can do some beta testing

really thanx for ur help

OK.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...