Catching keystrokes under Windows

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Olli Piepponen

    #1

    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.
  • Peter Hansen

    #2
    Re: Catching keystrokes under Windows

    Olli Piepponen wrote:[color=blue]
    >
    > 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.[/color]

    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

    Comment

    • Michael Geary

      #3
      Re: Catching keystrokes under Windows

      Olli Piepponen:[color=blue]
      > 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.[/color]

      To do this for all Windows applications, you need to use a systemwide
      message hook, which is set with the Windows function SetWindowsHookE x 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" <parente@cs.unc .edu> wrote in message
      news:85b6a599.0 308250326.15d20 640@posting.goo gle.com...[color=blue]
      > 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
      >
      > parente@cs.unc. edu (Peter Parente) wrote in message[/color]
      news:<85b6a599. 0308230628.49ba 3c74@posting.go ogle.com>...[color=blue][color=green]
      > > 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 <hokiegal99@hot mail.com> wrote in message[/color][/color]
      news:<bi66lq$sk j$1@solaris.cc. vt.edu>...[color=blue][color=green][color=darkred]
      > > > 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[/color][/color][/color]
      day...[color=blue][color=green][color=darkred]
      > > > 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[/color][/color][/color]


      Comment

      Working...