[英]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
.但是当我按
alt+ctrl
时,它会打印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.我希望它仅在我按
ctrl+alt
而不是alt+ctrl
或任何其他键时to
。如何执行此操作。 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
.我猜想编辑源代码(对此不太熟悉,但如果它可以工作就可以了)或者更具体地说,我希望它仅在我按住
ctrl
时识别它,然后在按住ctrl
的同时按alt
。
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 语句
if keyboard.is_pressed("ctrl") then
if keyboard.is_pressed("alt") then
print("done")
end
end
(Not written in python) (不是用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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.