How to trigger a Windows and L key Python-hotkey?

Question:

I am trying to run this code in Python.

import pyautogui
pyautogui.hotkey('win', 'l')

So that when I run it it will trigger switch user in Windows but all it does is press l when I need it to press Win+l

Asked By: Craftx398

||

Answers:

As said in my comment, it is most likely that this key combination doesn’t work because Windows handles it specially.

If you want to simply lock the workstation, you can use this solution which uses ctypes to call Windows’ LockWorkstation-Function:

Lock windows workstation using Python

Python code:

import ctypes
ctypes.windll.user32.LockWorkStation()
Answered By: Felix

I had this issue for Win+V and instead of using the hotkey function I used:

pyautogui.keyDown('winleft')
pyautogui.press('v')
pyautogui.keyUp('winleft')
Answered By: Zanark
import pyautogui
pyautogui.hotkey('winleft', 'l')

This is working in my laptop.

Answered By: Akshay Chordia
import pyautogui
pyautogui.hold('win')
pyautogui.press('l')

This is working on my PC if you want to use only pyautogui and not ctypes

Answered By: hase1010
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.