记录每个按键时在计数定时器中连续记录动力,同时执行keylogger.py。

huangapple go评论71阅读模式
英文:

Record momentum consecutively when each key is pressed in a count timer while executing keylogger.py

问题

我有以下的keylogger.py代码文件,用于检测特定按键的按下并保存到log.txt文件中。我想要在这个Python代码中添加一个计时器,该计时器在代码运行时启动,同时我还想要在文件开始时保存keys被按下的确切时刻,并将此信息保存在log.txt文件或另一个单独的文件中(可能需要使用with openf.write)。我在以下链接中找到了一些计时的想法:https://stackoverflow.com/questions/17211188/how-to-create-a-timer-on-python 和 https://pythonhow.com/measure-execution-time-python-code/

所以我的log.txt文件会是这样的

log.py

RLLR     #此行将已由keylogger.py生成

R: 0.2秒
L: 0.24秒
L: 1.34秒
R: 2.5秒

keylogger.py

from pynput.keyboard import Listener, Key
import time

# 设置日志文件位置
logFile = "/home/diego/log.txt"

# 获取程序启动时间
start_time = time.time()

def writeLog(key):
    translate_keys = {
        Key.right: "R",
        Key.left: "L",
    }
    if key in translate_keys:
        current_time = time.time() - start_time
        with open(logFile, "a") as f:
            f.write(translate_keys[key] + ": {:.2f}s\n".format(current_time))

with Listener(on_press=writeLog) as l:
    l.join()
英文:

I have the following code-file keylogger.py below to detect a specific key being pressed and saving to log.txt file.
I want to add to this python code a timer that starts when the code starts to run, I also want this part of the code to save the exact moment the keys were pressed starting with the start of the file and save this information in the log.txt file or another separate file (would need to use with open and f.write I think). Several timekeeping ideas I found in https://stackoverflow.com/questions/17211188/how-to-create-a-timer-on-python and https://pythonhow.com/measure-execution-time-python-code/.

So my log.txt would be something like

log.py

RLLR     #this line would already be produced by keylogger.py

R: 0.2s
L:0.24s
L:1.34s
R:2.5s

keylogger.py

from pynput.keyboard import Listener, Key

#set log file location
logFile = "/home/diego/log.txt"



def writeLog(key):
    translate_keys = {
        Key.right: "R",
        Key.left: "L",
    }
    if key in translate_keys:
        with open(logFile, "a") as f:
            f.write(translate_keys[key])

with Listener(on_press=writeLog) as l:
    l.join()

答案1

得分: 1

使用单独的文件会是我认为最简单的解决方案:

from pynput.keyboard import Listener, Key
import time

# 设置按键记录的日志文件位置
keysLogFile = "/home/diego/keys_log.txt"

# 设置按键时间戳记录的日志文件位置
timestampsLogFile = "/home/diego/ts_log.txt"

# 存储开始时间戳
startTime = time.time()

def writeLog(key):
    translate_keys = {
        Key.right: "R",
        Key.left: "L",
    }
    if key in translate_keys:
        currentTime = time.time() - startTime
        with open(keysLogFile, "a") as f:
            f.write(translate_keys[key])
        with open(timestampsLogFile, "a") as f:
            f.write(translate_keys[key] + ": " + str(currentTime) + "s\n")

with Listener(on_press=writeLog) as l:
    l.join()

这将为您生成两个单独的文件:

keys_log.txt

RLLR

ts_log.txt

R: 0.2s
L: 0.24s
L: 1.34s
R: 2.5s
英文:

Using separate files would be the simplest solution I think:

from pynput.keyboard import Listener, Key
import time

#set log file location for keys pressed
keysLogFile = "/home/diego/keys_log.txt"

#set log file location for keys timestamps
timestampsLogFile = "/home/diego/ts_log.txt"

#store start timestamp
startTime = time.time()


def writeLog(key):
    translate_keys = {
        Key.right: "R",
        Key.left: "L",
    }
    if key in translate_keys:
        currentTime = time.time() - startTime
        with open(keysLogFile, "a") as f:
            f.write(translate_keys[key])
        with open(timestampsLogFile, "a") as f:
            f.write(translate_keys[key] + ": " + str(currentTime) + "s\n")

with Listener(on_press=writeLog) as l:
    l.join()

This would give you two separate files:

keys_log.txt

RLLR

ts_log.txt

R: 0.2s
L: 0.24s
L: 1.34s
R: 2.5s

huangapple
  • 本文由 发表于 2020年1月7日 01:55:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定