[Tutor] operating system key-bindings that call Python scripts
alan.gauld at btinternet.com
Tue Aug 21 09:53:46 CEST 2007
<tpc247 at gmail.com> wrote
> (Windows or Linux) to recognize that every time I type, say,
> <Ctrl>-'C' '1',
> a Python script I wrote will execute and out will pop the current
> date time
Ctrl-C is a particularly difficult combination since in many OS it
is the system interrupt but if you went for a somewhat less
choice like Ctrl-T say, then it becomes a little bit easier to
> in my desired format. I want this to happen not just in my Xemacs
> editor buffer, but inside a textfield for, say, the Gmail
> application in
> Mozilla, in Python IDLE, in the Google desktop search bar and at the
This is truly difficult since each application has its own mechanism
grabbing keystrokes. Xemacs indeed has two mechanisms, one for
console mode and one for GUI mode! The GUI has its own key
handling events (but subverts system combinations like Ctrl-C as
mentioned above) and the OS command line is the hardest of all unless
you restrict it to a GUI console window in which case you can catch
it at the GUI level. Alternatively you can write your own shell!
Catching the keypresses at a global level in a GUI is possible but you
then have the problem of identifying the original target widget and
manipulating that after running your python code. You also need to
post on the original keystroke since the target application might
use it for something else!
> *) the Apple OS is ideally suited to doing what you want, and
> learning AppleScript.
> In Windows or Linux it's a lot more complicated to
> bind a sequence of keys to call a Python program that generates
> output in
> any graphical window
Linux largely depends on the GUI in use. Most sit on top of X and
be manipulated at that level but it involves very low level coding in
Windows is not too difficult to catch the keystrokes and execute a
python script (use WinExec() ) but getting the result back into your
target application will be tricky, probably involving use of a lot of
> and, in fact, it may be impossible or more trouble than
> it's worth
Certainly difficult and very open to a trial and error approach and
then could result in some spectacularly unpredictable behaviour that
could seriously damage your applications/data! Basically you are
to change the OS behaviour. Its the OS job to detect key presses and
direct them to their destination, to change that you have to interact
at a fundamental level with the OS keyhandling.
> Have any of you ever successfully engineered an operating system
Yes, done this in VB and C++
> that calls a Python script and directs the output where the cursor
> is in any
> graphical window in Windows and Linux ?
I've separately routed characters to another window in Windoze using
> I guess my expectation should be the answer is no, in which case I'd
> happy just to find out if it's possible to create key-bindings in
> Xemacs to
> call a Python program.
Calling Python is easy because there are standard emacs functions for
executing a shell command (which includes python) and for scraping
the output into a text buffer and pasting that wherever you want. But
will involve some elisp.
But most of what you want is not possible in pure python (except
possibly in Windows) it will require a fair bit of coding in C,
Applescript etc as well
> well, in which case my next step is to port the following to elisp:
> def output_current_datetime_in_my_desired_format():
> import datetime
> print datetime.datetime.now().strftime("%d%b%Y%a
Frankly, for emacs, that would be the easiest since the elisp required
to call your Python is almost certainly harder than the elisp to print
some formated date/time stuff!
And every emacs user should know the basics of elisp, without it
you are losing a large part of the power of emacs!
Author of the Learn to Program web site
More information about the Tutor