Giter VIP home page Giter VIP logo

Comments (8)

moses-palmer avatar moses-palmer commented on July 20, 2024 3

FYI, this functionality has been added to the master branch.

from pynput.

moses-palmer avatar moses-palmer commented on July 20, 2024

Hi,

I think this could be solved for OSX by modify the flags passed to CGEventTapCreate. They can be found here.

If you pass 0 instead of Quartz.kCGEventTapOptionListenOnly as the third argument, you should be able to intercept keys. There is currently no way to do this through the library, but I just made a commit where the creation of the event tap is done by a distinct method; if you override pynpyt._util.darwin.ListenerMixin._create_event_tap to set the flags and pynpyt._util.darwin.ListenerMixin._handle to modify the event, you should be able to create what you want.

from pynput.

adiron avatar adiron commented on July 20, 2024

I'll try to make this toggleable and open a PR if I succeed.

from pynput.

moses-palmer avatar moses-palmer commented on July 20, 2024

Great!

This repository does not have a CONTRIBUTING.md, but please try to follow these simple guidelines:

  • Use pylint. Run python setup.py lint before maing any modifications, and make sure that the pylint score does not decrease. I personally use atom as editor, and it has a pylint plugin; I am sure your preferred editor has one as well.
  • Please keep in mind that pynput has darwin, win32 and xorg backends. Ensure that you do not introduce breaking changes.

A possible way to implement what you want is as follows:

  • Introduce a new flag parameter to the Listener constructor---intercept or similar---to tell the backend that interception is wanted.
  • If intercepting, try to modify the propagated event with the return value of the handler function as called here, here and other locations in the same method. I suggest you use a pynput.KeyCode as return value, and perhaps ignore interceptions if the handler returns None.
  • For now, raise NotImplementedError if intercept is set for backends other than darwin.

The tricky part will be to convert the KeyCode to what the CGEvent contains; the inverse of pynput.keyboard._darwin.Listener._event_to_key.

Thank you for your effort!

from pynput.

pssolanki111 avatar pssolanki111 commented on July 20, 2024

Hey @moses-palmer

Is this functionality available across platforms ( only the major 3 )?

I could not find anything with a keyword search for intercept in the docs.

What I'm looking to do is capturing all input from keyboard, and translating the inputs to something of my own and type it out later on.

Only thing which needs figuring out is intercepting the keyboard input so that it doesn't get typed out into any open windows having focus.

Appreciate the amazing work on pynput, btw.

Thanks :)

EDIT

nvm. I see you named the arg as suppress.
Thanks for the amazing work again.

from pynput.

dumblob avatar dumblob commented on July 20, 2024

@pssolanki111 do you have your code public somewhere? I would like to take a look before diving in the depths of the key mappings hell 😉.

from pynput.

pssolanki111 avatar pssolanki111 commented on July 20, 2024

hey @dumblob I actually dropped working on that project due to a change in requirements. I only did some tests around it and I remember I came across some roadblocks. Below is the piece of code I wrote for tests, which as I mentioned, couldn't continue writing more to.

The use case for the below tests was a re-mapper program which would intercept keys in real time and type in trans-litted variants of letters based on a pre defined mapping.

Also I think you need darwin_intercept function only for OSX. For other OS, you should be able to do fine without it (IIRC)

Also If i remember well, the issue I faced was with re-mapped key press. It appeared to me that the suppressor was suppressing the inputs I was trying to enter programmatically. And the requirements changed before I figured this out.

from pynput.keyboard import Listener, Key, KeyCode, Controller
import Quartz

PATH = '/home/pssolanki/mappings.json'

controller = Controller()

def darwin_intercept(event_type, event):
    length, chars = Quartz.CGEventKeyboardGetUnicodeString(
        event, 100, None, None)
    if length > 0 and chars == 'x':
        # Suppress x
        return None
    elif length > 0 and chars == 'a':
        # Transform a to b
        Quartz.CGEventKeyboardSetUnicodeString(event, 1, 'b')
    else:
        return event

def key_pressed(key):
    if not isinstance(key, KeyCode):
        print('l')
        controller.press(key)
    print(f'pressed: {key}')

def key_released(key):
    print(f'released: {key}')

with Listener(on_press=key_pressed, on_release=key_released, suppress=True) as listener:
    listener.join()

from pynput.

dumblob avatar dumblob commented on July 20, 2024

Ok, thanks anyway @pssolanki111 !

from pynput.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.