ricky

HotKeySet only on focus

8 posts in this topic

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

Share this post


Link to post
Share on other sites



Ricky,

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

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

Share this post


Link to post
Share on other sites

Maybe his "infinite" loop isn't a GUI loop? :unsure:


Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
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

 


My Scripts:

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

AutoISO  - Automatic ISO Image creation using ImgBurn.

SoundBoard - Play any song or sound you want at the press of a hotkey

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