paul23
paul23

Reputation: 9455

(pynput) capture keys prevent sending them to other applications

Well on pynput I capture a key (say spacebar) by doing something alike:

from pynput import keyboard
from pynput.keyboard import Key

def on_press(key, ctrl):
    if key == Key.space:
        print('captured')


def main():
    with keyboard.Listener(on_press=on_press) as listener:
        listener.join()

However I notice that this still sends the original key-code to other applications. I wish to "bind" key(combination)s to other keys (or more advanced actions) using python, so this needs to be prevented.

How can this be done? Or is this out of the realm of what python is allowed to do by the OS?

Upvotes: 5

Views: 4230

Answers (2)

sebse
sebse

Reputation: 19

To prevent a specific key to be sent system wide (on Windows) you can use the kwarg "win32_event_filter".

A working example by lukakoczorowski on https://github.com/moses-palmer/pynput/issues/170

Constructin a proper "win32_event_filter" allows you to prevent the propagation of hotkeys too.

Upvotes: 1

Jiawei Fei
Jiawei Fei

Reputation: 47

set suppress=True like this

def main():
    with keyboard.Listener(on_press=on_press, suppress=True) as listener:
        listener.join()

Upvotes: 3

Related Questions