Skip to content

Commit

Permalink
Add support for OS X and python3
Browse files Browse the repository at this point in the history
Still somewhat experimental
  • Loading branch information
omad committed Sep 1, 2015
1 parent 3c2fbcb commit 821653c
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions gui.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@


import sys
from PyQt4 import QtGui, QtCore
import urllib,urllib2
import pyHook
try:
import urllib.request as urllib2
except ImportError:
import urllib2
import urllib
from datetime import datetime
from sys import platform as _platform

if _platform == "darwin":
from Foundation import NSObject, NSLog
from Cocoa import NSEvent, NSKeyDownMask, NSKeyDown
import string
elif _platform == "win32":
import pyHook

class KeyCounter(QtCore.QObject):
'''
Expand All @@ -15,17 +24,30 @@ class KeyCounter(QtCore.QObject):
def __init__(self):
super(KeyCounter, self).__init__()
self.keyCount = 0
self.setupKeyCounter()
if _platform == "win32":
self.setupKeyCounterWin()
elif _platform == "darwin":
self.setupKeyCounterMac()

def setupKeyCounterMac(self):
mask = NSKeyDownMask
NSEvent.addGlobalMonitorForEventsMatchingMask_handler_(mask, self.macCountKey)
NSEvent.addLocalMonitorForEventsMatchingMask_handler_(mask, self.macCountKey)

def macCountKey(self, event):
eventCharAscii = ord(event._.characters)
if eventCharAscii > 32 and eventCharAscii < 127:
self.keyCount += 1

def countKey(self, event):
'''
Increment the keyCount variable if an ascii key was pressed
'''
if event.Ascii > 32 and event.Ascii < 127:
self.keyCount += 1
return True
return True # Don't block key handling

def setupKeyCounter(self):
def setupKeyCounterWin(self):
'''
Setup the hook for monitoring global key presses
'''
Expand Down Expand Up @@ -111,7 +133,7 @@ def postCountToGoogleForm(keyCount):
req = urllib2.Request(url,dataenc)
response = urllib2.urlopen(req)
except Exception as e:
print e
print(e)
return True

def main():
Expand Down

0 comments on commit 821653c

Please sign in to comment.