Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53881 > unrolled thread
| Started by | eamonnrea@gmail.com |
|---|---|
| First post | 2013-09-09 10:39 -0700 |
| Last post | 2013-09-14 11:42 -0700 |
| Articles | 12 — 9 participants |
Back to article view | Back to comp.lang.python
Monitor key presses in Python? eamonnrea@gmail.com - 2013-09-09 10:39 -0700
Re: Monitor key presses in Python? Dave Angel <davea@davea.name> - 2013-09-09 18:19 +0000
Re: Monitor key presses in Python? John Gordon <gordon@panix.com> - 2013-09-09 18:40 +0000
Re: Monitor key presses in Python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-09 23:37 +0000
Re: Monitor key presses in Python? Nobody <nobody@nowhere.com> - 2013-09-10 02:33 +0100
Re: Monitor key presses in Python? Michael Torrie <torriem@gmail.com> - 2013-09-09 20:43 -0600
Re: Monitor key presses in Python? Grant Edwards <invalid@invalid.invalid> - 2013-09-10 14:18 +0000
Re: Monitor key presses in Python? eamonnrea@gmail.com - 2013-09-14 10:44 -0700
Re: Monitor key presses in Python? Roy Smith <roy@panix.com> - 2013-09-14 14:03 -0400
Re: Monitor key presses in Python? eamonnrea@gmail.com - 2013-09-14 11:10 -0700
Re: Monitor key presses in Python? Dave Angel <davea@davea.name> - 2013-09-14 22:03 +0000
Re: Monitor key presses in Python? Paul Rubin <no.email@nospam.invalid> - 2013-09-14 11:42 -0700
| From | eamonnrea@gmail.com |
|---|---|
| Date | 2013-09-09 10:39 -0700 |
| Subject | Monitor key presses in Python? |
| Message-ID | <57051d11-abd9-4621-9618-1574cd37545c@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-09-09 18:19 +0000 |
| Message-ID | <mailman.188.1378750802.5461.python-list@python.org> |
| In reply to | #53881 |
On 9/9/2013 13:39, eamonnrea@gmail.com wrote:
> 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.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-09-09 18:40 +0000 |
| Message-ID | <l0l4n3$sqi$1@reader1.panix.com> |
| In reply to | #53881 |
In <57051d11-abd9-4621-9618-1574cd37545c@googlegroups.com> eamonnrea@gmail.com writes:
> 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?
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-09-09 23:37 +0000 |
| Message-ID | <522e5bbd$0$29988$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #53881 |
On Mon, 09 Sep 2013 10:39:43 -0700, eamonnrea wrote:
> 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."
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Nobody <nobody@nowhere.com> |
|---|---|
| Date | 2013-09-10 02:33 +0100 |
| Message-ID | <pan.2013.09.10.01.33.17.46000@nowhere.com> |
| In reply to | #53881 |
On Mon, 09 Sep 2013 10:39:43 -0700, eamonnrea wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2013-09-09 20:43 -0600 |
| Message-ID | <mailman.198.1378781022.5461.python-list@python.org> |
| In reply to | #53881 |
On 09/09/2013 11:39 AM, eamonnrea@gmail.com wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2013-09-10 14:18 +0000 |
| Message-ID | <l0n9n1$kpu$1@reader1.panix.com> |
| In reply to | #53881 |
On 2013-09-09, eamonnrea@gmail.com <eamonnrea@gmail.com> wrote:
> 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.
--
Grant Edwards grant.b.edwards Yow! I'm having an
at EMOTIONAL OUTBURST!! But,
gmail.com uh, WHY is there a WAFFLE
in my PAJAMA POCKET??
[toc] | [prev] | [next] | [standalone]
| From | eamonnrea@gmail.com |
|---|---|
| Date | 2013-09-14 10:44 -0700 |
| Message-ID | <5c59400c-45ca-40f0-846c-05bef3eb0233@googlegroups.com> |
| In reply to | #53917 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-09-14 14:03 -0400 |
| Message-ID | <roy-B7F251.14034214092013@news.panix.com> |
| In reply to | #54173 |
In article <5c59400c-45ca-40f0-846c-05bef3eb0233@googlegroups.com>, eamonnrea@gmail.com wrote: > 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.
[toc] | [prev] | [next] | [standalone]
| From | eamonnrea@gmail.com |
|---|---|
| Date | 2013-09-14 11:10 -0700 |
| Message-ID | <bf7b8085-4994-4006-ad66-3ee4d291114e@googlegroups.com> |
| In reply to | #54174 |
I didnt wanna say that, in case people threw a fit and stuff. So yeah, how would I monitor the key presses?
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2013-09-14 22:03 +0000 |
| Message-ID | <mailman.381.1379196255.5461.python-list@python.org> |
| In reply to | #54175 |
On 14/9/2013 14:10, eamonnrea@gmail.com wrote: > 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. -- DaveA
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2013-09-14 11:42 -0700 |
| Message-ID | <7xeh8rz1m8.fsf@ruckus.brouhaha.com> |
| In reply to | #54173 |
eamonnrea@gmail.com writes: > 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web