Monitor key presses in Python?

E

eamonnrea

Is there a way to detect if the user presses a key in Python that works on most OS's? I've only seen 1 method, and that only works in Python 2.6 and less. If you get the key, can you store it in a variable?

Also, is there a way to create a callback in Python?
 
D

Dave Angel

Is there a way to detect if the user presses a key in Python that works on most OS's? I've only seen 1 method, and that only works in Python 2.6 and less. If you get the key, can you store it in a variable?

Also, is there a way to create a callback in Python?

What is usually meant by "a callback" is a function object. In Python,
functions are first class objects. You just use the function name
without the parentheses.

def my_function():
print "Executing my_function"

b = my_function # b is now a function object

b()

Likewise, instead of storing it in a global, you might pass it to a
method which stores it as an object attribute, or whatever.

Also of interest is that you can easily create partial functions, where
some of the parameters are already decided. See the docs for
functools.partial

And if you're trying to use a method as a callback, you can store the
bound-method, which is effectively a partial including the self
parameter.

Finally, don't forget lambda functions, which can be useful if you're
trying to create a simple function and don't need a name for it.
 
J

John Gordon

In said:
Is there a way to detect if the user presses a key in Python that works on
most OS's?

That depends on what you're really asking; your question is somewhat vague.

Are you asking for a function that waits for the user to press a key and
then returns the key that was pressed?

Or are you asking for a function that detects whether a key has been
pressed at all?
 
S

Steven D'Aprano

Is there a way to detect if the user presses a key in Python that works
on most OS's? I've only seen 1 method, and that only works in Python 2.6
and less.

http://code.activestate.com/recipes/577977

I have just tried the above under Linux in Python 3.3, and it works fine.
I have no way of testing it under Windows.

If you get the key, can you store it in a variable?

You're new to programming, aren't you? :)

Yes you can store it in a variable. Anything that is returned can be
stored in a variable. Here is an example:


py> def example():
.... print("Press any character key... ")
.... c = getch()
.... print("You typed: '%c'" % c)
....
py> example()
Press any character key...
You typed: 'y'


The above is running under Python 3.3.

Also, is there a way to create a callback in Python?

The answer to your question as you ask it is "Yes, naturally." Callback
is short for *callback function* and describes the *purpose* of the
function, not how you write it or what it does.

But a better answer is, "A callback to what? It depends on what is doing
the calling back."
 
N

Nobody

Is there a way to detect if the user presses a key in Python that works on
most OS's? I've only seen 1 method, and that only works in Python 2.6 and
less.

There's no "generic" solution to this.

At a minimum, there's getting "key presses" from a windowing system and
getting character input from a terminal or console. The two cases are
themselves quite different, and each case has differences between
operating systems.
 
M

Michael Torrie

Is there a way to detect if the user presses a key in Python that
works on most OS's? I've only seen 1 method, and that only works in
Python 2.6 and less. If you get the key, can you store it in a
variable?

Also, is there a way to create a callback in Python?

Some python programs display a graphical user interface. Others run in
a text-mode console (dos prompt, unix shell, etc). And yet others don't
have any display at all. If you're talking about a graphical user
interface app (windows, dialogs, buttons, etc), then you'll have to rely
on the particular user interface library you are using to provide that
sort of access. If you're just running in a dos box, or a unix
terminal, then there are other ways of doing what you want, but I'm not
sure any one way is portable across all operating systems. I did find
this code segment that claims to work on windows and unix:

http://code.activestate.com/recipes/134892/

Anyway tell us more about what environment and kind of program you are
dealing with.

As for callbacks, of course. functions are objects in python. You can
pass them as arguments, assign them to variables, and then call them.
All graphical user interface libraries rely on them to handle events.
 
G

Grant Edwards

Is there a way to detect if the user presses a key in Python that
works on most OS's?

No. Unless by "most OSes" you mean "most Unixes" or "most Windows".
I've only seen 1 method, and that only works in
Python 2.6 and less.
If you get the key, can you store it in a variable?
Sure.

Also, is there a way to create a callback in Python?

Yes.
 
E

eamonnrea

It might sound strange, but I'd need this to run without the user knowing.I believe I can do this by making the file a .pyw file, and use a GUI library to monitor the key presses. I think I could use PyGame, PyGlet and/or Cocos2d as well.
 
R

Roy Smith

It might sound strange, but I'd need this to run without the user knowing.

It's called a keylogger. And after all the revelations about the NSA
over the past few weeks, it doesn't sound strange at all.
 
E

eamonnrea

I didnt wanna say that, in case people threw a fit and stuff.

So yeah, how would I monitor the key presses?
 
P

Paul Rubin

I'd need this to run without the user knowing.

You are asking for programming advice when you should probably be asking
for legal advice instead, about interception of electronic
communications. We are not qualified to give legal advice here.
 
D

Dave Angel

I didnt wanna say that, in case people threw a fit and stuff.

So yeah, how would I monitor the key presses?

There's a huge difference between monitoring key presses within your own
process, and intercepting them system-wide. if you need to see
keystrokes even when your window does not have the focus, then you need
to call some system DLL function, (some kind of "system hook", which
you can probably do using the platform module, or the ctypes module, or
some module that I wouldn't have on Linux. This type of code is not the
least bit portable, which just means I can't test things here to try to
help.

Note also that if you get one of the ActivePython implementations from
ActiveState, you'll get PyWin32, which may well have a function just for
the purpose. See:

http://docs.activestate.com/activepython/2.5/pywin32/PyWin32.HTML

I'm sure it can be separately downloaded, but when I used to run
Windows, I just got the whole package. The ActivePython also had the
best offline documentation I was able to find at the time.

See also Tim Golden's win32 web page:

http://timgolden.me.uk/python/win32_how_do_i.html

There is a separate win32 mailing list; see:

https://mail.python.org/mailman/listinfo/python-win32

Possibly the most frequent poster there is Tim Golden, so search his
site first.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top