Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 输入一个代码以注册按下的第一个100键和长度_Python_Python 3.x_Pynput - Fatal编程技术网

Python 输入一个代码以注册按下的第一个100键和长度

Python 输入一个代码以注册按下的第一个100键和长度,python,python-3.x,pynput,Python,Python 3.x,Pynput,需要你的帮助,它只适用于第一个。。。 这个想法是将前100次按键的时间和长度记录下来 from pynput import keyboard import time, os tinit = time.time() Resultatfichier=open('Keyregister','a') x = 1 def callb(key): #what to do on key-release ti1 = str(time.time() - t)[0:8] #converting flo

需要你的帮助,它只适用于第一个。。。 这个想法是将前100次按键的时间和长度记录下来

from pynput import keyboard 
import time, os

tinit = time.time()
Resultatfichier=open('Keyregister','a')
x = 1

def callb(key): #what to do on key-release
    ti1 = str(time.time() - t)[0:8] #converting float to str, slicing the float
    ti2 = str(time.time() - tinit)[0:8] #converting float to str, slicing the float
    Resultatfichier.write("At "+ti2+" The key " + str(key) + " is pressed for "+ ti1 + " seconds\n") 
    x = x + 1
    return False #stop detecting more key-releases
def callb1(key): #what to do on key-press
    return False #stop detecting more key-presses

while x <= 100:  
    with keyboard.Listener(on_press = callb1) as listener1: #setting code for listening key-press
        listener1.join()

        t = time.time() #reading time in sec

    with keyboard.Listener(on_release = callb) as listener: #setting code for listening key-release
        listener.join()

Resultatfichier.close()
从pynput导入键盘
导入时间,os
tinit=时间。时间()
resultafichier=open('keyrister','a')
x=1
def callb(钥匙):#钥匙释放时要做什么
ti1=str(time.time()-t)[0:8]#将浮点转换为str,分割浮点
ti2=str(time.time()-tinit)[0:8]#将浮点转换为str,分割浮点
Resultatfichier.write(“在“+ti2+”处,按下“+str(键)+”键的时间为“+ti1+”秒\n”)
x=x+1
返回False#停止检测更多密钥释放
def callb1(按键):#按键时要做什么
返回False#停止检测更多按键

虽然x我测试了您的代码,但如果您在callb函数中声明了
global x
,它似乎工作正常:

从pynput导入键盘
导入时间,os
tinit=时间。时间()
resultafichier=open('keyrister','a')
x=1
def callb(钥匙):#钥匙释放时要做什么
全局x#如果不是,则由于变量的范围,在创建之前分配“x”
ti1=str(time.time()-t)[0:8]#将浮点转换为str,分割浮点
ti2=str(time.time()-tinit)[0:8]#将浮点转换为str,分割浮点
Resultatfichier.write(“在“+ti2+”处,按下“+str(键)+”键的时间为“+ti1+”秒\n”)
x=x+1
返回False#停止检测更多密钥释放
def callb1(按键):#按键时要做什么
返回False#停止检测更多按键

而x长度应如何工作?ti1是长度(按键和释放按键之间的时间)/ti2是程序开始和按键之间的时间