skip to Main Content

I’m trying to do what this question asked (this question has no valid answers with functional code using pynput): Press Windows+D with pynput. But, my attempts are not working as expected.

On Linux Ubuntu, pressing Windows + d will minimize all windows, thereby showing the desktop. Doing it again will bring all the windows back as they were.

Here’s my code:

import time

from pynput.keyboard import Key, Controller

keyboard = Controller()
SUPER_KEY = Key.cmd

keyboard.press(SUPER_KEY)
# time.sleep(1)
keyboard.press('d')
keyboard.release('d')
keyboard.release(SUPER_KEY)

When I run it, I expect the Windows + d shortcut to be pressed, hiding all windows. Instead, only the Windows key is pressed, which brings up the program launcher search tool, and then a single d is left printed in my terminal, like this:

$ ./pynput_press_Windows+D_to_show_the_desktop.py 
$ d

How do I get this to work?

The reference documentation says (https://pynput.readthedocs.io/en/latest/keyboard.html) that Key.cmd is the "Super" or "Windows" key. I’ve also tried with Key.cmd_l and Key.cmd_r.

cmd = 0

A generic command button. On PC platforms, this corresponds to the Super key or Windows key, and on Mac it corresponds to the Command key. This may be a modifier.

cmd_l = 0

The left command button. On PC platforms, this corresponds to the Super key or Windows key, and on Mac it corresponds to the Command key. This may be a modifier.

cmd_r = 0

The right command button. On PC platforms, this corresponds to the Super key or Windows key, and on Mac it corresponds to the Command key. This may be a modifier.


Update 4 June 2023: keyboard monitor test program, to ensure Key.cmd + d is correct for my keyboard (it is): modified from https://pynput.readthedocs.io/en/latest/keyboard.html#monitoring-the-keyboard:

from pynput import keyboard

print("Keyboard monitor demo program. Press Esc to exit.")

def on_press(key):
    try:
        print('alphanumeric key {0} pressed'.format(
            key.char))
    except AttributeError:
        print('special key {0} pressed'.format(
            key))

def on_release(key):
    print('{0} released'.format(
        key))
    if key == keyboard.Key.esc:
        # Stop listener
        print("Exiting the program.")
        return False

# Collect events until released
with keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

Sample output when I press Super + D:

$ ./pynput_monitor_keyboard.py 
Keyboard monitor demo program. Press Esc to exit.
Key.enter released
special key Key.cmd pressed
alphanumeric key d pressed
'd' released
Key.cmd released

2

Answers


  1. Chosen as BEST ANSWER

    I just want to tie the loose ends on this question.

    As far as I can tell, my code in the question is fine. @Сергей Кох's answer is fine too. There is just a bug in pynput is all, preventing it from working in Ubuntu.

    I'm on Ubuntu 22.04.2, and even in the X11 window manager (as opposed to Wayland), it still doesn't work.

    Here's my bug report: Bug on Linux: Windows (Super) + D doesn't work

    Here is some valid code that should work. If you have a Mac or Windows, please try it out and leave a comment if this code works or not. Both techniques here should be equally valid:

    import time
    
    from pynput.keyboard import Key, Controller
    
    
    # ========== technique 1 ===========
    # From: https://pynput.readthedocs.io/en/latest/keyboard.html
    print("Trying technique 1")
    
    keyboard = Controller()
    SUPER_KEY = Key.cmd
    with keyboard.pressed(SUPER_KEY):
        keyboard.press('d')
        keyboard.release('d')
    
    
    time.sleep(1.0)
    
    
    # ========== technique 2 ===========
    print("Trying technique 2")
    
    keyboard = Controller()
    SUPER_KEY = Key.cmd
    
    keyboard.press(SUPER_KEY)
    keyboard.press('d')
    keyboard.release('d')
    keyboard.release(SUPER_KEY)
    

    So, for people on Ubuntu, if you want to automate a tool to press Windows + D, use ydotool instead:

    1. I have posted a full demo in my answer here: Ask Ubuntu: How can I write a program to press keys, such as Windows + D, in Wayland? (replace xdotool in Wayland), and
    2. I have written a full article with even more info on my website here: Tutorial: Getting started with ydotool to automate key presses (or mouse movements) in Linux.

  2. If you have a dual language keyboard, a different keyboard "shortcut" may work. On my Russian-English keyboard, pressing the Russian "в" worked, which is "d" in the English layout. I learned this using keyboard monitoring from the documentation by pressing "win + d" on the keyboard.

    from pynput.keyboard import Key, Controller
    
    keyboard = Controller()
    
    with keyboard.pressed(Key.cmd):
        keyboard.press('в')
        keyboard.release('в')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search