Jump to content

Trying to create button that when clicked allows writing to file.


Recommended Posts

I am trying to create a menu that has settings stored in an ini file, and my program reads well, the only problem being that it doesnt seem to either start a function, or detect that a single button is pressed.

#include <GUIConstantsEX.au3>                                                                                   ;For various event operations such as the close button
#include <Misc.au3>

_main()                                                                                                         ;Skip straight to _main()

Func _main()
    Local $msg
    Local $height   = (@DesktopHeight / 15)                                                                     ;Figures out an appropriate height for the button as per the monitor size
    Local $winhandle = GUICreate("GameMenu",@DesktopWidth,@DesktopHeight, 0, 0, 0x80000000)                     ;Create GUI
    Local $forwardkey = IniRead (@WorkingDir & "\setup.ini", "PROFILE", "key1", "Key Not Found")
    Global $hInput1 = GUICtrlCreateButton( $forwardkey, @DesktopHeight / 3, @DesktopHeight / 4, @DesktopHeight / 2, @DesktopHeight / 2)
    global $exit    = GUICtrlCreateButton("EXIT", @DesktopHeight / 18.4, @DesktopWidth / 18.4, @DesktopHeight / 18, @DesktopWidth / 16)

    GUISetState()                                                                                               ;Make GUI Visible
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg =$forwardkey
                _Buttonpressed()
                MsgBox(01, "Input", $hInput1)
            Case $msg = $exit
                Exit
            ExitLoop
        EndSelect
    WEnd
EndFunc

Func _Buttonpressed()
    $input = 0
    MsgBox(02, "1", "Hello World")
    Do
        If _IsPressed(08) Then
            $button = "BACKSPACE"
            $input = 1
        EndIf
        If _IsPressed("09") Then
            $button = "TAB"
            $input = 1
        EndIf
        If _IsPressed("0D") Then
            $button = "ENTER"
            $input = 1
        EndIf
        If _IsPressed("14") Then
            $button = "CAPS LOCK"
            $input = 1
        EndIf
        If _IsPressed("1B") Then
            $button = "ESC"
            $input = 1
        EndIf
    Until $input = 1
    Return($button)
EndFunc

The big button in the middle of the screen is meant to be the one that allows for the changing of the setting. Right now, I only have a few keys that it can be changed to, but I wish to soon have most of the keyboard, but I refuse to script that in, until the problem I am having now is fixed. Can anybody spot any problems with my script? I havetried to include a smaller version of the ini file (not the full one so you dint need to create it, but ini uploading is not permitted, so you will have to create it (sorry).

Anyway, Thanks pplz.

Link to comment
Share on other sites

Changed post completely click the spoiler for original content.

$hInput1 = _Buttonpressed()
    MsgBox(01, "Input", $hInput1)

Buttonpresses are detected, but you didn't check for the return value of the function.

edit:

what's the idea of

Case $msg = $forwardkey

I can't see it do much besides giving false positives.

I got a bit confused as to what value's where meant to go where, but I'd do something like this:

#include <GUIConstantsEX.au3>                                                                                   ;For various event operations such as the close button
#include <Misc.au3>

Global $hGUI = GUICreate("GameMenu",@DesktopWidth,@DesktopHeight, 0, 0, 0x80000000)                     ;Create GUI
Global $sForwardKey = IniRead (@WorkingDir & "\setup.ini", "PROFILE", "key1", "Key Not Found")
Global $Ctrl_Button = GUICtrlCreateButton($sForwardKey, @DesktopHeight / 3, @DesktopHeight / 4, @DesktopHeight / 2, @DesktopHeight / 2)
Global $Ctrl_Exit   = GUICtrlCreateButton("EXIT", @DesktopHeight / 18.4, @DesktopWidth / 18.4, @DesktopHeight / 18, @DesktopWidth / 16)
Global $sKey
GUISetState()                                                       ;Make GUI Visible

While 1
    Switch GUIGetMsg()
        Case $Ctrl_Button
            $sKey = _Buttonpressed()
            MsgBox(01, "Input", $sKey)
        Case $Ctrl_Exit
            Exit
        ExitLoop
    EndSwitch
WEnd


Func _Buttonpressed()
    MsgBox(02, "1", "Hello World")
    While 1
        Select
            Case _IsPressed("08")
                Return "BACKSPACE"
            Case _IsPressed("09")
                Return "TAB"
            Case _IsPressed("0D")
                Return "ENTER"
            Case _IsPressed("14")
                Return "CAPS LOCK"
            Case _IsPressed("1B")
                Return "ESC"
        EndSelect
    WEnd
EndFunc

I noticed that Space and Enter will trigger the focussed button again, creating a loop-like situation, so perhaps you need to unfocus the big button after it's pressed.

Edited by Tvern
Link to comment
Share on other sites

So are you saying it should be more like this?

While 1
        Switch $GUIGetMsg()
        Select
            Case $forwardkey
                $forwardkey = _Buttonpressed($button)
                MsgBox(01, "Input", $hInput1)
            Case $exit
                Exit
            ExitLoop
        EndSelect
        EndSwitch
    WEnd

EDIT

Oh I see, i will try it out then.

EDIT 2

That worked better than my script, the biggest problem being that it seems to hang on the second function.

Edited by Mikeman27294
Link to comment
Share on other sites

You don't need to put the select statement inside that switch statement. I went a bit overboard adjusting the script and made a lot of unneeded changes which come down to preference, or standards.

For instance your _Buttonpressed() function was identical in function to mine, but I just thought this looked cleaner, I switched your Select with a Switch to reduce Global variables and I took the main code outside the Main() function to indicate that AutoIt doesn't require it. Unlike, for instance C.

Take from my example what you like and discard the rest.

The most important changes where using the return value of _Buttonpressed() in the MsgBox and using $Ctrl_Button instead of $forwardkey in the main loop.

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