Sign in to follow this  
Followers 0
AlainB

How to make a button act like a momentary switch?

10 posts in this topic

Hi,

When you click on a button, the action will start only when you release the button, not when you press on it. On the script below, the buttons are used more like a toggle switch.

Is there a way to make the action start when you press the button and stop when you release it, more like a momentary switch?

Thanks!

Alain

#include <GUIConstantsEx.au3>
Dim $Delay=20

$Dll = DllOpen("inpout32.dll")
DllClose("inpout32.dll")

$Form = GUICreate("",275,150) 
$Button1 = GUICtrlCreatebutton("CCW",55,50,75,50)
$Button2 = GUICtrlCreatebutton("CW",150,50,75,50)
GUISetState(@SW_SHOW) 

While 1
    $msg = GUIGetMsg()

    Switch $msg
        Case $Button1
                $Label = " Running CCW"
                $a=192
                $b=128
                SendSteps()

        Case $Button2
                $Label = "  Running CW"
                $a=64
                $b=0
                SendSteps()

        Case $GUI_EVENT_CLOSE
            DllCall( $Dll, "int", "Out32", "int", 0x378, "int", 0)
            Exit
    EndSwitch
WEnd

func SendSteps()
                GUICtrlCreatelabel($Label,110,120,75,17)
            while 1
                DllCall( $Dll, "int", "Out32", "int", 0x378, "int", $a)
                sleep($Delay)
                DllCall( $Dll, "int", "Out32", "int", 0x378, "int", $b)
                sleep($Delay)
                $msg=GUIGetMsg()
                if $msg=$Button2 or $msg=$Button1 Then 
                    GUICtrlCreatelabel("    Stopped!",110,120,75,17)
                    ExitLoop
                EndIf
            WEnd
EndFunc

Share this post


Link to post
Share on other sites



AlainB,

Wlcome to the AutoIt forum. :>

Her is one way to do what you want:

#include <GUIConstantsEx.au3>

$hGUI = GUICreate("Test", 500, 500)

$hButton = GUICtrlCreateButton("Press", 10, 10, 80, 30)

$hLabel = GUICtrlCreateLabel("Not Pressed", 10, 50, 200, 20)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    ; Look at where the cursor is at this moment
    $aCInfo = GUIGetCursorInfo()
    ; If the button is pressed and we are over the button
    If $aCInfo[2] And $aCInfo[4] = $hButton Then
        If GUICtrlRead($hLabel) <> "Pressed" Then
            GUICtrlSetData($hLabel, "Pressed")
        EndIf
    Else
        If GUICtrlRead($hLabel) <> "Not Pressed" Then
            GUICtrlSetData($hLabel, "Not Pressed")
        EndIf
    EndIf

WEnd

You need the extra checks on the button text to prevent flashing. ;)

Does that help? :unsure:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Here my version:

#include <GUIConstantsEx.au3>
#include <Misc.au3>

Opt("GUIOnEventMode", 1)

Dim $Delay=20

$Form = GUICreate("",275,150)
$Button1 = GUICtrlCreatebutton("CCW",55,50,75,50)
$Button2 = GUICtrlCreatebutton("CW",150,50,75,50)
GUISetState(@SW_SHOW)

$stop = False

GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "Start")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

$dll = DllOpen("user32.dll")

While Sleep(10000)
WEnd

Func Start()
    While Sleep(10) * _IsPressed("01", $dll)
        $aM = GUIGetCursorInfo()
        If $aM[4] <> $Button1 Then ExitLoop
        Prg1()
    WEnd
EndFunc

Func Prg1()
    Local Static $i = 0
    $i += 1
    ConsoleWrite($i & @CRLF)
EndFunc

Func _Exit()
    DllClose($dll)
    Exit
EndFunc

While you press the CCW button it will write to console.

Br,

UEZ

Edit: made some modifications because I didn't like previous version

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

UEZ,

You did not close the DLL on exit! :>

Shame on you! :unsure:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

@Melba23: ;):unsure:

:>

:D

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Thanks both for your fast replies.

Yes I think that this is what I need. I will have to study the scripts a bit.

I read a few times about the console but I dont know what it is and how to use it. Could you explain.

By the way, is there something wrong about loading the DLL in the memory and closing it right away as I am doing in my script?

Thanks!

Edited by AlainB

Share this post


Link to post
Share on other sites

AlainB,

I read a few times about the console but I dont know what it is and how to use it.

In its original meaning it referred to the DOS prompt. In this case it refers to the lower pane of the SciTE editor where you get information on how your script is running or the errors that prevent it from running. The ConsoleWrite function places text there - very useful for debugging. Run this line in SciTE:

ConsoleWrite(@CRLF & "Can you see me?" & @CRLF & @CRLF)

I get the following in my SciTE lower pane (= the console):

>"C:\Program Files\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.exe" /run /prod /ErrorStdOut /in "M:\Program\Au3 Scripts\fred4.au3" /autoit3dir "C:\Program Files\AutoIt3" /UserParams    
+>08:38:04 Starting AutoIt3Wrapper v.2.0.1.34    Environment(Language:0409  Keyboard:00000809  OS:WIN_VISTA/Service Pack 2  CPU:X64 OS:X86)
>Running AU3Check (1.54.19.0)  from:C:\Program Files\AutoIt3
+>08:38:04 AU3Check ended.rc:0
>Running:(3.3.6.1):C:\Program Files\AutoIt3\autoit3.exe "M:\Program\Au3 Scripts\Test.au3"    

Can you see me?

+>08:38:04 AutoIT3.exe ended.rc:0
>Exit code: 0    Time: 2.793

You can see how the required text has appeared among the normal info lines.

By the way, is there something wrong about loading the DLL in the memory and closing it right away as I am doing in my script?

Yes, you might as well not bother opening it in the first place. :>

All clear? :unsure:

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

I was not aware of this function in SciTE. Very convenient. For the readers that don't know, the console can be accessed under the tab VIEW and OUTPUT or by pressing F8.

TOOLS and GO or F5 will run the script. It is also very convenient.

Alain

Edited by AlainB

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

This is simple use GUIGetCursorInfo() i don´t read the whole thread i only respond for the first question... :/

#include <GUIConstantsEx.au3>
Dim $Delay = 20

$Dll = DllOpen("inpout32.dll")
DllClose("inpout32.dll")
Global $label, $a, $b, $labelstop = "      Stopped!"
$Form = GUICreate("", 275, 150)
$Button1 = GUICtrlCreateButton("CCW", 55, 50, 75, 50)
$Button2 = GUICtrlCreateButton("CW", 150, 50, 75, 50)
$label1 = GUICtrlCreateLabel("", 110, 120, 75, 17)
GUISetState(@SW_SHOW)

While 1
    $aMouse = GUIGetCursorInfo()
    if @error then continueloop
    $msg = GUIGetMsg()

    Switch $msg
        Case $GUI_EVENT_CLOSE
            DllCall($Dll, "int", "Out32", "int", 0x378, "int", 0)
            Exit
    EndSwitch
    Select
        case $aMouse[2] = 1

            If $aMouse[4] = $Button1 Then
                $label = " Running CCW"
                $a = 192
                $b = 128
                GUICtrlsetdata($label1, $label)
                do
                sendsteps()
                until $aMouse[2] = 1
            EndIf
                If $aMouse[4] = $Button2 Then
                $label = "  Running CW"
                $a = 64
                $b = 0
                GUICtrlsetdata($label1, $label)
                do
                sendsteps()
            until $aMouse[2] = 1
        EndIf
    case $aMouse[2] = 0
        If Guictrlread($label1) = $label then
            GUICtrlsetdata($label1, $labelstop)
        Endif
    Endselect
WEnd
Func SendSteps()
        DllCall($Dll, "int", "Out32", "int", 0x378, "int", $a)
        Sleep($Delay)
        DllCall($Dll, "int", "Out32", "int", 0x378, "int", $b)
        Sleep($Delay)
EndFunc   ;==>SendSteps

To close a dll use dllclose()

EDIT: dllclosing at the end of the script.. :unsure:

;..... allthe script
Case $GUI_EVENT_CLOSE
DllCall($Dll, "int", "Out32", "int", 0x378, "int", 0)
dllclose($dll)
Exit
;... more script

also ...the GUIGetCursorInfo() Function you can use it on picturebox and else, to make hoverbutton chages.. or picturebuttons, etc, etc.. is an easy way to do :>

Edited by monoscout999

Share this post


Link to post
Share on other sites

Thanks Monoscout999, with the changes you made, my script is working exactely as I wanted it to work.

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
Sign in to follow this  
Followers 0