[Tutor] Press ESC to exit()
Alan Gauld
alan.gauld at yahoo.co.uk
Mon May 1 17:40:11 EDT 2017
On 01/05/17 19:28, Michael C wrote:
> Hi all, I found out that one way to press ESC to kill the script was to
> use my previous script language, AutoHotKey and this is how it works:
>
> ## When ESC is pressed, runs the function 'kill'
> Esc::kill()
> Is there a way to write it in Python on windows?
Yes, of course but it is very OS dependant and you would need
different code on Windows, MacOS and *nix. Its also an incredibly
risky thing to do because you have to catch the keystroke
at the highest level of the event management system which
means that any program that uses the ESC key is now broken.
To illustrate, lets assume you are running a tool that decides
it wants to format your hard drive. It pops up a window saying
it will do so in 5s unless you hit escape. You hit escape and
your script dutifully intercepts it and kills your own app,
meanwhile the tool sees no Escape and so proceeds to format
your hard drive. Is that really what you want?
There are usually better behaved ways of doing things which
will obey the conventions of the OS. But if you are absolutely
sure you want to take the risk of breaking your computer
we can talk about how you might proceed. It will not be
trivial however and nowhere near as short as your existing
AutoHotKey code. Also it will depend to some extent on
whether your program that you want to kill is itself a GUI
or a CLI.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list