Jump to content

HotKeySet only on focus


ricky
 Share

Recommended Posts

Hello,

I create a program with :

HotKeySet("{ENTER}", "_Search")
HotKeySet("{F3}", "_Search")

When the focus is on this program, no problem, but if I work with another program, the keys are blocked only for this program. It's possible to activate this function only when this program is focused?

Thanks for your help

Link to comment
Share on other sites

  • Moderators

Ricky,

Look at GUISetAccelerators - like HotKeys but only active when the GUI is active.

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

Thanks for your answers, I already tried with GUISetAccelerators, but the program in an infinite loop.

How to reset the GuiGetMsg()?

For info : Opt('GUIOnEventMode', 1) is set in the program

 

@JohnOne, I don't understant what can I do with winactive. Could you please be more explicit?

Edited by ricky
Link to comment
Share on other sites

  • Moderators

ricky,

That makes no sense at all - all AutoIt scripts have to have an infinite loop to stay alive. And what does GUIGetMsg have to do with it if you are using OnEvent mode?

If you are unsure how to use GUISetAccelerators in OnEvent mode then here is the Help file example converted to that mode:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Opt('GUIOnEventMode', 1)

Example()

Func Example()
    GUICreate("Custom MsgBox", 225, 80)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")


    GUICtrlCreateLabel("Please select a button.", 10, 10)
    Local $idYes = GUICtrlCreateButton("Yes", 10, 50, 65, 25)
    GUICtrlSetOnEvent($idYes, "_Yes")
    Local $idNo = GUICtrlCreateButton("No", 80, 50, 65, 25)
    GUICtrlSetOnEvent($idNo, "_No")
    Local $idExit = GUICtrlCreateButton("Exit", 150, 50, 65, 25)
    GUICtrlSetOnEvent($idExit, "_Exit")

    ; Set GUIAccelerators for the button controlIDs, these being Ctrl + y and Ctrl + n
    Local $aAccelKeys[2][2] = [["^y", $idYes], ["^n", $idNo]]
    GUISetAccelerators($aAccelKeys)

    GUISetState(@SW_SHOW) ; Display the GUI.

    While 1
        Sleep(10)
    WEnd



EndFunc   ;==>Example

Func _Close()
    MsgBox($MB_SYSTEMMODAL, "You selected", "Close")
    Exit
EndFunc



Func _Yes()
    MsgBox($MB_SYSTEMMODAL, "You selected", "Yes")
    Exit
EndFunc



Func _No()
    MsgBox($MB_SYSTEMMODAL, "You selected", "No")
    Exit
EndFunc



Func _Exit()
    MsgBox($MB_SYSTEMMODAL, "You selected", "Exit")
    Exit
EndFunc

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

Try this

;Variable to remember hotkey status. This prevents unnecessary work on the CPU constantly Re-(Dis)enabling hotkeys.
Global $HotkeyStatus = False

;Launch a Notepad to test Hotkey focus.
ShellExecute("notepad.exe")

;Wait for a Window with the Class:Notepad to Exist.
WinWait("[CLASS:Notepad]")

;Get Handle of Class:Notepad; Assign to Variable $handle.
Global $handle = WinGetHandle("[CLASS:Notepad]")

;Idle Function. This keeps the script alive.
idle()
Func idle()

    ;This is the Idle Loop.
    While 1

        ;If Window is Active(Focused) then enable Hotkeys.
        If WinActive($handle) = True Then
            If Not $HotkeyStatus = True Then StartHotkeys()

        ;Else Window is Not Active(Focused) and disables the Hotkeys.
        Else
            If Not $HotkeyStatus = False Then StopHotkeys()
        EndIf

        ;Sleep for the duration of a single frame to render onscreen.
        Sleep(1000 / @DesktopRefresh)

        ;If Window does not exist, exit script.
        If Not WinExists($handle) Then Exit

    ;ends the Idle Loop.
    WEnd

EndFunc   ;==>idle

Func StopHotkeys()
    ;Stops the Hotkey.
    HotKeySet("{space}")

    ;Track the Hotkey Status
    $HotkeyStatus = False
EndFunc   ;==>StopHotkeys

Func StartHotkeys()
    ;Starts the Hotkey.
    HotKeySet("{space}", "FunctionA")

    ;Track the Hotkey Status
    $HotkeyStatus = True
EndFunc   ;==>StartHotkeys

Func FunctionA()
    ;A simple message box to inform the user that the hotkey is working.
    MsgBox(0, "Space Pressed", "")
EndFunc   ;==>FunctionA

 

 

 

Link to comment
Share on other sites

  • 3 years later...

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