marcoauto Posted February 22, 2011 Posted February 22, 2011 ciao I would do a function that starts only if a key is keypressed AND the main program window is active, but I can't do it. The keypressed function works, but evenif I have minimized my program window or ther is another windows active Where is the mistake? Thanks This is my code: expandcollapse popup#include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GUIConstantsEx.au3> $mainwindow = GUICreate("MyProgram", 826, 365, 434, 104) $Start = GUICtrlCreateButton("Start (F5)", 48, 36, 217, 217) $Play = GUICtrlCreateButton("Play (F6)", 311, 36, 217, 217) $Stop = GUICtrlCreateButton("Stop (F7)", 574, 36, 217, 217) GUISetState(@SW_SHOW) $handle = WinGetHandle("MyProgram", "") $Start_cmd= "80 0A 00 81 81 81 81 38 81 E6 E8 30 32 ED 04 00 00" $Str = StringSplit($Start_cmd," ") $Start_cmd = '' For $x = 1 to $Str[0] If $Str[$x] Then $Start_cmd &= Chr((Dec($Str[$x]))) EndIf Next $Play_cmd= "80 0A 00 81 81 81 81 38 81 E6 E8 30 33 EE 04 00 00" $Str = StringSplit($Play_cmd," ") $Play_cmd = '' For $x = 1 to $Str[0] If $Str[$x] Then $Play_cmd &= Chr((Dec($Str[$x]))) EndIf Next $Stop_cmd= "80 0A 00 81 81 81 81 38 81 E6 E8 30 34 EF 04 00 00" $Str = StringSplit($Stop_cmd," ") $Stop_cmd = '' For $x = 1 to $Str[0] If $Str[$x] Then $Stop_cmd &= Chr((Dec($Str[$x]))) EndIf Next 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 Func _StartPressed($PLayout_IP, $Playout_Name) UDPStartup() $socketA = UDPOpen($PLayout_IP, 6400) If @error <> 0 Then Exit $status = UDPSend($socketA, $Start_cmd) GUICtrlSetBkColor($Playout_Name, $Btn_Color_blu) sleep($Timeout) UDPShutdown() EndFunc Func _PlayPressed($PLayout_IP, $Playout_Name) UDPStartup() $socketA = UDPOpen($PLayout_IP, 6400) If @error <> 0 Then Exit $status = UDPSend($socketA, $Play_cmd) GUICtrlSetBkColor($Playout_Name, $Btn_Color_green) sleep($Timeout) UDPShutdown() EndFunc Func _StopPressed($PLayout_IP, $Playout_Name) UDPStartup() $socketA = UDPOpen($PLayout_IP, 6400) If @error <> 0 Then Exit $status = UDPSend($socketA, $Stop_cmd) GUICtrlSetBkColor($Playout_Name, $Btn_Color_green) sleep($Timeout) UDPShutdown() EndFunc While 1 $nMsg = GUIGetMsg() $state = WinGetState("MyProgram", "") If BitAnd($state, 8) Then ;check if the main windows is active If _IsPressed(74) Then ;is F5 (Start) pressed? _CheckStart() EndIf If _IsPressed(75) Then ;is F6 (Play) pressed? _CheckPlay() EndIf If _IsPressed(76) Then ;is F7 (Stop) pressed? _CheckStop() EndIf EndIf Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Start _CheckStart() Case $Play _CheckPlay() Case $Stop _CheckStop() EndSwitch WEnd
Moderators Melba23 Posted February 22, 2011 Moderators Posted February 22, 2011 marcoauto,Use Accelerator keys - they are only active when your GUI is active. I have removed a lot of code so you just get the essentials of how to set up the Accelerator keys:#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> $mainwindow = GUICreate("MyProgram", 826, 365, 434, 104) $Start = GUICtrlCreateButton("Start (F5)", 48, 36, 217, 217) $Play = GUICtrlCreateButton("Play (F6)", 311, 36, 217, 217) $Stop = GUICtrlCreateButton("Stop (F7)", 574, 36, 217, 217) GUISetState(@SW_SHOW) ; Set accelerators Dim $AccelKeys[3][2]=[["{F5}", $Start], ["{F6}", $Play], ["{F7}", $Stop]] ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<< GUISetAccelerators($AccelKeys) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $Start ConsoleWrite("Starting" & @CRLF) ;_CheckStart() Case $Play ConsoleWrite("Playing" & @CRLF) ;_CheckPlay() Case $Stop ConsoleWrite("Stopping" & @CRLF) ;_CheckStop() EndSwitch WEndAll clear? 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
marcoauto Posted February 22, 2011 Author Posted February 22, 2011 All clear? M23Fantastic!! It works and it's easy (now that I know how to implement it)Thanks againmarco
marcoauto Posted February 22, 2011 Author Posted February 22, 2011 marcoauto,Use Accelerator keys - they are only active when your GUI is active. All clear? M23It's work with all windows, but with a vnc viewer windows it doesn't work.If I open a vnc client and i click on it, the F5, F6 and F7 keys work evenif my autoit program windows is deselect :-(Is it a bug?marco
Moderators Melba23 Posted February 22, 2011 Moderators Posted February 22, 2011 marcoauto,Is it a bug?I have no idea and anyway I have no knowledge of vnc. I suggest you open a new thread and see if someone else can offer an opinion. 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
AutoBert Posted February 27, 2011 Posted February 27, 2011 Hello marcoauto, just place a Commandline for debuggin in your Skript and you can see why it fails:While 1 $nMsg = GUIGetMsg() $state = WinGetState("MyProgram", "") If BitAND($state, 8) Then ;check if the main windows is active ConsoleWrite($state & @CRLF) ;what is the State???? just for debugging autoBert
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