ryantollefson Posted April 23, 2007 Posted April 23, 2007 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?
ryantollefson Posted April 24, 2007 Author Posted April 24, 2007 Try to use TimeInt and TimeDiffAdmitedly 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.
Generator Posted April 24, 2007 Posted April 24, 2007 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.
ryantollefson Posted April 24, 2007 Author Posted April 24, 2007 (edited) I figured it out . 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 April 24, 2007 by ryantollefson
Generator Posted April 24, 2007 Posted April 24, 2007 I figured it out . 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.
ryantollefson Posted April 24, 2007 Author Posted April 24, 2007 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.:"
martin Posted April 24, 2007 Posted April 24, 2007 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.
ryantollefson Posted April 24, 2007 Author Posted April 24, 2007 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now