Jump to content

HotKey does not work (HotKeySet), and can't click tray icon to exit


Recommended Posts

I'm trying to add a HotKeySet to the help-file example of "_WinAPI_ReadDirectoryChanges", but the hotkey will not work.

If I add a "Sleep()" function right before the While loop and type the hotkey during that sleep, the hotkey function runs, but once the loop is entered, hotkey doesn't work.

Also, I can't click the tray icon to exit the script (no menu appears).

Here's the example script for _WinAPI_ReadDirectoryChanges (with my hotkey additions right after the #include's):

#include <APIFilesConstants.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIError.au3>
#include <WinAPIFiles.au3>
#include <WinAPIMem.au3>

;===USER ADDED==================

HotKeySet("{ESC}", "_exit_func")

Func _exit_func()
    Exit
EndFunc

;===END USER ADDED===============

Global $g_sPath = @TempDir & '\~TEST~'

DirCreate($g_sPath)
If Not FileExists($g_sPath) Then
    MsgBox(BitOR($MB_ICONERROR, $MB_SYSTEMMODAL), 'Error', 'Unable to create folder.')
    Exit
EndIf

OnAutoItExitRegister('OnAutoItExit')

Local $hDirectory = _WinAPI_CreateFileEx($g_sPath, $OPEN_EXISTING, $FILE_LIST_DIRECTORY, BitOR($FILE_SHARE_READ, $FILE_SHARE_WRITE), $FILE_FLAG_BACKUP_SEMANTICS)
If @error Then
    _WinAPI_ShowLastError('', 1)
EndIf

Local $pBuffer = _WinAPI_CreateBuffer(8388608)

Local $aData

While 1
    $aData = _WinAPI_ReadDirectoryChanges($hDirectory, BitOR($FILE_NOTIFY_CHANGE_FILE_NAME, $FILE_NOTIFY_CHANGE_DIR_NAME), $pBuffer, 8388608, 1)
    If Not @error Then
        _ArrayDisplay($aData, '_WinAPI_ReadDirectoryChanges')
    Else
        _WinAPI_ShowLastError('', 1)
    EndIf
WEnd

Func OnAutoItExit()
    DirRemove($g_sPath, 1)
EndFunc   ;==>OnAutoItExit

 

Edited by lee321987
Link to comment
Share on other sites

  • lee321987 changed the title to HotKey does not work (HotKeySet), and can't click tray icon to exit

Hi @lee321987,

I reproduced your behavior on two Win10 machines.

@AutoItExe:     C:\Program Files (x86)\AutoIt3\autoit3_x64.exe
@AutoItVersion: 3.3.14.2
@CPUArch:       X64
@OSArch:        X64
@OSVersion:     WIN_10

A tray icon is displayed but I can not use it either 🙁 .
In a non deterministic way I get the _ArrayDisplay() output shown (in case I create/delete a file in the folder), but sometimes the script just exits 🥴 .

By the way - the only change I had to do, to run your script is:

;~ #include <WinAPIMem.au3>
#include <WinAPIDiag.au3>
#include <WinAPISys.au3>

Over all it seems to me pretty buggy, but I believe you will handle the situation by doing this kind of "watch dog job" on your own, am I possibly right?

Best regards
Sven

________________
Stay innovative!

Edited by SOLVE-SMART

Stay innovative!

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Link to comment
Share on other sites

_WinAPI_ReadDirectoryChanges blocks until a change is made. (Or the directory handle is closed, I think)

Depending what you're aiming to achieve, the solution may be to create two scripts. The second script could be called when _WinAPI_ReadDirectoryChanges detects a change maybe?

You could use HotKeySet in the second script to kill the process of the first script as well. Edit: In this case, you'd want to start both scripts at the same time otherwise HotKeySet wouldn't be functional until _WinAPI_ReadDirectoryChanges detects a change.

Edited by Luke94
Link to comment
Share on other sites

Instead of creating 2 scripts you can use ReadDirectoryChangesW API in async mode (overlapped).  It is a bit more complicated but, you get to have way more control on your program.  I believe there is an example on this forum using this API.  Search for it, if you cannot find it, I think I have an example somewhere.  LMK.

Link to comment
Share on other sites

@lee321987

you can use :

#include <WinAPIFiles.au3>
_EnumFiles("D:\op\")
Func _EnumFiles($dir = "")
    Global $prev, $prev_o = "on"
    While 1
        Global $aData = _WinAPI_EnumFiles($dir, 1, '*.*;')
        For $i = 1 To $aData[0][0]
            if $prev_o = "on" Then
                for $i2 = 1 To $aData[0][0]
                    $prev = $prev & $aData[$i2][0] & @CRLF
                Next
                $prev_o = "off"
            EndIf
            if StringInStr($prev, $aData[$i][0]) Then
            Else
                MsgBox(0, '', $aData[$i][0])
                $prev = ""
                $prev_o = "on"
            EndIf
        Next

        Sleep(50)
    WEnd
EndFunc   ;==>_EnumFiles

 

Edited by ad777

iam ِAutoit programmer.

best thing in life is to use your Brain to

Achieve

everything you want.

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