Jump to content

1 function, two macros. Can it be done?


Recommended Posts

Hello all,

I found a couple of threads that touched in this issue - but workarounds were always found before anyone could get around to answering the original request. So, I'll ask here:

I've got a function that is going to be handling the administration of a lot of stuff. The GUI I have running is calling this function on 17 different GUI button presses and what will eventually be around 50 hotkeys. Before you think it's a keylogger, it isn't. The GUI is representing one of my companies keypads and controlling it via a serial connection. As it stands, if I want to enter an alphanumeric character in the keypad, I can do so by pressing a button. It would be MUCH simpler to be able to type everything into the keypad.

Anyway, each hotkey press and GUI button press is calling the same function. The function sends the key to the keypad and writes the function call (_Reg_PressKey()) and its keypress to a listview that records the script. The beginning of the function code looks like this:

Func bKeypadButtons()
Select
  Case @GUI_CtrlId = $bTop1 Or @HotKeyPressed = "{NUMPADDIV}"
   _Reg_PressKey("T1")
   __WriteKeypress("T1")
  Case @GUI_CtrlId = $bTop2 Or @HotKeyPressed = "{NUMPADMULT}"
   _Reg_PressKey("T2")
   __WriteKeypress("T2")
  Case @GUI_CtrlId = $bTop3 Or @HotKeyPressed = "{NUMPADSUB}"
   _Reg_PressKey("T3")
   __WriteKeypress("T3")
  Case @GUI_CtrlId = $bTop4 Or @HotKeyPressed = "{NUMPADADD}"
   _Reg_PressKey("T4")
   __WriteKeypress("T4")
  Case @GUI_CtrlId = $bKey1 Or @HotKeyPressed = "{NUMPAD1}"
   _Reg_PressKey("1")
   __WriteKeypress("1")
  Case @GUI_CtrlId = $bKey2 Or @HotKeyPressed = "{NUMPAD2}"
   _Reg_PressKey("2")
   __WriteKeypress("2")
  Case @GUI_CtrlId = $bKey3 Or @HotKeyPressed = "{NUMPAD3}"
   _Reg_PressKey("3")
   __WriteKeypress("3")
  Case @GUI_CtrlId = $bKey4 Or @HotKeyPressed = "{NUMPAD4}"
   _Reg_PressKey("4")
   __WriteKeypress("4")
;ETC. This goes on for the enter numberpad and most of the alpha keys

As it stands, it seems that I can not use both @GUI_CtrlID and @HotKeyPressed in the same function. This would save a significant amount of code and greatly increase the efficiency if it were possible.

Any ideas?

Thanks,

Fett

[sub]My UDF[/sub][sub] - Basics and Time extensions. Great for those new at AutoIt, also contains some powerful time extensions for pros.[/sub][sub]ScrabbleIt[/sub][sub] - Scrabble done in pure AutoIt. (In Progress)[/sub][sub]Nerd Party Extreme | My Portfolio | [email="fett8802@gmail.com"]Contact Me[/email][/sub]
Link to comment
Share on other sites

If you try it with one or the other macro, by itself, does it work? If not, which one isn't working?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Yea, sorry, I wasn't very clear about that. If I press a GUI button with the above function, it seems to work normally. However, if I start by pressing a HotKey, it creates a fatal error with SciTE returning:

C:Documents and SettingskmillsDesktopScriptsProjectsAutoIt RegressionXT PanelsARP.au3 (169) : ==> Unknown macro.:

Case @GUI_CtrlId = $bTop1 Or @HotKeyPressed = "{NUMPADDIV}"

Case ^ ERROR

Also, if I press buttons and hit hotkeys back to back it begins creating weird returns. It seems that the @HotKeyPressed returns the previous returned hotkey, regardless of if the HotKey function actually called the function.

I actually realised as I was typing this response that I'm probably going to have to split it into two functions regardless. Unless there is a way to default them both at the end of the function?

Sorry for the waste of time, BrewMan.

Fett

Edited by fett8802
[sub]My UDF[/sub][sub] - Basics and Time extensions. Great for those new at AutoIt, also contains some powerful time extensions for pros.[/sub][sub]ScrabbleIt[/sub][sub] - Scrabble done in pure AutoIt. (In Progress)[/sub][sub]Nerd Party Extreme | My Portfolio | [email="fett8802@gmail.com"]Contact Me[/email][/sub]
Link to comment
Share on other sites

  • Moderators

fett8802,

That was fun - I do not use OnEvent mode much and was not too sure how to proceed! :doh:

Two problems:

- 1. You need to have already pressed a control to get a value in the @GUI_CtrlId macro, so you cannot test it until a control has fired.

- 2. The previous value remains in either macro, so you cannot do a test as you have tried to do.

Here is something which does seem to work. Given the above explanation you should be able to see why I have done it this way - but please do ask if not: :bye:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Global $iLast_ID, $iLast_HotKey = 0

Opt("GUIOnEventMode", 1)

HotKeySet("1", "Function")
HotKeySet("2", "Function")
HotKeySet("3", "Function")
HotKeySet("4", "Function")

$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$cButton_1 = GUICtrlCreateButton("1", 10, 10, 80, 30)
GUICtrlSetOnEvent(-1, "Function")
$cButton_2 = GUICtrlCreateButton("2", 10, 50, 80, 30)
GUICtrlSetOnEvent(-1, "Function")
$cButton_3 = GUICtrlCreateButton("3", 10, 90, 80, 30)
GUICtrlSetOnEvent(-1, "Function")
$cButton_4 = GUICtrlCreateButton("4", 10, 130, 80, 30)
GUICtrlSetOnEvent(-1, "Function")

$cDummy = GUICtrlCreateDummy()
GUICtrlSetOnEvent(-1, "Dummy")

GUISetState()

; Fil the macro so we have something to test against
GUICtrlSendToDummy($cDummy)
$iLast_ID = @GUI_CtrlId

While 1
    Sleep(10)
WEnd

Func Function()

    If @GUI_CtrlId <> $iLast_ID Then
        MsgBox(0, "Control", "You pressed ControlID: " & @GUI_CtrlId)
    EndIf
    If @HotKeyPressed <> $iLast_HotKey Then
        MsgBox(0, "HotKey", "You pressed HotKey: " & @HotKeyPressed)
    EndIf

    $iLast_ID = @GUI_CtrlId
    $iLast_HotKey = @HotKeyPressed

EndFunc

Func Dummy()
EndFunc

Func _Exit()
    Exit
EndFunc

I hope it helps. :oops:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I'm not sure why you're getting the unknown macro problem, unless you aren't using OnEvent mode and you're using the message loop mode.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Moderators

BrewManNH,

Run this and you will see - no control fired = unknown macro when you run: :bye:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$cButton_1 = GUICtrlCreateButton("1", 10, 10, 80, 30)
GUICtrlSetOnEvent(-1, "Function")

GUISetState()

; Let us try to read the macro here before a control is fired
ConsoleWrite(@GUI_CtrlId & @CRLF)

While 1
    Sleep(10)
WEnd

Func Function()
EndFunc

Func _Exit()
    Exit
EndFunc

And I get:

"M:ProgramAu3 Scriptsfred4.au3" (16) : ==> Unknown macro.:
ConsoleWrite(@GUI_CtrlId & @CRLF)
ConsoleWrite(^ ERROR

You need at least one control to have fired to set a value in the macro - no previous firing and the macro does not yet exist it seems. :oops:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Something like this should work :oops:...

Func _GUI_CtrlId_Wrapper()
    bKeypadButtons(@GUI_CtrlId)
EndFunc   ;==>_GUI_CtrlId_Wrapper

Func _HotKeyPressed_Wrapper()
    bKeypadButtons("", @HotKeyPressed)
EndFunc   ;==>_HotKeyPressed_Wrapper

Func bKeypadButtons($GUI_CtrlId = "", $HotKeyPressed = "")
    Select
        Case $GUI_CtrlId = $bTop1 Or $HotKeyPressed = "{NUMPADDIV}"
            _Reg_PressKey("T1")
            __WriteKeypress("T1")
        Case $GUI_CtrlId = $bTop2 Or $HotKeyPressed = "{NUMPADMULT}"
            _Reg_PressKey("T2")
            __WriteKeypress("T2")
            ; etc...
    EndSelect
EndFunc   ;==>bKeypadButtons
Link to comment
Share on other sites

Excellent KaFu! That will accomplish exactly what I need, and only in 6 extra lines of code instead of another 100. Thank you very much! Also, thanks to Melba and BrewMan for their input!

- Fett

Edited by fett8802
[sub]My UDF[/sub][sub] - Basics and Time extensions. Great for those new at AutoIt, also contains some powerful time extensions for pros.[/sub][sub]ScrabbleIt[/sub][sub] - Scrabble done in pure AutoIt. (In Progress)[/sub][sub]Nerd Party Extreme | My Portfolio | [email="fett8802@gmail.com"]Contact Me[/email][/sub]
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

×
×
  • Create New...