I am creating a hotkey as follows:
import keyboard
keyboard.add_hotkey ('win+esc', close_application)
The close_application function is what the hotkey calls is:
def close_application ():
keyboard.unhook_all_hotkeys()
sys.exit()
Problem:
Application never exist, as a sub thread creating automatically by keybard.hotkey function, never closes:
I tried all different ways to exit from the application as well as some other methods of keyboard module to release the hotkey, but none was successful.
May I ask if you have any idea how can i force to close the thread opened by the keyboard module ?
Related
I'm trying to create a keylogger using the Python keyboard library. Currently I have a start function like this:
def start(self):
# record the start datetime
self.start_dt = datetime.now()
# start the keylogger
keyboard.on_release(callback=self.callback)
# make a simple message
print(f"{datetime.now()} - Started keylogger")
# block the current thread, wait until CTRL+C is pressed
keyboard.wait("esc")
self.report()
In my application when I click a button it creates a KeyLogging object and calls this start function. However, when I click the button it freezes my application because this keyboard.wait method blocks the main thread until this keyboard thread finishes executing. Is there a way to not block the main thread and have this work in the background like an actual keylogger? I'd still like it to exit when "esc" is pressed. Thanks!
I am trying to implement some basic close out tasks for an automation script I have written in python using pyautogui. I open a program and click certain buttons every day to create daily reports. But if something goes wrong, I need it to close the program it is using.
I tried using atexit.register, but it didn't seem to run when I trigger sys.exit(). So I wrote a test script to verify that it was triggering, and it isn't.
import atexit as a
import sys
def closeout():
print("atexit triggered, closing out")
print("starting program")
print("Registering closeout with atexit")
a.register(closeout)
print(r"triggering sys.exit()")
sys.exit()
it should print out the following:
"starting program"
"Registering closeout with atexit"
"triggering sys.exit()"
"atexit triggered, closing out"
but I don't get the last line.
Any thoughts? I'm running 3.7.2 if it matters.
When you "run module" from IDLE, it uses the compile builtin to build a code object from the associated file, and then runs it with exec. The process that runs the code doesn't exit, and SystemExit is caught and silently ignored in the IDLE interpreter - otherwise a script that included it would cause IDLE to quit! - so the atexit handler isn't run.
Furthermore, the default build of IDLE is configured to delete any register atexit functions when exiting, so your handler isn't run when IDLE itself eventually exits. (See the exit function in the cpython repo.)
Basically I am writing a script that can be stopped and resumed at any time. So if the user uses, say PyCharm console to execute the program, he can just click on the stop button whenever he wants.
Now, I need to save some variables and let an ongoing function finish before terminating. What functions do I use for this?
I have already tried atexit.register() to no avail.
Also, how do I make sure that an ongoing function is completed before the program can exit?
Solved it using a really bad workaround. I used all functions that are related to exit in Python, including SIG* functions, but uniquely, I did not find a way to catch the exit signal when Python program is being stopped by pressing the "Stop" button in PyCharm application. Finally got a workaround by using tkinter to open an empty window, with my program running in a background thread, and used that to close/stop program execution. Works wonderfully, and catches the SIG* signal as well as executing atexit . Anyways massive thanks to #scrineym as the link really gave a lot of useful information that did help me in development of the final version.
It looks like you might want to catch a signal.
When a program is told to stop a signal is sent to the process from the OS, you can then catch them and do cleanup before exit. There are many diffferent signals , for xample when you press CTRL+C a SIGINT signal is sent by the OS to stop your process, but there are many others.
See here : How do I capture SIGINT in Python?
and here for the signal library: https://docs.python.org/2/library/signal.html
I am developing a Python application and I open ports using uPnP. The problem is that when the application is forced to be closed by process, the port is still opened. So I'm searching for an event in PyQt (or something else) to do some actions when the process is closing. Be sure that I'm not talking about the close button.
QApplications aboutToQuit signal appears to be built exactly for this purpose. Simply add a slot to your code and connect to this signal. Your slot should be notified before the application quits.
From the pyQT docs:
This signal is emitted when the application is about to quit the main event loop, e.g. when the event loop level drops to zero. This may happen either after a call to quit() from inside the application or when the users shuts down the entire desktop session.
The signal is particularly useful if your application has to do some last-second cleanup. Note that no user interaction is possible in this state.
(emphasis mine)
I currently have a Console based python program running under windows. The program maintains most of its data in memory and periodically saves the data to disk, or when the user shuts the application down via a Keyboard interrupt (Ctrl + C) event.
The problem i have is that when a user hits the "X" button at the top right of the console window, the session closes and the data in memory is lost. What i am looking for is an event/signal or hook so that i can clean up the memory before closing down.
I am hoping to do this without any external libraries, though if this is not possible i'd still like to know how it can be done.
In windows
if you are using pywin32, you can perform an event before the console is closed, I'm not sure this will tell you who or what is closing it, but maybe this gets you half way.
You might also want to check out: Prevent a console app from closing ...
def on_exit(signal_type):
print('caught signal:', str(signal_type))
import win32api
win32api.SetConsoleCtrlHandler(on_exit, True)
For those who come across this and use Linux...
the SIGHUP signal is thrown (signal hang up) when you close an SSH session/window.
import signal
signal.signal( signal.SIGHUP, handler )
def handler(signum, frame):
#this is called when the terminal session is closed
#do logic before program closes
pass