在本文中,我们将给您介绍关于在python中检测按键?的详细内容,并且为您解答python怎么检测按键的相关问题,此外,我们还将为您提供关于java–在控制台中检测按键、在Python中使用win32
在本文中,我们将给您介绍关于在python中检测按键?的详细内容,并且为您解答python怎么检测按键的相关问题,此外,我们还将为您提供关于java – 在控制台中检测按键、在Python中使用win32api检测按键、在Python中扫描按键、在Python中检查唯一输出的知识。
本文目录一览:在python中检测按键?(python怎么检测按键)
我正在用python开发一个秒表类型的程序,我想知道如何检测是否按下了一个键(例如p表示暂停,s表示停止),而我不希望它像raw_input这样等待用户输入,然后继续执行。有人知道如何在while循环中执行此操作吗?
另外,我想做这个跨平台的,但是如果那不可能,那么我的主要开发目标是linux
答案1
小编典典对于那些在窗户上努力寻找可行答案的人,我的是:pynput
from pynput.keyboard import Key, Listenerdef 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 releasedwith Listener( on_press=on_press, on_release=on_release) as listener: listener.join()
上面的功能将打印你按下的任何键,并在你释放“ esc”键时开始执行操作。键盘文档在这里用于更多变化的用法。
马库斯·冯·布罗迪(Markus von Broady)
强调了一个潜在的问题,即:这个答案并不需要你在当前窗口中激活此脚本,Windows的解决方案是:
from win32gui import GetWindowText, GetForegroundWindowcurrent_window = (GetWindowText(GetForegroundWindow()))desired_window_name = "Stopwatch" #Whatever the name of your window should be#Infinite loops are dangerous.while True: #Don''t rely on this line of code too much and make sure to adapt this to your project. if current_window == desired_window_name: with Listener( on_press=on_press, on_release=on_release) as listener: listener.join()
java – 在控制台中检测按键
解决方法
import java.util.Scanner; public class ScannerTest { public static void main(String[] args) { Scanner keyboard = new Scanner(system.in); boolean exit = false; while (!exit) { System.out.println("Enter command (quit to exit):"); String input = keyboard.nextLine(); if(input != null) { System.out.println("Your input is : " + input); if ("quit".equals(input)) { System.out.println("Exit programm"); exit = true; } else if ("x".equals(input)) { //Do something } } } keyboard.close(); } }
只需运行ScannerTest并输入任何文本,然后按
在Python中使用win32api检测按键
我正在尝试使用win32api通过特定的按键来打破Python中的循环。怎么会这样呢?
在以下代码中, win32api.KeyPress(’H’) 的实际版本是什么?
修订:
import win32api
while True :
cp = win32api.GetCursorPos()
print cp
if win32api.KeyPress('H') == True :
break
我希望能够通过按 h 键来中断循环。
编辑:
我正在尝试制作一个反复报告鼠标位置的程序,并且需要一种退出该程序的机制。
请参阅修订的代码。
在Python中扫描按键
我已经暂停了一个脚本,让我们说3500秒使用时间模块为ex time.sleep(3500)。
现在,我的目标是扫描按键,而脚本睡觉,我的意思是在这一行。
它就像我想重新启动脚本,如果按下“按键Ctrl + R” 。
对于前..考虑
Linux脚本意外的文件结尾错误
使用tac和sed反向文件
如何在应用程序重置之后重置鼠标指针?
脚本下载文件并根据Windows Vista计算机上的date重命名?
使用Windows批处理命令附加到文件时,如何在文件中的下一个单词之后立即追加?
#!/usr/bin/python import time print "Hello.. again" while True: time.sleep(3500)
现在代码在最后一行,如果我按Ctrl + R,我想重新打印“你好..再次”行。
基地名称与空格在bash脚本?
编写一个可以从命令行input的D脚本
ISE的CMD /批处理脚本?
Python或PHP的小型Windowspipe理脚本?
dynamicBash脚本(即顶部)
我知道这不能完全回答你的问题,但你可以做到以下几点:
把程序逻辑代码放在一个函数中,比如说perform_actions 。 程序启动时调用它。
代码运行后,开始监听中断 。
也就是说,用户必须按ctrl + c而不是ctrl + r 。
接到中断后,等待半秒钟; 如果再次按下ctrl + c ,则退出。
否则,重新启动代码。
因此,一个中断的行为,你想CTRL + R的行为。 两个快速中断退出程序。
import time def perform_actions(): print("Hello.. again") try: while True: perform_actions() try: while True: time.sleep(3600) except KeyboardInterrupt: time.sleep(0.5) except KeyboardInterrupt: pass
使用信号(在这种情况下为SIGINT )的一个很好的副作用是您还可以通过其他方式重新启动脚本,例如通过运行kill -int <pid> 。
你可能想要使用Tkinter {需要X :(}
#!/usr/bin/env python from Tkinter import * # needs python-tk root = Tk() def hello(*ignore): print 'Hello World' root.bind('<Control-r>',hello) root.mainloop() # starts an X widget
如果按下ctrl+r此脚本将打印Hello World到控制台
另请参阅Tkinter键盘绑定 。 使用GTK的另一个解决方案可以在这里找到
在for循环睡眠3500次1秒检查是否每次按下一个键
# sleep for 3500 seconds unless ctrl+r is pressed for i in range(3500): time.sleep(1) # check if ctrl+r is pressed # if pressed -> do something # otherwise go back to sleep
在Python中检查唯一输出
问题是这样的:“你有数字123456789,按顺序.在每个数字之间,你必须插入任何内容,加号或乘法符号,以便结果表达式等于2002.编写一个打印所有的程序解决方案.(有两个.)“
import random def try1(param): global solved opers = [''+'',''*'',''''] hotpotato = (''%s''.join(param) % (random.choice(opers),random.choice(opers),) ) if eval(hotpotato) == 2002: solved += 1 print "Solution:",hotpotato,"= 2002 :-)" else: pass solved = 0 while solved == 0: try1(''123456789'')
此代码打印它遇到的第一个解决方案并停止.任何人都可以告诉我如何让它在停止前打印两种解决方案吗?
解决方法
solutions = set([])
每次找到解决方案时,请更新集:
solutions.append(solution)
集合很好,因为不存储重复项:
>>> len(set([1,1,1])) 1
所以只需循环直到集合的大小大于1:
while len(solved) < 2: try1(''123456789'')
此外,您可以缩短此代码:
hotpotato = (''%s''.join(param) % (random.choice(opers),) )
对此:
hotpotato = (''%s''.join(param) % (random.choice(opers) for i in range(8))))
今天的关于在python中检测按键?和python怎么检测按键的分享已经结束,谢谢您的关注,如果想了解更多关于java – 在控制台中检测按键、在Python中使用win32api检测按键、在Python中扫描按键、在Python中检查唯一输出的相关知识,请在本站进行查询。
本文标签: