Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux still prints screen when hitting "print" #29

Closed
MrEliptik opened this issue Oct 8, 2019 · 4 comments
Closed

Linux still prints screen when hitting "print" #29

MrEliptik opened this issue Oct 8, 2019 · 4 comments

Comments

@MrEliptik
Copy link

Hi, I'm trying to use pyxhook to catch users hitting the "print screen" key. I successfully catch the event, but linux still run the default behavior, which is making a screenshot. With pyhook on Windows, the behavior is different, as the default "print screen" action is not happening.

One solution could be to remove the mapping between "print screen" and screenshot under linux, but I'd like to avoid that, making life easier to support different distros and OS.

Is there a way to "consume" the event after it was caught by my script ?

@MrEliptik
Copy link
Author

I've tested again on windows with Pyhook and the normal behavior still happens... It's just that I couldn't see it. Do you have an idea of how to consume the event so that it's not grabbed by other apps?

@calumah
Copy link

calumah commented Oct 9, 2019

I don't think that's possible with pyxhook right now. You can see #15 it's the same problem. Otherwise you can change the configuration of keyboard shortcuts with python on linux but this depends on the desktop environment and is not recommended. Users are free to change their keyboard shortcuts as they wish, or else you must use a dead key for both systems.

@MrEliptik
Copy link
Author

@calumah Indeed it seems impossible with pyxhook right now. I've been digging a bit more into xlib and there might be something with XGrabKey(<Display*>, AnyKey, AnyModifier, <window>, false, GrabModeAsync, GrabModeAsync); see this link. I'll try to investigate tonight and post updates for futur reference.

@MrEliptik
Copy link
Author

I've finally managed to do what I want! For anyone curious, here's how to completely grab the 'print' key, without event propagation:

from Xlib.display import Display
from Xlib import X, XK

def handle_event(aEvent):
    if aEvent.type == X.KeyPress:
        print(aEvent)

# current display
root = Display().screen().root

# To check the keycode of your key
'''
keysym = XK.string_to_keysym('Print')
print(disp.keysym_to_keycode(keysym))
''''
# 107 is the keycode for 'print'
root.grab_key(107, X.Mod2Mask, 0, X.GrabModeAsync, X.GrabModeAsync)

# listen for events
while 1:
        event = root.display.next_event()
        handle_event(event)

This will print the event details. Here is an output example:

<class 'Xlib.protocol.event.KeyPress'>(type = 2, detail = 107, sequence_number = 13, time = 11384954, root = <<class 'Xlib.display.Window'> 0x000001fe>, window = <<class 'Xlib.display.Window'> 0x000001fe>, child = <<class 'Xlib.display.Window'> 0x004001a4>, root_x = 2182, root_y = 2612, event_x = 2182, event_y = 2612, state = 16, same_screen = 1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants