Jump to content

Keypress only with active windows


marcoauto
 Share

Recommended Posts

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:

#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
Link to comment
Share on other sites

  • Moderators

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
WEnd

All clear? :)

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

marcoauto,

Use Accelerator keys - they are only active when your GUI is active. ;)

All clear? :)

M23

It'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

Link to comment
Share on other sites

  • Moderators

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

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

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

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