PythonScript: Check If Notepad++ Was Closed To Terminate an Infinite Loop
-
Try registering a callback on
NOTIFICATION.SHUTDOWN
, which is triggered when Notepad++ is asked to shut down.here is an example which registers that callback to write to
c:\temp\onShutdown.txt
*# encoding=utf-8 """in response to https://community.notepad-plus-plus.org/topic/22919/ this is an example of registering a NOTIFICATION.SHUTDOWN callback the example just writes to c:\temp\onShutdown.txt with the date/time and the arguments passed """ from Npp import * from datetime import datetime def myOnShutdown_22919(kwargs): with open("c:\\temp\\onShutdown.txt", 'a') as f: f.write("{}\tshutdown args: {}\n".format(datetime.now().strftime('%Y-%m-%d-%H:%M:%S'), ', '.join(["{}=>{}".format(k,kwargs[k]) for k in kwargs]))) pass notepad.callback(myOnShutdown_22919, [NOTIFICATION.SHUTDOWN]) console.write("registered [NOTIFICATION.SHUTDOWN] = myOnShutdown_22919\n") with open("c:\\temp\\onShutdown.txt", 'a') as f: f.write("{}\tregistered myOnShutdown_22919\n".format(datetime.now().strftime('%Y-%m-%d-%H:%M:%S')))
Once you have that working right, put in a call which disables your “hold down the key” logic.
But as we’ve said before, this is a bad direction to be going in. As you can see, it continues to cause headaches… I am thinking of another way to implement similar functionality without a hack.
-
P PeterJones referenced this topic on
-
@peterjones Interestingly enough writing to a file like this works just fine, I just did so to test out the callback function. Let me detail the issue I am having a bit further:
My script holds the alt key for column select and releases it at times that are necessary, this all happens in a large infinite loop so the script can act as a toggle.
My script will stop holding alt if it detects that text has been selected, this allows the user to enter the tab key without using the Windows Alt+Tab function. The script will resume holding the alt key once it detects that there is no text selected so that column select can continue to be used.
If the user toggles on my script and selects some text and then closes Notepad++ everything is okay because the Alt key was released when the text was selected.
However, if the user toggles on my script and does NOT select any text (alt is currently being held down) then closes Notepad++, if they enter tab the tab key anywhere in Windows the Alt+Tab function occurs because the script must have left the alt key held down even when Notepad++ closed.
I used the callback function with NOTIFICATION.SHUTDOWN to release the Alt key, but for some reason that does not fix the problem. Which is strange because it will still write to the test file I tested it with, that part works fine.
-
@john-doe-1 said in PythonScript: Check If Notepad++ Was Closed To Terminate an Infinite Loop:
My script holds the alt key for column select and releases it at times that are necessary,
Your paradigm is BAD and causing you problems.
Try this instead. -
@alan-kilborn Hey I tried testing the callback function with SHUTDOWN like so in a simple Python script:
notepad.callback(my_callback(), [NOTIFICATION.SHUTDOWN])
Where my_callback() is a function that writes “Hello world!” to a text file. I noticed that as soon as the script executes that text file gets “Hello world!” written to it, isn’t that not supposed to happen until Notepad++ is closed/shutdown? I thought that was the purpose of the [NOTIFICATION.SHUTDOWN]
-
@john-doe-1 said in PythonScript Toggleable Script?:
Where my_callback() is a function that writes…
my_callback()
is the invocation of that function – you’re running it right there, that’s why you see output immediately.You want simply
my_callback
there, like this:notepad.callback(my_callback, [NOTIFICATION.SHUTDOWN])
No
()
after it. -
I used my moderator powers to move your “hey” post from the “toggle” discussion to the “terminate on close” discussion (and moved Alan’s reply).
Unlike the last post, where you started talking about it in one then created another for the same conversation, this is a valid separation of concepts, and it will help keep the conversations separate. Especially since going down this “I take control of your ALT key” is the wrong way to go for your end goal. But the shutdown notification is an interesting aside.
You don’t need to be working on it any more, because you shouldn’t be going down this path for your project. Because it is the wrong path. But if you are still interested in talking about the shutdown event from a learning standpoint, keep that discussion here. And Alan’s explanation is why it triggered at the “wrong” time.
-
@alan-kilborn said in PythonScript: Check If Notepad++ Was Closed To Terminate an Infinite Loop:
You want simply my_callback there, like this:
notepad.callback(my_callback, [NOTIFICATION.SHUTDOWN])Hey I just tried that out and now it doesn’t write “Hello world!” to the text file at all. Not upon execution and not after closing Notepad++
-
@john-doe-1 said in PythonScript: Check If Notepad++ Was Closed To Terminate an Infinite Loop:
Hey I just tried that out and now it doesn’t write “Hello world!” to the text file at all. Not upon execution and not after closing Notepad++
Then you are doing something wrong. But since we are not looking over your shoulder, we cannot tell you what.
- Does my exact script work for you?
- if so, then you can start comparing what’s different between my script and yours that is causing yours not to work
- if not, then you are doing something wrong, because it works every time for me. Explain the exact procedure that makes you think it should be active but is not working.
- Run script that registers the callback – it should only register the callback once
- Exit Notepad++
- Look at
c:\temp\onShutdown.txt
and see that it wrote to the file
- You have to be careful in NOTIFICATION.SHUTDOWN callbacks
- They cannot do too much, otherwise Notepad++ might finish shutting down before they have run.
- For example, my original debug callback tried to do a
notepad.messageBox()
call – the message box never showed, because Notepad++ was beyond the point of being able to do GUI like MessageBox.
- When I was debugging, I started with
notepad.callback(myOnShutdown_22919, [NOTIFICATION.SHUTDOWN, NOTIFICATION.BUFFERACTIVATED])
, so that I could get the callback to run by just activating tabs; this allowed me to debug that there were no errors (I originally had some, because I forgot to double-backslash the paths). You may have to debug
As a reminder, this is not the right solution for your column-selection issue. Do not continue down this path if your goal is still to get your column-selection hack to work.
- Does my exact script work for you?
-
@peterjones Okay I’ve done some more testing, and writing to a file after shutdown is working. Strangley the statement:
pyautogui.keyUp(“alt”)
Does nothing after shutdown even though it is placed right above the statement for writing to a file, I even tried including the library again in the my_callback definition and no luck. Its strange that this statement refuses to work properly in the shutdown callback but writing to a file works no problem.
-
Its strange that this statement refuses to work properly in the shutdown callback but writing to a file works no problem(said above)
Works with my_callback upon shutdown SOMETIMES (said below)It might be that GUI handles (hWnd and similar) have already been deleted/cleared by the time it gets that far (or at least sometimes), so whatever it is that
pyautogui.keyDown()
operates on might not exist anymore. Yet another reason that the SHUTDOWN callback might not be viable for every circumstance (especially yours). -
@peterjones Couldn’t edit so I did a double reply, sorry. Okay I just noticed what’s even stranger is that the statement:
pyautogui.keyDown(“alt”)
Works with my_callback upon shutdown SOMETIMES. It is the strangest thing, I will run the script and then close out Notepad++ and hit Tab to see if it uses the Windows Alt+Tab feature (indicating Alt is still being held after shutdown). Sometimes it will Alt+Tab me (meaning Alt is still being held down and the callback didn’t work) and other times it will work perfectly and simply Tab (meaning Alt was properly released during the callback).
It seems completely random like a coin flip as to which result will occur, any idea why this may be?
-
(I was in the middle of an edit when you posted)
To finish my thought:
I believe the intention of the SHUTDOWN notification handler was to allow plugins to clean up their memory/objects/background files nicely when Notepad++ exits. It wasn’t intended as a place to do any GUI changes (including any win32 api calls like keyboard handlers and the like, which is presumably what
pyautogui.keyDown
works with).No application or plugin or script should ever affect the keyboard or mouse input for the whole OS (like your persistent modification that lasts after Notepad++ exits) without the documentation making it very clear that it intentionally does so, and without the author of said application/plugin/script fully understanding all the nuances involved – and I’m sorry, but your questions and difficulties in implementing this tell me that you don’t understand the nuances. If I ever found that a Notepad++ plugin author or the author of a PythonScript script for Notepad++ ever messed with the keyboard in such a way that if I alt+tab to anther application, it will have hijacked my keyboard so that my keyboard doesn’t work as expected, or if Notepad++ exits and it still has hijacked my keyboard, I will immediately delete that plugin or script, and never trust anything written by that developer again. (The only application that I would be willing to have hijack the keyboard even when you are in another application is something like AutoHotKey, whose sole purpose is to provide hotkeys in situations where local hotkeys aren’t an option.)
-
@peterjones Yeah that is my concern, I’m gonna work on this some more later to see if I can get the script all cleaned up so that is no longer an issue. Thanks for your help so far!