Sign in to follow this  
Followers 0
ryantollefson

Press and Hold GUI buttons?

9 posts in this topic

I have a GUI that is working pretty well, but I want to set several of the buttons so that they will repeat a command as long as the GUIbutton is held down. Any suggestions on how to do this?

Share this post


Link to post
Share on other sites



Try to use TimeInt and TimeDiff

Share this post


Link to post
Share on other sites

Try to use TimeInt and TimeDiff

Admitedly I'm a newb, but it looks to me like that could work to return the time that the button is pressed, but I need the button command to repeat while the button is being pressed and held. If this can be done with TimeDiff any chance can you show me an example?

Thanks.

Share this post


Link to post
Share on other sites

Admitedly I'm a newb, but it looks to me like that could work to return the time that the button is pressed, but I need the button command to repeat while the button is being pressed and held. If this can be done with TimeDiff any chance can you show me an example?

Thanks.

Umm, the command isn't given until the button is released as it has been pressed once. I will think about that.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I figured it out :shocked: . Alright, this script is ugly, but shows basic functionality for holding down a button:

#include <GUIConstants.au3>

GUICreate("Press and hold", 200, 200)
GUISetState()

$btn_test = GuiCtrlCreateButton("Test", 30, 100) ;create GUI button
$counter = 0

While 1
    GuiCtrlCreateLabel ($counter, 50, 50);just to show button effect - I know it's ugly
    $msg = GUIGetMsg()
    $mouse_pos = GUIGetCursorInfo()

    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case ($mouse_pos[2] = 1) and ($mouse_pos[4] = $btn_test)
            $counter = ($counter + 1) ;raise counter when button held
        Case ($mouse_pos[2] = 1) And ($mouse_pos[4] <> $btn_test) 
            $counter = 0 ;reset the counter when button not held
    EndSelect
WEnd
Exit

Only thing I'm wondering is if having a GUIGetCursorInfo() in a While loop is bad CPU wise? (I'm a newb, and don't really know how to optimize code).

Edited by ryantollefson

Share this post


Link to post
Share on other sites

I figured it out :shocked: . Alright, this script is ugly, but shows basic functionality for holding down a button:

#include <GUIConstants.au3>

GUICreate("Press and hold", 200, 200)
GUISetState()

$btn_test = GuiCtrlCreateButton("Test", 30, 100) ;create GUI button
$counter = 0

While 1
    GuiCtrlCreateLabel ($counter, 50, 50);just to show button effect - I know it's ugly
    $msg = GUIGetMsg()
    $mouse_pos = GUIGetCursorInfo()

    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case ($mouse_pos[2] = 1) and ($mouse_pos[4] = $btn_test)
            $counter = ($counter + 1) ;raise counter when button held
        Case ($mouse_pos[2] = 1) And ($mouse_pos[4] <> $btn_test) 
            $counter = 0 ;reset the counter when button not held
    EndSelect
WEnd
Exit

Only thing I'm wondering is if having a GUIGetCursorInfo() in a While loop is bad CPU wise? (I'm a newb, and don't really know how to optimize code).

The loop won't take up CPU usage if you put a sleep in, for like 0.1 second it helps a lot.

Share this post


Link to post
Share on other sites

Ok, thanks for the help so far, but I just discovered I get this error if I don't keep focus on my gui window:

"press and hold button.au3 (29) : ==> Subscript used with non-Array variable.:"

Share this post


Link to post
Share on other sites

Ok, thanks for the help so far, but I just discovered I get this error if I don't keep focus on my gui window:

"press and hold button.au3 (29) : ==> Subscript used with non-Array variable.:"

You need to show us the code around line 29.

If you check @error after

$mouse_pos = GUIGetCursorInfo()

then it might not be 0 in which case $mouse_pos will not be an array.

If this is the problem then instead of

Case ($mouse_pos[2] = 1) and ($mouse_pos[4] = $btn_test)

$counter = ($counter + 1) ;raise counter when button held

you could say

Case IsArray($mouse_pos)

Select

Case ($mouse_pos[2] = 1) and ($mouse_pos[4] = $btn_test)

$counter = ($counter + 1) ;raise counter when button held

Case ($mouse_pos[2] = 1) And ($mouse_pos[4] <> $btn_test)

$counter = 0 ;reset the counter when button not held

EndSelect


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.

Share this post


Link to post
Share on other sites

Thanks, that seemed to fix it. Just in case anyone wants it, here is the code:

#include <GUIConstants.au3>

GUICreate("Press and hold", 200, 200)
GUISetState()

$btn_test = GuiCtrlCreateButton("Test", 30, 100) ;create GUI button
$counter = 0

While 1
    GuiCtrlCreateLabel ($counter, 50, 50);just to show button effect - I know it's ugly
    $msg = GUIGetMsg()
    $mouse_pos = GUIGetCursorInfo()

    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case IsArray($mouse_pos)
            Select
                Case ($mouse_pos[2] = 1) and ($mouse_pos[4] = $btn_test)
                $counter = ($counter + 1) ;raise counter when button held
                Case ($mouse_pos[2] = 1) And ($mouse_pos[4] <> $btn_test) 
                $counter = 0 ;reset the counter when button not held
            EndSelect
    EndSelect
WEnd
Exit

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