简体   繁体   中英

keyboard.is_pressed() recognizes combination of two keys wrongly in python

This example code:

while 1:
    if keyboard.is_pressed('ctrl+alt'):
        print('to') 

But when I press alt+ctrl it prints to . Tried using:

while 1:

    if keyboard.is_pressed('ctrl+alt') and not keyboard.is_pressed('alt+ctrl') :
        print('to') 

But doesn't work. I want it to print to only when I press ctrl+alt and not alt+ctrl or any other key .How to do this. I would guess editing source code (not too familiar with this but if it works it's ok) Or more specifically I want it to recognize it only when I hold down ctrl then press alt while still holding down ctrl .

Operating System: windows 7
Python Version: 3.8.8
Keyboard: Logitech k200
IDE: pycharm community edition
Module: keyboard module latest version
Module Link: https://github.com/boppreh/keyboard
Pip Version: Latest

Edit Fixed:

import keyboard

def on_alt(event):
    if keyboard.is_pressed('ctrl'):
        print('Ctrl + Alt pressed')

keyboard.on_press_key('alt', on_alt)
time.sleep(1e6)

while not just make it two if statements

if keyboard.is_pressed("ctrl") then
    if keyboard.is_pressed("alt") then
        print("done")
    end
end

(Not written in python)

Fixed. The following code workes in the latest version

import keyboard

def on_alt(event):
    if keyboard.is_pressed('ctrl'):
        print('Ctrl + Alt pressed')

keyboard.on_press_key('alt', on_alt)
time.sleep(1e6)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM