How to register multiple key presses at the same time in python with library keyboard

Question:

I want to create a program when I press ctrl and alt at the same time that refreshes the page 100 times. The only thing that doesn’t work is when you press ctrl and alt at the same time. Does anyone know how to fix this. Here is my code

import pyautogui
import keyboard

while True:  
    try:  e
        if keyboard.is_pressed('ctrl,alt'):  
            print('You Pressed h Key!')
            pyautogui.hotkey('f5') 

    except:
        break
Asked By: Robbe Vermandel

||

Answers:

If you would display error – i.e.

except Exception as ex:
    print('Exception:', ex)

then you would see (at least on Linux)

Exception: Impossible to check if multi-step hotkeys are pressed (`a+b` is ok, `a, b` isn't).

And this would show you that you need ctrl+alt instead of ctrl,alt

import keyboard

while True:
    try:
        if keyboard.is_pressed('ctrl+alt'):
            print('You Pressed ctrl+alt')
    except Exception as ex:
        print('Exception:', ex)
        break

EDIT:

Also works for me

if keyboard.is_pressed('ctrl') and keyboard.is_pressed('alt'):

and add_hotkey()

import keyboard

def update():
    print('You Pressed ctrl+alt')

keyboard.add_hotkey('ctrl+alt', update)

keyboard.wait('esc')  # press `Esc` to end program
Answered By: furas
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.