1

Please mind that I am completely green in Python and I do not know how callbacks work in this language.

I have been trying for some time to solve my problem, but I cannot figure out what to do.

I have the problem with using callbacks in Python. I use the Python keyboard module to listen to keys pressed on a machine.

When I try to use the keyboard.hook() method, it asks me for a callback. I know how callbacks work, but as long as Python is not my main language I can't really figure out what to do with it.

In JavaScript it's as easy as naming a parameter in a function then printing that parameter. Easy as that.

import keyboard

keyboard.hook()

## How to print keys?

In the official documentation, it is written that the hook() method invokes a callback. How do I access this callback and most importantly print keys which are recorded from it? I just need a simple example then I will be able to remember it forever.

Any help really appreciated.

3
  • Use def foo(): # some code and then keyboard.hook(foo) Commented Dec 19, 2018 at 21:34
  • 1
    the included examples do a fine job of outlining this Commented Dec 19, 2018 at 21:37
  • Oh, dang it! I didn't initially thought that the creator included examples. I thought he would add it to the docs, but maybe I didn't dive into it deeper. Anyway, thanks for your response, it helped me a lot. Commented Dec 19, 2018 at 21:47

2 Answers 2

1

You can pass a function just like you would a variable—by passing its name to the hook() method.

Then, per the docs on keyboard.hook(), it calls your callback with a keyboard.KeyboardEvent with three fields:

  • name: an Unicode representation of the character (e.g. "&") or description (e.g. "space"). The name is always lower-case.
  • scan_code: number representing the physical key, e.g. 55.
  • time: timestamp of the time the event occurred, with as much precision as given by the OS.

So, putting it together, you can use it like this:

import keyboard

def my_keyboard_hook(keyboard_event):
    print("Name:", keyboard_event.name)
    print("Scan code:", keyboard_event.scan_code)
    print("Time:", keyboard_event.time)

keyboard.hook(my_keyboard_hook)

# Block forever, so that the program won't automatically finish,
# preventing you from typing and seeing the printed output
keyboard.wait()

And each time a key is pressed, you'll print the details of the keyboard event.

Note that the keyboard.wait() call is not necessary if your program would otherwise continue running—I just wanted to make sure that you didn't run the example code, see it terminate immediately, and then think something went wrong.

Sign up to request clarification or add additional context in comments.

1 Comment

Yup, that's what I wanted. Thanks for your answer to this kind of "stupid" question. I will be sure to remember it!
1

Something like this appears to be what you want from the documentation.

def keyHook(info):
    print(info)

keyboard.hook(keyHook)

1 Comment

Thank you for your time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.