Jump to content

Function tell difference between Hotkey & GUI call


 Share

Recommended Posts

Hi,

I have a piece of code that would work most smoothly if I could somehow have the Function() targeted by a GUI menu AND by a hotkey know which one of those items called it. Can it be done? I do have lots of ways that I could work -around- this problem so in case it cannot be done it is no biggie.. Still, I have a feeling I'm missing something stupendously obvious.. :whistle:

Cheers,

Sunaj

;pseudo code

HotKeySet("{ESC}", "fLaunch")

GUICtrlCreateMenuitem($yep, $OptionsContext)

GUICtrlSetOnEvent($yep, "fLaunch")

Func fLaunch()
    If GUI then 
        x
    ElseIf Hotkey Then
        y
    EndIf
EndFunc
Link to comment
Share on other sites

Hi,

I have a piece of code that would work most smoothly if I could somehow have the Function() targeted by a GUI menu AND by a hotkey know which one of those items called it. Can it be done? I do have lots of ways that I could work -around- this problem so in case it cannot be done it is no biggie.. Still, I have a feeling I'm missing something stupendously obvious.. :whistle:

Cheers,

Sunaj

;pseudo code

HotKeySet("{ESC}", "fLaunch")

GUICtrlCreateMenuitem($yep, $OptionsContext)

GUICtrlSetOnEvent($yep, "fLaunch")

Func fLaunch()
    If GUI then 
        x
    ElseIf Hotkey Then
        y
    EndIf
EndFunc
Not sure if this is what you are looking for, but what if you make Func fLaunch ($callType) and make the hotkey call with another parameter than the GUI call?

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Well, yes, that is what I'm looking for - only thing is that hotkey associated functions and GUICtrlSetOnEvent cannot pass parameters natively (see: http://www.autoitscript.com/forum/index.php?showtopic=15881). So.. not an option (unless I do a workaround). To reiterate: I want to have the Function() targeted by a GUI menu AND by a hotkey know whether a hotkey or a gui item called it..

Not sure if this is what you are looking for, but what if you make Func fLaunch ($callType) and make the hotkey call with another parameter than the GUI call?

Link to comment
Share on other sites

  • Moderators

;pseudo code
Opt('GUIOnEventMode', 1)
Global $yep = '&Hello', $MenuItem
HotKeySet("{ESC}", "fLaunch")
$GUI = GUICreate('')
GUISetOnEvent(-3, '_ExitNow', $GUI)
$OptionsContext = GUICtrlCreateMenu('...')
$MenuItem = GUICtrlCreateMenuitem($yep, $OptionsContext)
GUICtrlSetOnEvent($MenuItem, "fLaunch")
GUISetState()

While 1
    Sleep(100000)
WEnd

Func fLaunch()
    If @HotKeyPressed = '{ESC}' Then
        MsgBox(64, 'Info', 'Event called Hotkey')
    Else
        MsgBox(64, 'Info', 'Event called by GUI')
    EndIf
    Return
EndFunc

Func _ExitNow()
    Exit
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Hi SmOke_N,

:whistle: - thanks a lot for pointing towards the @HotKeyPressed macro! Sometimes I just stares into the code until blind on the most odd spots! Oh and.. I seriously appreciate the energy you put into helping not just me but seasoned and newbie alike members all across the forum: at the end of it, it is this kinda community spirit that helps make Autoit what is is. GREAT!

Cheers,

Sunaj

;pseudo code
Opt('GUIOnEventMode', 1)
Global $yep = '&Hello', $MenuItem
HotKeySet("{ESC}", "fLaunch")
$GUI = GUICreate('')
GUISetOnEvent(-3, '_ExitNow', $GUI)
$OptionsContext = GUICtrlCreateMenu('...')
$MenuItem = GUICtrlCreateMenuitem($yep, $OptionsContext)
GUICtrlSetOnEvent($MenuItem, "fLaunch")
GUISetState()

While 1
    Sleep(100000)
WEnd

Func fLaunch()
    If @HotKeyPressed = '{ESC}' Then
        MsgBox(64, 'Info', 'Event called Hotkey')
    Else
        MsgBox(64, 'Info', 'Event called by GUI')
    EndIf
    Return
EndFunc

Func _ExitNow()
    Exit
EndFunc
Link to comment
Share on other sites

  • Moderators

Hi SmOke_N,

:whistle: - thanks a lot for pointing towards the @HotKeyPressed macro! Sometimes I just stares into the code until blind on the most odd spots! Oh and.. I seriously appreciate the energy you put into helping not just me but seasoned and newbie alike members all across the forum: at the end of it, it is this kinda community spirit that helps make Autoit what is is. GREAT!

Cheers,

Sunaj

Thanks... there are many here that do that, and many others that are forming the habit... they all deserve that credit, some more so than myself.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Are you sure that works? I tried something similar before and if I called the function via the button first it told me that @HotKeyPressed was undefined.

*Edit: Well there I go making a fool of myself. It works fine. They must have changed something at some point.

Edited by Saunders
Link to comment
Share on other sites

  • Moderators

Are you sure that works? I tried something similar before and if I called the function via the button first it told me that @HotKeyPressed was undefined.

*Edit: Well there I go making a fool of myself. It works fine. They must have changed something at some point.

You could probably manipulate @GUI_CtrlID as well there... I took the path of least resistance :whistle:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

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