472,983 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 software developers and data experts.

Catching keystrokes under Windows

Hi,

I'm having a little problem catching keystrokes under Windows. I did a
little research and found that with mscvrt.getch() one can cath a single
key that is pressed. However this doesn't work when the program is run
on the backround and not as the primary task. What I would like to have
is a same sort of function that would also work when the program is being
run on the background. I'm trying to implement a program that would listen
for a certain key to be pressed and then start a timer that would *beep*
after 30 seconds or so. I've everything else sorted out except how to
catch the one key.

So is there an easy way like msvcrt.getch() is or does one have to start
playing with the win32api module? If the win32api-module is needed for this
task could you also explain how? Python is my first real programming
language so I don't understand too much about C\C++ stuff. Examples would
be highly appreciated.
Jul 18 '05 #1
2 4918
Olli Piepponen wrote:

I'm having a little problem catching keystrokes under Windows. I did a
little research and found that with mscvrt.getch() one can cath a single
key that is pressed. However this doesn't work when the program is run
on the backround and not as the primary task. What I would like to have
is a same sort of function that would also work when the program is being
run on the background. I'm trying to implement a program that would listen
for a certain key to be pressed and then start a timer that would *beep*
after 30 seconds or so. I've everything else sorted out except how to
catch the one key.

So is there an easy way like msvcrt.getch() is or does one have to start
playing with the win32api module? If the win32api-module is needed for this
task could you also explain how? Python is my first real programming
language so I don't understand too much about C\C++ stuff. Examples would
be highly appreciated.


You absolutely cannot use msvcrt for this, as it works only with console
programs (i.e. DOS window programs).

You do have to use the win32 stuff, but at that point it becomes mostly
a non-Python question: you will have to research how this is done in
Windows in general, then come back here with the results of that research
and ask how to duplicate it in Python. That part is easy, but the research
is your job.

Note that "background" and "primary task" have little meaning here...
there is no "primary task" under Windows, although there is the concept
of the active window (the top-most one generally). You're probably asking
for a way of "hooking" in to the OS itself, in the same way that something
like Explorer intercepts Alt-Tab no matter where it's typed...

-Peter
Jul 18 '05 #2
Olli Piepponen:
I'm having a little problem catching keystrokes under Windows.
I did a little research and found that with mscvrt.getch() one
can cath a single key that is pressed. However this doesn't work
when the program is run on the backround and not as the
primary task. What I would like to have is a same sort of
function that would also work when the program is being run
on the background. I'm trying to implement a program that
would listen for a certain key to be pressed and then start a
timer that would *beep* after 30 seconds or so. I've
everything else sorted out except how to catch the one key.

So is there an easy way like msvcrt.getch() is or does one have
to start playing with the win32api module? If the
win32api-module is needed for this task could you also explain
how? Python is my first real programming language so I don't
understand too much about C\C++ stuff. Examples would be
highly appreciated.
To do this for all Windows applications, you need to use a systemwide
message hook, which is set with the Windows function SetWindowsHookEx using
the WH_KEYBOARD option. The hook code must be in a DLL (dynamic link
library) and should be written in a language like C or C++.

This is a fairly advanced bit of Windows programming, and you wouldn't want
to tackle it without some prior experience in both Windows and C/C++.
However, Pete Parente wrote a DLL that does this keystroke interception and
calls back to Python code to handle the keystrokes. I haven't looked at
Pete's code yet, but you can find it here:

http://sourceforge.net/projects/uncassist

pyHook is what you're looking for on that page. Below is Pete's original
message with some more information.

Hope that helps,

Mike

"Peter Parente" <pa*****@cs.unc.edu> wrote in message
news:85**************************@posting.google.c om... Change of plans. The hooks library is at
http://www.sourceforge.net/projects/uncassist. You can download and
run the pyHook windows installer, or check it out of CVS. There's an
example.py file in the CVS pyHook module that shows you how to use it
(requires wxPython for the GUI).

Pete

pa*****@cs.unc.edu (Peter Parente) wrote in message

news:<85**************************@posting.google. com>...
I have a Python library that can capture system wide keystrokes and
mouse events. Basically, the windows hooking code is in a C Python
extension that passes callback information back to Python when an
event occurs. I'll
post it to our Python tools sourceforge site in the near future. (I
need to clean it up a bit and provide an example before it will be the
least bit useful to you.)

When it's posted, it will appear at
http://sourceforge.net/projects/uncpythontools/ as the pyHook project.

Pete
hokieghal99 <ho********@hotmail.com> wrote in message news:<bi**********@solaris.cc.vt.edu>...
Does anyone know of a keystroke logger that has been written in Python
for Windows machines? I'd like to see such a script and use it as a
point of reference for a real-time backup project that I'm working on.
Basically, I'd like to be able to capture all keystrokes from the
keyboard buffer and write them to a text file so I could reporduce
emails, documents, etc. in the event of file loss that occurs between
nightly backups. For example, my boss comes to me, he has deleted an
email that he was writing... he has been working on the email all day... and he expects me to wave a magic wand and bring it back.

Thanks for any advice, code or pointers. Also, if Python isn't suited
for this, let me know and I'll look at doing this in c

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Michael Howes | last post by:
I'm trying to handle a number of keystrokes in a TextBox and for other reasons I'm using the KeyDown I'm trying to know when Shift-Tab is pressed and the following code doesn't work if ( (...
5
by: Lucas Tam | last post by:
Hi all, Has anyone done this before? Send client side keystrokes to an application from a webpapge? This maybe more of a Javascript question. In short, I have an application on the client's...
4
by: SStory | last post by:
I want to use F6,F7,etc for an entire form. Have key preview to true and it works everywhere but on the grid. On the grid sometimes it works and sometimes not. Appears that if I am in a cell...
3
by: Phil Galey | last post by:
In VB6 you can block invalid keystrokes in the KeyPress event by setting KeyAscii = 0. How can you block invalid keystrokes in VB.NET?
2
by: Tim Diggle | last post by:
I'm trying to enter data into a database application running under Citrix ICA Client. I'm doing it from a VBA macro running under Microsoft Excel 2000 (although it may need to be backwardly...
1
by: Tor Inge Schulstad | last post by:
Hi group! I want to make a windows service application that counts all the keystrokes a user does on the computer. Can anyone give me a hint on how i can detect those keystrokes when they are...
3
by: Gunnar Syren | last post by:
I'm trying to implement a macro feature in my application by recording and playing back keystrokes. At first I thought it would be enough to catch KeyDown in my main form, but I soon realized that...
3
by: Robert Inder | last post by:
I am struggling to catch kestrokes within an Internet Explorer 6 window. My window happens to be displaying three frames, though I suspect a similar problem would arise with a single document. ...
1
by: zanthor | last post by:
Ok... so call me crazy, but I have an idea and I'm looking for where to start... I want to write an application that catches keystrokes and then either redirects them to the in focus window or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.