闲的无聊,记录下上下班时间。
背景,不怎么关电脑,每天第一次敲键盘即为上班时间,每天按下锁屏快捷键(Windows键+L)即为下班。
python2.7
# -*- coding: utf-8 -*- #
# by oldj http://oldj.net/ #
import pythoncom
import pyHook
import os
import datetime
import time
today, on_duty_file, off_duty_file = datetime.datetime.now().strftime('%Y-%m-%d'), '', ''
key_dict = {'first':{'keyid':76,'time':0},'second':{'keyid':91,'time':0}}
def checkDir(today):
global on_duty_file
global off_duty_file
print on_duty_file
on_duty_file = "C:/Users/duty_time/"+today+"/on_dufy.txt"
off_duty_file = "C:/Users/duty_time/"+today+"/off_dufy.txt"
return True
def mkdir():
global today
today = datetime.datetime.now().strftime('%Y-%m-%d')
path = "C:/Users/duty_time/" + today
pan = os.path.exists(path)
print path
if not pan:
os.mkdir(path)
checkDir(today)
return True
def onKeyboardEvent(event):
mkdir() #创建当天文件夹
# 监听键盘事件
keyid=event.KeyID
global on_duty_file
global off_duty_file
global key_dict
if (not os.path.exists(on_duty_file)):
with open(on_duty_file.decode('utf-8'), 'ab+') as f:
f.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
if keyid == key_dict['first']['keyid']:
key_dict['first']['time'] = time.time()
if keyid == key_dict['second']['keyid']:
key_dict['second']['time'] = time.time()
if key_dict['first']['time'] > 0 and key_dict['second']['time'] >0 and abs(key_dict['first']['time']-key_dict['second']['time']) < 10000:
with open(off_duty_file.decode('utf-8'), 'ab+') as f:
f.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+'-------')
key_dict['first']['time']=0
key_dict['second']['time']=0
return True
def main():
# 创建一个“钩子”管理对象
hm = pyHook.HookManager()
# 监听所有键盘事件
hm.KeyDown = onKeyboardEvent
# 设置键盘“钩子”
hm.HookKeyboard()
# 进入循环,如不手动关闭,程序将一直处于监听状态f
pythoncom.PumpMessages()
if __name__ == "__main__":
main()
python 3.5
# -*- coding: utf-8 -*- #
# by oldj http://oldj.net/ #
import pythoncom
import pyHook
import os
import datetime
import time
today, on_duty_file, off_duty_file = datetime.datetime.now().strftime('%Y-%m-%d'), '', ''
key_dict = {'first':{'keyid':76,'time':0},'second':{'keyid':91,'time':0}}
def checkDir(today):
global on_duty_file
global off_duty_file
on_duty_file = "F:/duty_time/"+today+"/on_dufy.txt"
off_duty_file = "F:/duty_time/"+today+"/off_dufy.txt"
return True
def mkdir():
global today
today = datetime.datetime.now().strftime('%Y-%m-%d')
path = "F:/duty_time/" + today
pan = os.path.exists(path)
print(path)
if not pan:
os.mkdir(path)
checkDir(today)
return True
def onKeyboardEvent(event):
mkdir() #创建当天文件夹
# 监听键盘事件
keyid=event.KeyID
global on_duty_file
global off_duty_file
global key_dict
if ( os.path.exists(on_duty_file)):
print(on_duty_file)
with open(on_duty_file, 'a+') as f:
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
f.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
if keyid == key_dict['first']['keyid']:
key_dict['first']['time'] = time.time()
if keyid == key_dict['second']['keyid']:
key_dict['second']['time'] = time.time()
if key_dict['first']['time'] > 0 and key_dict['second']['time'] >0 and abs(key_dict['first']['time']-key_dict['second']['time']) < 10000:
with open(off_duty_file, 'a+') as f:
f.write(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+'-------')
key_dict['first']['time']=0
key_dict['second']['time']=0
return True
def main():
# 创建一个“钩子”管理对象
hm = pyHook.HookManager()
# 监听所有键盘事件
hm.KeyDown = onKeyboardEvent
# 设置键盘“钩子”
hm.HookKeyboard()
# 进入循环,如不手动关闭,程序将一直处于监听状态f
pythoncom.PumpMessages()
if __name__ == "__main__":
main()
PS : 编辑器安装pyhook报错的话,自己手动安装
资源 https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyhook
执行 pip install 安装