python按键盘上哪个键运行_在python中检测按键?

在python中检测按键?

我正在用python创建一个秒表类型的程序,我想知道如何检测是否按下了一个键(例如p表示暂停,s表示停止),并且我不希望它像raw_input这样等待 用户输入,然后继续执行。 有人知道如何在while循环中执行此操作吗?

另外,我想做这个跨平台的,但是如果那不可能,那么我的主要开发目标是linux

10个解决方案

39 votes

Python有一个具有许多功能的键盘模块。 安装它,也许使用以下命令:

pip3 install keyboard

然后在如下代码中使用它:

import keyboard # using module keyboard

while True: # making a loop

try: # used try so that if user pressed other than the given key error will not be shown

if keyboard.is_pressed('q'): # if key 'q' is pressed

print('You Pressed A Key!')

break # finishing the loop

else:

pass

except:

break # if user pressed a key other than the given key the loop will break

Community answered 2020-01-03T01:51:24Z

21 votes

对于那些在窗户上努力寻找可行答案的人,我的是:pynput

from pynput.keyboard import Key, Listener

def on_press(key):

print('{0} pressed'.format(

key))

def on_release(key):

print('{0} release'.format(

key))

if key == Key.esc:

# Stop listener

return False

# Collect events until released

with Listener(

on_press=on_press,

on_release=on_release) as listener:

listener.join()

上面的功能将打印您所按的任何键,并在您释放“ esc”键时启动操作。 键盘文档在这里用于更多变化的用法。

马库斯·冯·布罗迪(Markus von Broady)强调了一个潜在的问题,即:这个答案并不需要您在当前窗口中激活此脚本,Windows的解决方案是:

from win32gui import GetWindowText, GetForegroundWindow

current_window = (GetWindowText(GetForegroundWindow()))

desired_window_name = "Stopwatch" #Whatever the name of your window should be

if current_window == desired_window_name:

with Listener(

on_press=on_press,

on_release=on_release) as listener:

listener.join()

Mitrek answered 2020-01-03T01:51:54Z

17 votes

正如OP提到raw_input一样-这意味着他想要cli解决方案。Linux:curses是您想要的(Windows PDCurses)。 Curses是cli软件的图形API,您不仅可以检测关键事件,还可以实现更多目标。

该代码将检测按键,直到按下新行。

import curses

import os

def main(win):

win.nodelay(True)

key=""

win.clear()

win.addstr("Detected key:")

while 1:

try:

key = win.getkey()

win.clear()

win.addstr("Detected key:")

win.addstr(str(key))

if key == os.linesep:

break

except Exception as e:

# No input

pass

curses.wrapper(main)

Abc Xyz answered 2020-01-03T01:52:18Z

10 votes

对于Windows,您可以这样使用msvcrt:

import msvcrt

while True:

if msvcrt.kbhit():

key = msvcrt.getch()

print(key) # just to show the result

Benjie answered 2020-01-03T01:52:38Z

6 votes

使用此代码查找按下了哪个键

from pynput import keyboard

def on_press(key):

try:

print('alphanumeric key {0} pressed'.format(

key.char))

except AttributeError:

print('special key {0} pressed'.format(

key))

def on_release(key):

print('{0} released'.format(

key))

if key == keyboard.Key.esc:

# Stop listener

return False

# Collect events until released

with keyboard.Listener(

on_press=on_press,

on_release=on_release) as listener:

listener.join()

Manivannan Murugavel answered 2020-01-03T01:52:58Z

6 votes

使用PyGame拥有一个窗口,然后您可以获取关键事件。

对于字母p:

import pygame, sys

import pygame.locals

pygame.init()

BLACK = (0,0,0)

WIDTH = 1280

HEIGHT = 1024

windowSurface = pygame.display.set_mode((WIDTH, HEIGHT), 0, 32)

windowSurface.fill(BLACK)

while True:

for event in pygame.event.get():

if event.key == pygame.K_p: # replace the 'p' to whatever key you wanted to be pressed

pass #Do what you want to here

if event.type == pygame.locals.QUIT:

pygame.quit()

sys.exit()

A.J. Uppal answered 2020-01-03T01:53:22Z

4 votes

keyboard模块可以完成更多操作。

以下是一些方法:

方法1:

使用功能keyboard:

import keyboard

while True:

if keyboard.read_key() == "p":

print("You pressed p")

break

当按下键p时,这将打破循环。

方法2:

使用功能keyboard:

import keyboard

keyboard.wait("p")

print("You pressed p")

它将等待您按p并按其继续输入代码。

方法3:

使用功能keyboard:

import keyboard

keyboard.on_press_key("p", lambda _:print("You pressed p"))

它需要一个回调函数。 我使用keyboard,因为键盘功能将键盘事件返回到该功能。

一旦执行,当按下键时它将运行该功能。 您可以通过运行以下行来停止所有挂钩:

keyboard.unhook_all()

方法4:

user8167727已经回答了这种方法,但是我不同意他们编写的代码。 它将使用功能keyboard,但以另一种方式:

import keyboard

while True:

if keyboard.is_pressed("p"):

print("You pressed p")

break

按下p会中断循环。

笔记:

keyboard将读取整个操作系统的按键。

keyboard在Linux上需要root用户

Black Thunder answered 2020-01-03T01:54:57Z

1 votes

我建议您使用PyGame并添加一个事件句柄。

[http://www.pygame.org/docs/ref/event.html]

Madison Courto answered 2020-01-03T01:55:21Z

1 votes

所以我根据这篇文章(使用msvcr库和Python 3.7)制作了这款游戏。

以下是游戏的“主要功能”,即检测所按下的按键:

# Requiered libraries - - - -

import msvcrt

# - - - - - - - - - - - - - -

def _secret_key(self):

# Get the key pressed by the user and check if he/she wins.

bk = chr(10) + "-"*25 + chr(10)

while True:

print(bk + "Press any key(s)" + bk)

#asks the user to type any key(s)

kp = str(msvcrt.getch()).replace("b'", "").replace("'", "")

# Store key's value.

if r'\xe0' in kp:

kp += str(msvcrt.getch()).replace("b'", "").replace("'", "")

# Refactor the variable in case of multi press.

if kp == r'\xe0\x8a':

# If user pressed the secret key, the game ends.

# \x8a is CTRL+F12, that's the secret key.

print(bk + "CONGRATULATIONS YOU PRESSED THE SECRET KEYS!\a" + bk)

print("Press any key to exit the game")

msvcrt.getch()

break

else:

print(" You pressed:'", kp + "', that's not the secret key(s)\n")

if self.select_continue() == "n":

if self.secondary_options():

self._main_menu()

break

如果您需要程序的完整源代码,则可以在此处查看或下载该程序:

秘钥游戏(GitHub)

(注意:秘密按键为:Ctrl + F12)

我希望您可以作为一个例子,并为那些来此信息的人提供帮助。

Ferd answered 2020-01-03T01:56:03Z

0 votes

key = cv2.waitKey(1)

这是来自openCV软件包。它无需等待即可检测到按键。

eyllanesc answered 2020-01-03T01:56:23Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值