Last active
October 14, 2021 06:07
-
-
Save navono/620adcc59dd7852e2384beab1ca57e8f to your computer and use it in GitHub Desktop.
检测鼠标点击
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # This is a sample Python script. | |
| # Press Shift+F10 to execute it or replace it with your code. | |
| # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. | |
| from pynput import mouse | |
| from psutil import * | |
| import time | |
| from datetime import datetime | |
| class mouseTimer(object): | |
| def __init__(self): | |
| self.start_time = 0 | |
| self.time_list = [] | |
| self.left_pressed = False | |
| def on_click(self, x, y, button, pressed): | |
| if str(button) == "Button.left" and pressed: | |
| if not self.left_pressed: | |
| self.start_time = time.time() | |
| self.left_pressed = True | |
| now = datetime.now() | |
| current_time = now.strftime("%H:%M:%S") | |
| print('%s 开始记录时间。CPU: %s, 内存使用率: %s' % (current_time, cpu_percent(), virtual_memory()[2])) | |
| else: | |
| self.left_pressed = False | |
| end_time = time.time() | |
| now = datetime.now() | |
| current_time = now.strftime("%H:%M:%S") | |
| self.time_list.append((end_time - self.start_time) * 1000) | |
| print('%s 距离上一次左键单机: %f。CPU: %s, 内存使用率: %s' % (current_time, ((end_time - self.start_time) * 1000), cpu_percent(), virtual_memory()[2]) + 'ms \n') | |
| # if str(button) == "Button.left" and not pressed: | |
| # self.left_pressed = False | |
| # end_time = time.time() | |
| # self.time_list.append((end_time - self.start_time) * 1000) | |
| # print('run time: %f' % ((end_time - self.start_time) * 1000) + 'ms') | |
| if str(button) == "Button.right" and pressed: | |
| return False | |
| def start(self): | |
| with mouse.Listener(on_click=self.on_click) as listener: | |
| listener.join(); | |
| listener = mouse.Listener( | |
| on_click=self.on_click, | |
| suppress=False | |
| ) | |
| listener.start() | |
| def print_hi(name): | |
| # Use a breakpoint in the code line below to debug your script. | |
| print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint. | |
| # Press the green button in the gutter to run the script. | |
| if __name__ == '__main__': | |
| # print_hi('PyCharm') | |
| mouse_timer = mouseTimer() | |
| mouse_timer.start() | |
| filename = 'path' | |
| with open(filename, 'a') as file_object: | |
| for i in mouse_timer.time_list: | |
| file_object.write(str(i)+'\n') | |
| # See PyCharm help at https://www.jetbrains.com/help/pycharm/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment