Jump to content

Need Help with Checkbox Toggle with mouseclick/hotkey


Smurfin
 Share

Recommended Posts

Hi,

I'm new to AutoIT and I want to make a GUI with a checkbox that can be checked/unchecked using both a hotkey and mouseclick

It works with a little problem though, I can check/uncheck using F10 (the hotkey I used) and I can check/uncheck with mouseclick

the problem is if I check/uncheck with mouse click, then I press F10, every once so often it stay checked/unchecked. It should uncheck if checked, and check if unchecked, with either a mouseclick or F10 press.

I don't know what's wrong with my script, please help.

thanks in advance

here is the script :

#include <GuiConstantsEx.au3>
#include <AVIConstants.au3>
#include <TreeViewConstants.au3>

; GUI
GuiCreate("AutoHeal", 200, 100); create Gui Window 200x100 pixels 
GuiSetIcon(@SystemDir & "\AutohealICON.bmp", 0); set program icon

; CHECKBOX
$Checkname="AutoHeal On/Off (F10)";name to display at GUI
$checkbox=GuiCtrlCreateCheckbox($Checkname, 10, 30, 150, 20);create Checkbox 
Guictrlsetstate($checkbox,$GUI_UNCHECKED)

Global $Checked
$iniWinName = IniRead("macro.ini","Settings","Window Name","Element Client")
$healstate=Guictrlread($checkbox)
$Checked="OFF"


; GUI MESSAGE LOOP
GuiSetState()
While GuiGetMsg() <> $GUI_EVENT_CLOSE
    Hotkeyset("{F10}","Togglecheckbox"); Ctrl-F10 Toggle
WEnd

; Toggle ON/OFF checkbox and then auto heal
Func ToggleCheckBox()
    while 1
        If $checked="ON" Then 
          Guictrlsetstate($checkbox,$GUI_UNCHECKED)
          $checked="OFF"
          exitloop
        EndIf
        If $checked="OFF" Then 
            Guictrlsetstate($checkbox,$GUI_CHECKED)
            $checked="ON"
            exitloop
        EndIf
    WEnd
EndFunc
Edited by Smurfin
Link to comment
Share on other sites

  • Moderators

@Smurfin,

The problem arises because you are not changing the value of $Checked when you use the mouse to click the box. So the first time you use F10 subsequently, it thinks $Checked is the wrong value.

I added a small label to show the value of $Checked, changed the GUI message loop to look for the checkbox being actioned and seriously pruned your Toggle function!

This code works as you want ():

#include <GuiConstantsEx.au3>
#include <AVIConstants.au3>
#include <TreeViewConstants.au3>

; GUI
GuiCreate("AutoHeal", 200, 100); create Gui Window 200x100 pixels 
GuiSetIcon(@SystemDir & "\AutohealICON.bmp", 0); set program icon

; CHECKBOX
$Checkname="AutoHeal On/Off (F10)";name to display at GUI
Global $checkbox=GuiCtrlCreateCheckbox($Checkname, 10, 20, 150, 20);create Checkbox 
Global $label = GUICtrlCreateLabel("", 10, 60, 50, 20)
Guictrlsetstate($checkbox,$GUI_UNCHECKED)

Global $Checked
$iniWinName = IniRead("macro.ini","Settings","Window Name","Element Client")
$healstate=Guictrlread($checkbox)
$Checked="OFF"
GUICtrlSetData($label, $Checked)

GuiSetState()

Hotkeyset("{F10}","Togglecheckbox"); Ctrl-F10 Toggle

; GUI MESSAGE LOOP

While 1
    
    $msg = GuiGetMsg()
    
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $checkbox
            ToggleCheckBox()
    EndSwitch
WEnd

; Toggle ON/OFF checkbox and then auto heal
Func ToggleCheckBox()
    
    If $checked="ON" Then 
        Guictrlsetstate($checkbox,$GUI_UNCHECKED)
        $checked="OFF"
        GUICtrlSetData($label, $Checked)
    Else
        Guictrlsetstate($checkbox,$GUI_CHECKED)
        $checked="ON"
        GUICtrlSetData($label, $Checked)
    EndIf

EndFunc

I also moved the HotKeySet declaration outside the loop - you only need declare it once, not every time!

Hope this helps you progress.

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

oh one more thing, I would like to ask about this command -> hotkeyset

if at that script above I set to use Alt-F10 as toggle key to check/uncheck the checkbox, like this Hotkeyset("!{F10}","Togglecheckbox") , it works alright. But when I exit the script my keyboard is behaving like alt key is still being pressed, so if I type for example letter F on my keyboard at let's say Mozilla Firefox, it's like pressing Alt-F (go to 'file' menu), so I have to press alt once to deactivate it. Is that common if using combination key ? I've tried using Ctrl which is ^ for AutoIT, and it behaved the same.

and can this hotkeyset command be used with IniRead ? I use :

$iniHotKey = IniRead("macro.ini","Settings","HotKey","{F10}")
Hotkeyset($iniHotKey,"Togglecheckbox")

then I create macro.ini like this :

[Settings]
HotKey=!{F11}

but it won't take that Alt-F11 to use with the hotkeyset command, while by reading that $iniHotKey , it should pick !{F11} as hotkey, right ? at least that's what I thought it should run, but its not. And it behaves like there is no key is set for that HotKeyset (won't do anything)

Edited by Smurfin
Link to comment
Share on other sites

  • Moderators

@smurfin,

First time I have played with HotKeySet, so I had to experiment a bit before replying. You need to save the hotkey definition as a string, enclosed in quotes.

The following code works perfectly for me:

IniWrite("M:\Program\Au3 Scripts\Hotkey.ini", "Settings", "Hotkey", "!{F11}")

$key = IniRead("M:\Program\Au3 Scripts\Hotkey.ini", "Settings", "Hotkey", "")

HotKeySet($key, "test")

While 1 
    
WEnd

; -----

Func test()
    
    MsgBox(0,"Test","It worked")
    
EndFunc

As to the "sticky" keys, a search of the forum suggests that adding Send ("{CTRLUP}{CTRLUP}{CTRLUP}{ALTUP}{ALTUP}{ALTUP}") at the end of your script is the only way to go. To quote Valik (one of the developers): "AutoIt can't judge the intent. Maybe you really did mean to have those other keys down, maybe not. We don't know, but you do. So you must be the one to clear the modifier state if that's what you really need to do."

Hope this helps.

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

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