Sign in to follow this  
Followers 0
Leighwyn

GUI Button for defining a keybinding, slight hiccup...

6 posts in this topic

Here's what I'm working with:

As a part of my program's GUI, there is a tab devoted to defining keyboard keys for a variety of commands. The commands are given in a list, and there is a button labeled "Define" which, when pushed, waits to grab what the next keypress is by using your standard _isPressed() mess. It then checks which command in the list is active, and writes to the appropriate variable and .ini key accordingly.

First, my code works just fine. There's just a mild hiccup that occurs which I haven't been able to resolve in any way that wasn't completely crude.

Here is a walkthrough of how it surfaces:

1) I select a command in my list

2) I push the "Define" button

3) I want to bind "{SPACE}" to this command, so...

4) I push the space bar

4a) It assigns to the variable and .ini as it should

4b) Since I just pressed the space bar, and the "Define" button was the active element in my GUI, I effectively pressed the button again...grrr

Once this cycle begins, I can break it by left-clicking my mouse anywhere inside a blank part of my GUI, THEN press space bar. So that's the "crude" solution that I've been employing...the first line of the function that executes when the button is pressed does a MouseClick() just beside the button, to stop it from being the active element.

My question should be obvious, then. Is there a better way to do this? Preferably, one that isn't noticed by the user?

Thanks for any help

Share this post


Link to post
Share on other sites



Code is below. It isn't the original stuff, I just hacked out the bit that mattered.

Incident: Pick a command, click "Define", and press Space Bar to say that you want the command to be assigned to Space Bar. Space bar gets assigned, but also causes the button to trigger again (since that's how Windows works with highlighted buttons)

My crummy solution: I add in a line that says

MouseClick("left", $x,$y)oÝ÷ Û^­ëa{¢Ýȧi¹ZF«yª¶PájÜÊØ^nëm¢x¬h«ªê-(!(!µçjºZÛ)iÇj¼(Ö¶+Úµè­"Ëaz·Ü®é¦ÊÊ%ºØ¨(ºW]¡Êyº1jëh×6#include-once
#include "GUIConstants.au3"
#include "Misc.au3"
#cs
   Chopped up to isolate the issue, not original code
#ce
opt("GUIOnEventMode", 1)
global $command1="<none>"
global $command2="<none>"
global $command3="<none>"

gui_init()
gui_loop()

Func gui_init()
    local $exStyle = -1
    
    GUICreate("Info", 500, 450, -1, -1, -1, $exStyle)
    GUISetOnEvent($GUI_EVENT_CLOSE, "gui_exit")
    $gui_Status = GUICtrlCreateEdit("", 0, 25, 400, 75, $ES_READONLY)
    
;GUI TABS
    GUICtrlCreateTab(0, 0, 500, 25, $TCS_FIXEDWIDTH)
    
  ;Tab #5 KEYBINDING DEFINITION
    $tab_keydef = GUICtrlCreateTabItem("Keys Setup")
    global $gui_commandList = GUICtrlCreateList("", 10, 120, 125, 180)
    GUICtrlSetData(-1, "command1|command2|command3")
    GUICtrlSetOnEvent(-1, "gui_CommandListHandler")
    global $gui_keyLabel = GUICtrlCreateLabel("...",145,120,250,50)
    $gui_keyUpdate = GUICtrlCreateButton("Define",145,190,60,25)
    GUICtrlSetOnEvent(-1, "gui_KeyDefine")
    
;End of Tabs
    GUICtrlCreateTabItem("")
    GUISetState(@SW_SHOW)
EndFunc

Func gui_exit()
    Exit
EndFunc


Func gui_CommandListHandler()
;Updates a label to show some info of active command in list
    local $currCommand = GUICtrlRead($gui_commandList)
    local $currKey = eval($currCommand)
    GuiCtrlSetData($gui_keyLabel,"Command ["&$currCommand&"] was defined as...  "&$currKey)
EndFunc

Func gui_KeyDefine()
;waits for keypress, then redefines the active command in list to equal the pressed key
    local $currCommand = GUICtrlRead($gui_CommandList)
    local $currKey = eval($currCommand)
    $oldtext = "Command ["&$currCommand&"] was defined as...  "&$currKey
    GUICtrlSetData($gui_keyLabel,$oldtext & @CRLF & "Redefining, waiting for keypress...")
    $newKey = StringUpper(gui_readKeypress())
    GUICtrlSetData($gui_keyLabel,GUICtrlRead($gui_keyLabel)&@CRLF&"["&$currCommand&"] now defined as... "&$newKey)
    assign($currCommand,$newKey)
EndFunc

Func gui_readKeypress()
;Standard "keylogger" function
    $start = TimerInit()
    While 1
        If TimerDiff($start) > 20000 Then
            GUICtrlSetData($gui_keyLabel,"TOOK TOO LONG, TRY AGAIN")
            return
        EndIf
        If _IsPressed(41) Then
            return("a")
        EndIf
        If _IsPressed(42) Then
            return("b")
        EndIf
        If _IsPressed(43) Then
            return("c")
        EndIf
        If _IsPressed(44) Then
            return("d")
        EndIf
        If _IsPressed(45) Then
            return("e")
        EndIf
        If _IsPressed(46) Then
            return("f")
        EndIf
        If _IsPressed(47) Then
            return("g")
        EndIf
        If _IsPressed(48) Then
            return("h")
        EndIf
        If _IsPressed(49) Then
            return("i")
        EndIf
        If _IsPressed('4a') Then
            return("j")
        EndIf
        If _IsPressed('4b') Then
            return("k")
        EndIf
        If _IsPressed('4c') Then
            return("l")
        EndIf
        If _IsPressed('4d') Then
            return("m")
        EndIf
        If _IsPressed('4e') Then
            return("n")
        EndIf
        If _IsPressed('4f') Then
            return("o")
        EndIf
        If _IsPressed(50) Then
            return("p")
        EndIf
        If _IsPressed(51) Then
            return("q")
        EndIf
        If _IsPressed(52) Then
            return("r")
        EndIf
        If _IsPressed(53) Then
            return("s")
        EndIf
        If _IsPressed(54) Then
            return("t")
        EndIf
        If _IsPressed(55) Then
            return("u")
        EndIf
        If _IsPressed(56) Then
            return("v")
        EndIf
        If _IsPressed(57) Then
            return("w")
        EndIf
        If _IsPressed(58) Then
            return("x")
        EndIf
        If _IsPressed(59) Then
            return("y")
        EndIf
        If _IsPressed('5a') Then
            return("z")
        EndIf
        If _IsPressed('08') Then
            return("{BACKSPACE}")
        EndIf
        If _IsPressed('09') Then
            return("{TAB}")
        EndIf
        If _IsPressed('0d') Then
            return("{ENTER}")
        EndIf
        If _IsPressed('A0') Then
            return("{LSHIFT}")
        EndIf
        If _IsPressed('A1') Then
            return("{RSHIFT}")
        EndIf       
        If _IsPressed('A2') Then
            return("{LCTRL}")
        EndIf
        If _IsPressed('A3') Then
            return("{RCTRL}")
        EndIf
        If _IsPressed('12') Then
            return("{ALT}")
        EndIf
        If _IsPressed('13') Then
            return("{PAUSE}")
        EndIf  
        If _IsPressed('14') Then
            return("{CAPSLOCK}")
        EndIf
        If _IsPressed('1b') Then
            return("{ESC}")
        EndIf
        If _IsPressed('20') Then
            return("{SPACE}")
        EndIf
        If _IsPressed('21') Then
            return("{PGUP}")
        EndIf
        If _IsPressed('22') Then
            return("{PGDN}")
        EndIf
        If _IsPressed('23') Then
            return("{END}")
        EndIf
        If _IsPressed('24') Then
            return("{HOME}")
        EndIf
        If _IsPressed('25') Then
            return("{LEFT}")
        EndIf
        If _IsPressed('26') Then
            return("{UP}")
        EndIf
        If _IsPressed('27') Then
            return("{RIGHT}")
        EndIf
        If _IsPressed('28') Then
            return("{DOWN}")
        EndIf
        If _IsPressed('2c') Then
            return("{PRINTSCREEN}")
        EndIf
        If _IsPressed('2d') Then
            return("{INSERT}")
        EndIf
        If _IsPressed('2e') Then
            return("{DEL}")
        EndIf
        If _IsPressed('30') Then
            return("0")
        EndIf
        If _IsPressed('31') Then
            return("1")
        EndIf
        If _IsPressed('32') Then
            return("2")
        EndIf
        If _IsPressed('33') Then
            return("3")
        EndIf
        If _IsPressed('34') Then
            return("4")
        EndIf
        If _IsPressed('35') Then
            return("5")
        EndIf
        If _IsPressed('36') Then
            return("6")
        EndIf
        If _IsPressed('37') Then
            return("7")
        EndIf
        If _IsPressed('38') Then
            return("8")
        EndIf
        If _IsPressed('39') Then
            return("9")
        EndIf
        If _IsPressed('60') Then
            return("{NUMPAD0}")
        EndIf
        If _IsPressed('61') Then
            return("{NUMPAD1}")
        EndIf
        If _IsPressed('62') Then
            return("{NUMPAD2}")
        EndIf
        If _IsPressed('63') Then
            return("{NUMPAD3}")
        EndIf
        If _IsPressed('64') Then
            return("{NUMPAD4}")
        EndIf
        If _IsPressed('65') Then
            return("{NUMPAD5}")
        EndIf
        If _IsPressed('66') Then
            return("{NUMPAD6}")
        EndIf
        If _IsPressed('67') Then
            return("{NUMPAD7}")
        EndIf
        If _IsPressed('68') Then
            return("{NUMPAD8}")
        EndIf
        If _IsPressed('69') Then
            return("{NUMPAD9}")
        EndIf
        If _IsPressed('90') Then
            return("{NUMLOCK}")
        EndIf
        If _IsPressed('91') Then
            return("{SCROLLLOCK}")
        EndIf
        If _IsPressed('70') Then
            return("{F1}")
        EndIf
        If _IsPressed('71') Then
            return("{F2}")
        EndIf
        If _IsPressed('72') Then
            return("{F3}")
        EndIf
        If _IsPressed('73') Then
            return("{F4}")
        EndIf
        If _IsPressed('74') Then
            return("{F5}")
        EndIf
        If _IsPressed('75') Then
            return("{F6}")
        EndIf
        If _IsPressed('76') Then
            return("{F7}")
        EndIf
        If _IsPressed('77') Then
            return("{F8}")
        EndIf
        If _IsPressed('78') Then
            return("{F9}")
        EndIf
        If _IsPressed('79') Then
            return("{F10}")
        EndIf
        If _IsPressed('7A') Then
            return("{F11}")
        EndIf
        If _IsPressed('7B') Then
            return("{F12}")
        EndIf
        If _IsPressed('BF') Then
            return("/")
        EndIf
        If _IsPressed('C0') Then
            return("`")
        EndIf
        If _IsPressed('DB') Then
            return("[")
        EndIf
        If _IsPressed('DC') Then
            return("\")
        EndIf
        If _IsPressed('DD') Then
            return("]")
        EndIf
        If _IsPressed('DE') Then
            return("'")
        EndIf
        If _IsPressed('BA') Then
            return(";")
        EndIf
        If _IsPressed('6A') Then
            return("{NUMPADMULT}")
        EndIf
        If _IsPressed('6B') Then
            return("{NUMPADADD}")
        EndIf
        If _IsPressed('6C') Then
            return("{NUMPADENTER}")
        EndIf
        If _IsPressed('6D') Then
            return("{NUMPADSUB}")
        EndIf
        If _IsPressed('6E') Then
            return("{NUMPADDOT}")
        EndIf
        If _IsPressed('6F') Then
            return("{NUMPADDIV}")
        EndIf
        If _IsPressed('BB') Then
            return("=")
        EndIf
        If _IsPressed('BC') Then
            return(",")
        EndIf
        If _IsPressed('BD') Then
            return("-")
        EndIf
        If _IsPressed('BE') Then
            return(".")
        EndIf
        sleep(10)
    WEnd
EndFunc
    
Func gui_loop()
    while 1
        sleep(10000)
    WEnd
EndFunc

Share this post


Link to post
Share on other sites

first and only b.u.m.p. until I abandon the issue :)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I don't know if it's what you need but look at HotKey control:

http://svn.autoitscript.com/trac/ticket/311

In this track topic there are links to MSDN and some AutoIt's UDFs.

Thanks, Zedna, that's an informative link. After a great amount of digging through the help file, I did manage to find something that would work for my script as-is...and it's silly enough for me to be ashamed :P

All I had to do was add the line...

GUICtrlSetState($gui_commandList, $GUI_FOCUS)

...to immediately transfer focus from the Button to the List whenever it was clicked on. Now, if I want to define a command hotkey as {SPACE}, I am free to press spacebar without the button re-activating itself :)

(And in the process, I also realized how outdated my AutoIt version was when it came to GUI #include's lol)

Edited by Leighwyn

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