[英]Python 2.7 - How to check if SHIFT-Key or CTRL+Key is pressed?
I wrote a small routine in python 2.7, which waits for some time (say, 10 minutes) but immediately exits if a key is pressed by the the user. 我在python 2.7中编写了一个小例程,该例程等待一段时间(例如10分钟),但是如果用户按下某个键,则会立即退出。
I'm on Windows (7, 64-bit), so I tried using the msvcrt library: 我在Windows(7位,64位)上,因此我尝试使用msvcrt库:
import sys
import msvcrt
from time import sleep
def sleep_sec(secstosleep, allowskip = True):
waitinterval_ms = 0.050
nwaits = int(secstosleep * 1000 / waitinterval)
sys.stdout.write("\n Sleeping for %s seconds. You can press CTRL-F12 to skip.\n\n" % secstosleep)
for sl in range(0, nwaits + 1):
sleep(waitinterval_ms)
if allowskip:
# Check if User has pressed CTRL-F12 to stop waiting
if = msvcrt.kbhit():
kp = msvcrt.getch()
if kp == '\xe0':
print "\a" # Beep once
sys.stdout.write("\nSleep interrupted by User\n")
break
In effect it works quite well, except for the fact that it will break if the user hits either F12, F11 or Cursor Up key: I found out that if I press F12, getch() will return the '\\xe0', but it seems the same code is also returned by the other mentioned keys; 实际上,它工作得很好,除了以下事实:如果用户按下F12,F11或Cursor Up键,它将中断:我发现如果按下F12,getch()将返回'\\ xe0',但是它似乎其他提到的键也返回了相同的代码; no way to trap CTRL, SHIFT, ALT, etc. 无法捕获CTRL,SHIFT,ALT等
I would like to force the user pressing CTRL-F12, instead, to avoid quitting the wait by inadvertently hitting one of those keys. 我想强迫用户按下CTRL-F12,以避免由于无意中按下这些键之一而退出等待。
Am I doing something wrong? 难道我做错了什么? Is there a way to intercept CTRL, ALT or SHIFT being pressed along with another key? 有没有办法拦截与其他键一起按下的CTRL,ALT或SHIFT?
Thanks in advance, Max - Italy 在此先感谢Max-意大利
From the msvcrt
manual for msvcrt.getch()
: 从msvcrt
手册中的msvcrt.getch()
:
[..] If the pressed key was a special function key, this will return '\\000' or '\\xe0'; [..]如果按下的键是特殊功能键,则将返回“ \\ 000”或“ \\ xe0”; the next call will return the keycode. 下一次调用将返回键码。
Just call getch()
again to get the actual keycode 只需再次调用getch()
即可获取实际的密钥代码
I will suggest you to use keyboard
module. 我建议您使用keyboard
模块。 It has a lot of features and is pretty simple to use. 它具有很多功能,并且非常易于使用。 You can also use it to detect keypresses. 您也可以使用它来检测按键。 Examples: 例子:
If you want to detect key a
: 如果要检测密钥a
:
import keyboard as kb
while True:
if kb.is_pressed("a"):break
You can also use it to detect combinations. 您也可以使用它来检测组合。 For example , if you want to check if a
and b
are pressed at the same time: 例如,如果要检查是否同时按下a
和b
:
import keyboard as kb
while True:
if kb.is_pressed("a+b"):break
So , you can use to to detect if ctrl
and f12
are pressed: 因此,您可以用来检测是否按下了ctrl
和f12
:
import keyboard as kb
while True:
if kb.is_pressed("ctrl+f12"):break
A sample according to your posted code: 根据您发布的代码的示例:
import sys
import keyboard as kb
import time
def sleep_sec(secstosleep,allowskip=True):
t = time.time() #getting time in sec
sys.stdout.write("\n Sleeping for %s seconds. You can press CTRL-F12 to skip.\n\n"% secstosleep)
if allowskip:
while time.time() - t <= secstosleep: #while current time(in sec) - t is less than secstosleep
if kb.is_pressed("ctrl+f11"): #if ctrl and f12 are pressed
print "\a"
sys.stdout.write("\nSleep interrupted by User\n")
quit(0)
sleep_sec(10*60) # 10*60 == 10 min
As suggested by Daniel, I've modified my code like this: 正如丹尼尔(Daniel)建议的那样,我已经修改了我的代码,如下所示:
import sys
import msvcrt
from time import sleep
def sleep_sec(secstosleep, allowskip = True):
waitinterval_ms = 0.050
nwaits = int(secstosleep * 1000 / waitinterval)
sys.stdout.write("\n Sleeping for %s seconds. You can press CTRL-F12 to skip.\n\n" % secstosleep)
for sl in range(0, nwaits + 1):
sleep(waitinterval_ms)
if allowskip:
# Check if User has pressed CTRL-F12 to stop waiting
if = msvcrt.kbhit():
kp = msvcrt.getch()
if kp == '\xe0':
kp += msvcrt.getch()
if kp == '\xe0\x8a': # \x8a is the keycode for CTRL-F12
print "\a" # Beep once
sys.stdout.write("\nSleep interrupted by User\n")
break
I've also noticed that different keycodes are returned for F12, CTRL-F12 and SHIFT-F12, so keystrokes can be easily recognized. 我还注意到为F12,CTRL-F12和SHIFT-F12返回了不同的键代码,因此可以轻松识别键击。
Thanks again, Daniel! 再次感谢,丹尼尔!
Max. 最高
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.