{"id":36,"date":"2006-09-24T14:32:57","date_gmt":"2006-09-24T18:32:57","guid":{"rendered":"http:\/\/scott.sherrillmix.com\/blog\/uncategorized\/active-window-logger\/"},"modified":"2009-07-08T08:07:23","modified_gmt":"2009-07-08T12:07:23","slug":"active-window-logger","status":"publish","type":"post","link":"http:\/\/scott.sherrillmix.com\/blog\/programmer\/active-window-logger\/","title":{"rendered":"Active Window Logger"},"content":{"rendered":"

I was curious how I spend my time and wanted something simple to log which window was active on my computer. I couldn’t see anything that did this easily on the internet, although there is a (more sophisticated\/complicated) browsing history tracker<\/a>, and I needed to relax for an hour or two so I thought I’d give it a shot in Python and see what came out. After looking at the flashing taskbar icon tutorial<\/a>, how to get the currently active window<\/a>, how to detect if Windows is idle<\/a> and how to get rid of the console window<\/a>, I could finally start programming. I really don’t know how people programmed before Google.<\/p>\r\n\r\n

Python really makes things easy. It took less than 90 lines of code and just a couple hours to write this. So here is the code for it if you want to build it yourself:<\/p>\r\n[python]\r\nimport wx\r\nfrom win32gui import GetWindowText, GetForegroundWindow\r\nfrom time import sleep, strftime, localtime\r\nfrom ctypes import windll, Structure, c_uint, sizeof, byref\r\n\r\nclass TaskBarApp(wx.Frame):\r\n def __init__(self, parent, id, title):\r\n wx.Frame.__init__(self, parent, -1, title, size = (1, 1), style=wx.FRAME_NO_TASKBAR|wx.NO_FULL_REPAINT_ON_RESIZE)\r\n self.ICON_STATE = 1\r\n self.ID_ICON_TIMER=wx.NewId()\r\n self.tbicon = wx.TaskBarIcon()\r\n icon = wx.Icon(‘logon.ico’, wx.BITMAP_TYPE_ICO)\r\n self.tbicon.SetIcon(icon, ‘Logging’)\r\n self.tbicon.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarLeftDClick)\r\n self.tbicon.Bind(wx.EVT_TASKBAR_RIGHT_UP, self.OnTaskBarRightClick)\r\n self.Bind(wx.EVT_TIMER, self.Log, id=self.ID_ICON_TIMER)\r\n self.SetIconTimer()\r\n self.Show(True)\r\n self.lastInputInfo = self.LASTINPUTINFO()\r\n self.lastInputInfo.cbSize = sizeof(self.lastInputInfo)\r\n self.Write(self.Now()+”<|>__LOGGERSTART__<|>0\\n”)\r\n\r\n def OnTaskBarLeftDClick(self, evt):\r\n if self.ICON_STATE == 0:\r\n self.Write(self.Now()+”<|>__LOGGERUNPAUSE__<|>0\\n”)\r\n self.StartIconTimer()\r\n icon = wx.Icon(‘logon.ico’, wx.BITMAP_TYPE_ICO)\r\n self.tbicon.SetIcon(icon, ‘Logging’)\r\n self.ICON_STATE = 1\r\n else:\r\n self.StopIconTimer()\r\n self.Write(self.Now()+”<|>__LOGGERPAUSE__<|>0\\n”)\r\n icon = wx.Icon(‘logoff.ico’, wx.BITMAP_TYPE_ICO)\r\n self.tbicon.SetIcon(icon, ‘Not Logging’)\r\n self.ICON_STATE = 0\r\n\r\n def OnTaskBarRightClick(self, evt):\r\n self.StopIconTimer()\r\n self.Write(self.Now()+”<|>__LOGGERSTOP__<|>0\\n”)\r\n self.tbicon.Destroy()\r\n self.Close(True)\r\n wx.GetApp().ProcessIdle()\r\n \r\n def SetIconTimer(self):\r\n self.icontimer = wx.Timer(self, self.ID_ICON_TIMER)\r\n self.icontimer.Start(10000)\r\n\r\n def StartIconTimer(self):\r\n try:\r\n self.icontimer.Start(10000)\r\n except:\r\n pass\r\n\r\n def StopIconTimer(self):\r\n try:\r\n self.icontimer.Stop()\r\n except:\r\n pass\r\n\r\n def Log(self, evt):\r\n windll.user32.GetLastInputInfo(byref(self.lastInputInfo))\r\n idleDelta = float(windll.kernel32.GetTickCount() – self.lastInputInfo.dwTime) \/ 1000\r\n self.Write(self.Now()+”<|>“+GetWindowText(GetForegroundWindow())+”<|>“+str(idleDelta)+”\\n”)\r\n\r\n def Write(self, text):\r\n f=open(‘log.tmp’, ‘a’)\r\n f.write (text)\r\n f.close()\r\n def Now(self):\r\n return strftime(“%Y-%m-%d %H:%M:%S”, localtime())\r\n\r\n class LASTINPUTINFO(Structure):\r\n _fields_ = [(“cbSize”, c_uint),(“dwTime”, c_uint)]\r\n\r\nclass MyApp(wx.App):\r\n def OnInit(self):\r\n frame = TaskBarApp(None, -1, ‘ ‘)\r\n frame.Center(wx.BOTH)\r\n frame.Show(False)\r\n return True\r\n\r\ndef main():\r\n app = MyApp(0)\r\n app.MainLoop()\r\n\r\nif __name__ == ‘__main__’:\r\n main()\r\n[\/python]\r\n\r\n

It’s not too pretty but it seems to work. If you try it, it should begin putting an entry in log.tmp<\/code> every ten seconds of ‘datetime<|>program title<|>idle time’. Unfortunately, it takes 16 megs of RAM. I guess there’s a lot going on in the background in the imported stuff. I think it would be much more efficient if programmed from scratch but I’m no programmer. Also, I would make an .exe for people without Python but the .dll files that would have to go along with it would take up more than 10 megs. Must be all the stuff going on in the background again. So it’s inefficient but it would get the job done if you want something quick for a day or two or maybe it could form the base of a more optimized program. Next time I have a little free time, I might write a program to read the log and make a summary.<\/p>\r\n

Oh you’ll also need 2 32×32 .ico files called logon.ico and logoff.ico. You can use these or make your own icons<\/a>.<\/p>\r\n\"A<\/a> \"A<\/a>","protected":false},"excerpt":{"rendered":"I was curious how I spend my time and wanted something simple to log which window was active on my computer. I couldn’t see anything that did this easily on the internet, although there is a (more sophisticated\/complicated) browsing history tracker, and I needed to relax for an hour or two so I thought I’d […]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,13],"tags":[86,91,87,88,82,710,90,92,89],"_links":{"self":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/36"}],"collection":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":1,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":495,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/posts\/36\/revisions\/495"}],"wp:attachment":[{"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/scott.sherrillmix.com\/blog\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}