I am making a program in which I want to use Windows + D combination to navigate to desktop, but I am unable to find a windows key in Key.<Keyname>
to do that. How can I do that? If not, is there any other way i should do this instead?
Asked
Active
Viewed 3,165 times
3

martineau
- 119,623
- 25
- 170
- 301

Akshay Singh
- 31
- 1
- 3
-
Which OS do you want Windows + D to work on? I'm aware of that working on Linux Ubuntu for instance. Do other OSs support this key sequence to show the desktop too? – Gabriel Staples Jun 04 '23 at 05:11
-
I'm still trying to figure this out. I asked a new question: [pynput library not working as expected in Python to press Windows + D key](https://stackoverflow.com/q/76399361/4561887) – Gabriel Staples Jun 04 '23 at 07:34
4 Answers
1
import keyboard
keyboard.press_and_release('windows+d')
To install:
pip install keyboard
I dont know how to use pynput and use the keyboard library, sorry

user19564834
- 11
- 2
-
`raise ImportError('You must be root to use this library on linux.')`. That stinks you have to be root to use this library. And then when I try to import it as root, I get `ModuleNotFoundError: No module named 'keyboard'`. So...no luck so far. – Gabriel Staples Jun 04 '23 at 07:45
-
And if I do `sudo pip install keyboard` instead of just `pip install keyboard`, to try to give root access to this library, then run my file with `sudo python3 my_test_file.py`, it still doesn't work. Now I get `IndexError: tuple index out of range` on the `keyboard.press_and_release('windows+d')` line. So, still no luck. – Gabriel Staples Jun 04 '23 at 07:50
0
the Keyname for the Windows Button is cmd_l.
if you want to press it just use it as: variable.press(Key.cmd_l)

Antonio
- 3
- 2
0
From my answer here: pynput library not working as expected in Python to press Windows + D key:
This presses Windows (Super) + D:
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)
Due to a bug in pynput
, this won't work in Linux though (ex: Ubuntu 22.04). See my answer in the link above for details, and instructions on using ydotool
instead.

Gabriel Staples
- 36,492
- 15
- 194
- 265