Craftx398
Craftx398

Reputation: 339

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

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

Upvotes: 5

Views: 19389

Answers (4)

hase1010
hase1010

Reputation: 65

import pyautogui
pyautogui.hold('win')
pyautogui.press('l')

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

Upvotes: 0

Akshay Chordia
Akshay Chordia

Reputation: 61

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

This is working in my laptop.

Upvotes: 5

Zanark
Zanark

Reputation: 63

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')

Upvotes: 6

Felix
Felix

Reputation: 6359

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()

Upvotes: 8

Related Questions