5

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?

paul23
  • 8,799
  • 12
  • 66
  • 149
  • The `pynput.keyboard.Listener` is for _listening_ to keyboard events, not changing them, so I don't think doing what you want is within the "realm". – martineau Jan 28 '19 at 02:46
  • @martineau is there a library that does intercept the inputs? – paul23 Jan 28 '19 at 03:19
  • There's nothing generic that I know of, but you might be able to use the `ctypes` module and do it in a platform-specific way via a particular OS's API. – martineau Jan 28 '19 at 03:25

2 Answers2

3

set suppress=True like this

def main():
    with keyboard.Listener(on_press=on_press, suppress=True) as listener:
        listener.join()
Jiawei Fei
  • 47
  • 2
1

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.

sebse
  • 19
  • 1