taurus905 Posted January 6, 2010 Share Posted January 6, 2010 I need help combining the functionality of how a combobox reacts using the up and down arrow keys and using the mouse. I thought this would be easy to do, but the solution is eluding me. I am hoping that I am just missing something simple and somebody will set me straight. As always, I greatly appreciate any input which points me in the right direction. taurus905 expandcollapse popup; Works with Arrow Keys.au3 Global Const $GUI_EVENT_CLOSE = -3 Opt('MustDeclareVars', 1) Dim $hGui Dim $cBlack = 0x000000, $cRed = 0xFF0000 Dim $sCombo, $hCombo Dim $hLabel Dim $msg $hGui = GUICreate("Works right using Arrow Keys to choose.", 350, 100) GUISetBkColor($cBlack) GUICtrlCreateLabel("But does not work right with Mouse.", 30, 10) GUICtrlSetBkColor(-1, 0xFFFFFF) For $i = 1 To 100 $sCombo &= $i & "|" Next $hCombo = GUICtrlCreateCombo("", 50, 40, 70) GUICtrlSetData(-1, $sCombo, "1") $hLabel = GUICtrlCreateLabel("1", 150, 40, 70) GUICtrlSetBkColor(-1, 0xFFFFFF) GUISetState(@SW_SHOW) While 1 $msg = GUIGetMsg() Switch $msg Case $hCombo If _IsPressed("26") Or _IsPressed("28") Then ; UP ARROW and DOWN ARROW While _IsPressed("26") Or _IsPressed("28") Sleep(10) WEnd Set_Label_Data() EndIf Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Exit Func Set_Label_Data() GUICtrlSetData($hLabel, GUICtrlRead($hCombo)) If Mod(GUICtrlRead($hCombo), 2) = 0 Then GUISetBkColor($cRed, $hGui) Else GUISetBkColor($cBlack, $hGui) EndIf Sleep(500) EndFunc Func _IsPressed($sHexKey, $vDLL = 'user32.dll') Local $a_R = DllCall($vDLL, "short", "GetAsyncKeyState", "int", '0x' & $sHexKey) If @error Then Return SetError(@error, @extended, False) Return BitAND($a_R[0], 0x8000) <> 0 EndFunc ; ==> _IsPressed This second script loops too many times when you hold down the up or down arrow key. expandcollapse popup; Works with Mouse.au3 Global Const $GUI_EVENT_CLOSE = -3 Opt('MustDeclareVars', 1) Dim $hGui Dim $cBlack = 0x000000, $cRed = 0xFF0000 Dim $sCombo, $hCombo Dim $hLabel Dim $msg $hGui = GUICreate("Works right using Mouse to choose.", 350, 100) GUISetBkColor($cBlack) GUICtrlCreateLabel("But does not work right with Arrow Keys.", 30, 10) GUICtrlSetBkColor(-1, 0xFFFFFF) For $i = 1 To 100 $sCombo &= $i & "|" Next $hCombo = GUICtrlCreateCombo("", 50, 40, 70) GUICtrlSetData(-1, $sCombo, "1") $hLabel = GUICtrlCreateLabel("1", 150, 40, 70) GUICtrlSetBkColor(-1, 0xFFFFFF) GUISetState(@SW_SHOW) While 1 $msg = GUIGetMsg() Switch $msg Case $hCombo Set_Label_Data() Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Exit Func Set_Label_Data() GUICtrlSetData($hLabel, GUICtrlRead($hCombo)) If Mod(GUICtrlRead($hCombo), 2) = 0 Then GUISetBkColor($cRed, $hGui) Else GUISetBkColor($cBlack, $hGui) EndIf Sleep(500) EndFunc "Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 6, 2010 Moderators Share Posted January 6, 2010 taurus905,Does this work as you want? expandcollapse popup#include <GUIConstantsEx.au3> ; Works with Arrow Keys and mouse Opt('MustDeclareVars', 1) Global $cBlack = 0x000000, $cRed = 0xFF0000, $sCombo, $msg Global $hGui = GUICreate("Works right using Arrow Keys to choose.", 350, 100) GUISetBkColor($cBlack) GUICtrlCreateLabel("And works with Mouse!!!", 30, 10) GUICtrlSetBkColor(-1, 0xFFFFFF) For $i = 1 To 100 $sCombo &= $i & "|" Next Global $hCombo = GUICtrlCreateCombo("", 50, 40, 70) GUICtrlSetData(-1, $sCombo, "1") Global $hLabel = GUICtrlCreateLabel("1", 150, 40, 70) GUICtrlSetBkColor(-1, 0xFFFFFF) GUISetState(@SW_SHOW) Global $sCombo_Text = "" While 1 $msg = GUIGetMsg() Switch $msg Case $hCombo If _IsPressed("26") Or _IsPressed("28") Then ; UP ARROW and DOWN ARROW While _IsPressed("26") Or _IsPressed("28") Sleep(10) WEnd Set_Label_Data() EndIf Case $GUI_EVENT_CLOSE Exit EndSwitch If GUICtrlRead($hCombo) <> $sCombo_Text Then $sCombo_Text = GUICtrlRead($hCombo) Set_Label_Data() EndIf WEnd Exit Func Set_Label_Data() GUICtrlSetData($hLabel, GUICtrlRead($hCombo)) If Mod(GUICtrlRead($hCombo), 2) = 0 Then GUISetBkColor($cRed, $hGui) Else GUISetBkColor($cBlack, $hGui) EndIf Sleep(500) EndFunc Func _IsPressed($sHexKey, $vDLL = 'user32.dll') Local $a_R = DllCall($vDLL, "short", "GetAsyncKeyState", "int", '0x' & $sHexKey) If @error Then Return SetError(@error, @extended, False) Return BitAND($a_R[0], 0x8000) <> 0 EndFunc ; ==> _IsPressedYou will notice that I have used Global/Local rather then Dim. AutoIt will do its best to get the variables scoped correctly, but it is always best to make it explicit. M23  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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
taurus905 Posted January 6, 2010 Author Share Posted January 6, 2010 taurus905, Does this work as you want? expandcollapse popup#include <GUIConstantsEx.au3> ; Works with Arrow Keys and mouse Opt('MustDeclareVars', 1) Global $cBlack = 0x000000, $cRed = 0xFF0000, $sCombo, $msg Global $hGui = GUICreate("Works right using Arrow Keys to choose.", 350, 100) GUISetBkColor($cBlack) GUICtrlCreateLabel("And works with Mouse!!!", 30, 10) GUICtrlSetBkColor(-1, 0xFFFFFF) For $i = 1 To 100 $sCombo &= $i & "|" Next Global $hCombo = GUICtrlCreateCombo("", 50, 40, 70) GUICtrlSetData(-1, $sCombo, "1") Global $hLabel = GUICtrlCreateLabel("1", 150, 40, 70) GUICtrlSetBkColor(-1, 0xFFFFFF) GUISetState(@SW_SHOW) Global $sCombo_Text = "" While 1 $msg = GUIGetMsg() Switch $msg Case $hCombo If _IsPressed("26") Or _IsPressed("28") Then ; UP ARROW and DOWN ARROW While _IsPressed("26") Or _IsPressed("28") Sleep(10) WEnd Set_Label_Data() EndIf Case $GUI_EVENT_CLOSE Exit EndSwitch If GUICtrlRead($hCombo) <> $sCombo_Text Then $sCombo_Text = GUICtrlRead($hCombo) Set_Label_Data() EndIf WEnd Exit Func Set_Label_Data() GUICtrlSetData($hLabel, GUICtrlRead($hCombo)) If Mod(GUICtrlRead($hCombo), 2) = 0 Then GUISetBkColor($cRed, $hGui) Else GUISetBkColor($cBlack, $hGui) EndIf Sleep(500) EndFunc Func _IsPressed($sHexKey, $vDLL = 'user32.dll') Local $a_R = DllCall($vDLL, "short", "GetAsyncKeyState", "int", '0x' & $sHexKey) If @error Then Return SetError(@error, @extended, False) Return BitAND($a_R[0], 0x8000) <> 0 EndFunc ; ==> _IsPressed You will notice that I have used Global/Local rather then Dim. AutoIt will do its best to get the variables scoped correctly, but it is always best to make it explicit. M23 Hello Melba23, Very nice and simple solution. Thank you for taking the time to get me back on track. And thanks for the tip on variable scope. I usually use Dim and Local, but maybe I should go back to using Global and Local. I did make one change to your solution, but I am not sure which one I will use yet. I may have to tweak it a little more. But it is nice to have a choice. Thanks again, taurus905 expandcollapse popup#include <GUIConstantsEx.au3> ; Works with Arrow Keys and mouse Opt('MustDeclareVars', 1) Global $cBlack = 0x000000, $cRed = 0xFF0000, $sCombo, $msg Global $hGui = GUICreate("Works right using Arrow Keys to choose.", 350, 100) GUISetBkColor($cBlack) GUICtrlCreateLabel("And works with Mouse!!!", 30, 10) GUICtrlSetBkColor(-1, 0xFFFFFF) For $i = 1 To 100 $sCombo &= $i & "|" Next Global $hCombo = GUICtrlCreateCombo("", 50, 40, 70) GUICtrlSetData(-1, $sCombo, "1") Global $hLabel = GUICtrlCreateLabel("1", 150, 40, 70) GUICtrlSetBkColor(-1, 0xFFFFFF) GUISetState(@SW_SHOW) Global $sCombo_Text = "" While 1 $msg = GUIGetMsg() Switch $msg Case $hCombo If _IsPressed("26") Or _IsPressed("28") Then ; UP ARROW and DOWN ARROW While _IsPressed("26") Or _IsPressed("28") Sleep(10) WEnd Set_Label_Data() EndIf If GUICtrlRead($hCombo) <> $sCombo_Text Then $sCombo_Text = GUICtrlRead($hCombo) Set_Label_Data() EndIf Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd Exit Func Set_Label_Data() GUICtrlSetData($hLabel, GUICtrlRead($hCombo)) If Mod(GUICtrlRead($hCombo), 2) = 0 Then GUISetBkColor($cRed, $hGui) Else GUISetBkColor($cBlack, $hGui) EndIf Sleep(500) EndFunc Func _IsPressed($sHexKey, $vDLL = 'user32.dll') Local $a_R = DllCall($vDLL, "short", "GetAsyncKeyState", "int", '0x' & $sHexKey) If @error Then Return SetError(@error, @extended, False) Return BitAND($a_R[0], 0x8000) <> 0 EndFunc ; ==> _IsPressed "Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs Link to comment Share on other sites More sharing options...
taurus905 Posted January 6, 2010 Author Share Posted January 6, 2010 Melba23, Just for the sake of completion, here is my final solution: expandcollapse popup; Works using Arrow Keys or Mouse.au3 Opt('MustDeclareVars', 1) Global $cBlack = 0x000000, $cRed = 0xFF0000, $sCombo, $msg Global $hGui = GUICreate("Works using Arrow Keys or Mouse", 350, 100) GUISetBkColor($cBlack) For $i = 1 To 100 $sCombo &= $i & "|" Next Global $hCombo = GUICtrlCreateCombo("", 50, 40, 70) GUICtrlSetData(-1, $sCombo, "1") Global $hLabel = GUICtrlCreateLabel("1", 150, 40, 70) GUICtrlSetBkColor(-1, 0xFFFFFF) Global $sFlag = "None" Global $hFlag = GUICtrlCreateLabel($sFlag, 250, 40, 50) GUICtrlSetBkColor(-1, 0xFFFFFF) GUISetState(@SW_SHOW) Global $sCombo_Text = GUICtrlRead($hCombo) While 1 $msg = GUIGetMsg() Switch $msg Case $hCombo If _IsPressed("26") Or _IsPressed("28") Then ; UP ARROW or DOWN ARROW While _IsPressed("26") Or _IsPressed("28") Sleep(10) WEnd Set_Label_Data() $sFlag = "Arrow" GUICtrlSetData($hFlag, $sFlag) $sCombo_Text = GUICtrlRead($hCombo) EndIf If GUICtrlRead($hCombo) <> $sCombo_Text Then $sCombo_Text = GUICtrlRead($hCombo) Set_Label_Data() $sFlag = "Mouse" GUICtrlSetData($hFlag, $sFlag) EndIf Case -3 ; $GUI_EVENT_CLOSE Exit EndSwitch WEnd Exit Func Set_Label_Data() GUICtrlSetData($hLabel, GUICtrlRead($hCombo)) If Mod(GUICtrlRead($hCombo), 2) = 0 Then GUISetBkColor($cRed, $hGui) Else GUISetBkColor($cBlack, $hGui) EndIf EndFunc Func _IsPressed($sHexKey, $vDLL = 'user32.dll') Local $a_R = DllCall($vDLL, "short", "GetAsyncKeyState", "int", '0x' & $sHexKey) If @error Then Return SetError(@error, @extended, False) Return BitAND($a_R[0], 0x8000) <> 0 EndFunc ; ==> _IsPressed Thank you for this line which made all the difference: If GUICtrlRead($hCombo) <> $sCombo_Text Then $sCombo_Text = GUICtrlRead($hCombo)taurus905 "Never mistake kindness for weakness."-- Author Unknown --"The highest point to which a weak but experienced mind can rise is detecting the weakness of better men."-- Georg Lichtenberg --Simple Obfuscator (Beta not needed.), Random names for Vars and Funcs Link to comment Share on other sites More sharing options...
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