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
= 0A 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
= 0The 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
= 0The 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
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:
So, for people on Ubuntu, if you want to automate a tool to press Windows + D, use
ydotool
instead:xdotool
in Wayland), andIf 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.