Beispiel #1
0
    def run(self):
        events = XEvents()
        events.start()
        while not events.listening():
            # Wait for init
            time.sleep(1)

        try:
            while events.listening():
                evt = events.next_event()
                if not evt:
                    time.sleep(0.5)
                    continue

                if evt.type != "EV_KEY" or evt.value != 1:  # Only count key down, not up.
                    continue

                self.thishour_count += 1

                if time.time() > self.nextsave:
                    self.save()

                    if datetime.now().hour != self.thishour.hour:
                        self.set_thishour()

        except KeyboardInterrupt:
            events.stop_listening()
            self.save()
Beispiel #2
0
    def run(self):
        events = XEvents()
        events.start()
        while not events.listening():
            # Wait for init
            time.sleep(1)

        try:
            while events.listening():
                evt = events.next_event()
                if not evt:
                    time.sleep(0.5)
                    continue

                if evt.type != "EV_MOV" and  # Only track key movement or
                    (evt.type != "EV_KEY" or evt.value != 1): # Only count key down, not up.
                    continue

                if evt.type == "EV_MOV":
                    self.thishour_distance += distance(evt.value, self.lastmove)
                    self.lastmove = evt.value
                
                if evt.type == "EV_KEY":
                    self.thishour_count+=1
            
                if time.time() > self.nextsave:
                    self.save()
            
                    if datetime.now().hour != self.thishour.hour:
                        self.set_thishour()
            
        except KeyboardInterrupt:
            events.stop_listening()
            self.save()
Beispiel #3
0
    def run(self):
        events = XEvents()
        events.start()
        while not events.listening():
            # Wait for init
            time.sleep(1)

        try:
            while events.listening():
                evt = events.next_event()
                if not evt:
                    time.sleep(0.5)
                    continue

                if evt.type != "EV_KEY" or evt.value != 1:  # Only count key down, not up.
                    continue

                self.thishour_count += 1

                if time.time() > self.nextsave:
                    self.save()

        except KeyboardInterrupt:
            events.stop_listening()
            self.save()

        except:
            print "Caught exception, probably not connected to the interwebs..."
Beispiel #4
0
    def run(self):
        events = XEvents()
        events.start()
        while not events.listening():
            # Wait for init
            time.sleep(1)

        try:
            while events.listening():
                evt = events.next_event()
                if not evt:
                    time.sleep(0.5)
                    continue
                
                if evt.type != "EV_KEY" or evt.value != 1: # Only count key down, not up.
                    continue

                self.thishour_count+=1
            
                if time.time() > self.nextsave:
                    self.save()
            
        except KeyboardInterrupt:
            events.stop_listening()
            self.save()
            
        except:
            print "Caught exception, probably not connected to the interwebs..."
Beispiel #5
0
    def run(self):
        events = XEvents()
        events.start()

        lastkeyat = time.time()
        
        while not events.listening():
            # Wait for init
            time.sleep(1)

        try:
            while events.listening():
                evt = events.next_event()

                if time.time() - lastkeyat > 0.75 and self.muted:
                    self.togglemute()
                    self.muted = False

                if not evt:
                    time.sleep(0.5)
                    continue
                
                if evt.type != "EV_KEY" or evt.value != 1: # Only count key down, not up.
                    continue

                if not self.muted and not "BTN_" in evt.code:
                    self.togglemute()
                    self.muted = True
                    
                lastkeyat = time.time()
                    
                self.thishour_count+=1
            
                if time.time() > self.nextsave:
                    self.save()
            
                    if datetime.now().hour != self.thishour.hour:
                        self.set_thishour()
            
        except KeyboardInterrupt:
            events.stop_listening()
            self.save()
Beispiel #6
0
class KbdCounter(object):
    def __init__(self, options):
        self.storepath=os.path.expanduser(options.storepath)
        self.conn = sqlite3.connect(self.storepath)
        self.dbcursor = self.conn.cursor()
        self.initialise_database()

        self.records = []
        self.lastsave = datetime.now()

        Gtk.init([])  # necessary if not using a Gtk.main() loop
        self.screen = Wnck.Screen.get_default()
        self.screen.force_update()  # recommended per Wnck documentation

        self.cur_win = 'Unknown window'
        self.set_current_window()

    def initialise_database(self):
        self.dbcursor.execute('create table if not exists record \
                               (time text, app_name text, code text, scancode text, value text)')

    def save(self):
        # self.set_nextsave()
        for record in self.records:
            self.dbcursor.execute('insert into record \
                                  (time,app_name,code,scancode,value) values \
                                  (?,?,?,?,?)', \
                                  (record.time, record.app_name, record.code, record.scancode, record.value))
        self.records = []
        self.lastsave = datetime.now()
        self.conn.commit()

    def set_current_window(self):
        while Gtk.events_pending():
            # Without this, get_active_window() returns outdated information
            Gtk.main_iteration()
        window = self.screen.get_active_window()
        if window:
            # when switching windows, get_active_window() sometimes returns None
            self.cur_win = window.get_class_group_name()

    def event_handler(self):
        evt = self.events.next_event()
        while(evt):
            if evt.type != "EV_KEY": # Only count key down, not up.
                evt = self.events.next_event()
                continue

            self.set_current_window()

            record = Record()
            record.time = datetime.now().strftime(timeformat)
            record.app_name = self.cur_win
            record.code = evt.get_code()
            record.scancode = evt.get_scancode()
            record.value = evt.get_value()
            self.records.append(record)

            if (datetime.now() - self.lastsave).total_seconds() > 60:
                self.save()

            evt = self.events.next_event()

    def run(self):
        self.events = XEvents()
        event_ready = threading.Event()
        self.events.set_event(event_ready)
        self.events.start()
        while not self.events.listening():
            # Wait for init
            time.sleep(1)
        try:
            while True:
                self.event_handler()
                event_ready.clear()
                event_ready.wait()
        except KeyboardInterrupt:
            self.events.stop_listening()
            self.save()
Beispiel #7
0
    def run(self):
        events = XEvents()
        events.start()
        while not events.listening():
            # Wait for init
            time.sleep(1)

        last_mov = None

        try:
            modifier_state = 0

            while events.listening():
                evt = events.next_event()
                if not evt:
                    time.sleep(0.5)
                    continue

                # read modifier state
                if evt.type == 'EV_KEY' and evt.code in MODIFIERS.iterkeys():
                    if evt.value:
                        modifier_state |= MODIFIERS[evt.code]
                    else:
                        modifier_state &= ~MODIFIERS[evt.code]

                # Key press (evt.value == 1) or release (evt.value == 0)
                if evt.type == 'EV_KEY' and evt.value == 1:
                    if evt.code.startswith('KEY'):
                        if evt.code == 'KEY_DUNNO':
                            idx = (evt.scancode, modifier_state)
                        else:
                            idx = (evt.code, modifier_state)
                        self.keyboard_events[idx] += 1

                    if evt.code.startswith('BTN'):
                        self.mouse_events[(evt.code, modifier_state)] += 1

                if evt.type == 'EV_MOV':
                    # EV_MOV's value is a tuple with the current mouse coordinates.
                    # To track movement, we need to compare with the last position
                    x, y = evt.value
                    if last_mov:
                        self.mouse_distance_x += abs(x - last_mov[0])
                        self.mouse_distance_y += abs(y - last_mov[1])

                    last_mov = x, y

                # Scrolling
                if evt.type == 'EV_REL':
                    if evt.code == 'REL_WHEEL':
                        if evt.value > 0:
                            self.mouse_events[('WHEEL_UP', modifier_state)] += evt.value
                        if evt.value < 0:
                            self.mouse_events[('WHEEL_DOWN', modifier_state)] += -evt.value

                if evt.code == 'REL_WHEEL' or (evt.type == 'EV_KEY' and evt.value == 1 and evt.code not in MODIFIERS.iterkeys()):
                    print("type %s value %s code %s scancode %s" % (evt.type, evt.value, evt.code, evt.scancode), end=' ')
                    print("S:%d C:%d A:%d M:%d S:%d" % (modifier_state & MODIFIERS['KEY_SHIFT_L'],
                                                            modifier_state & MODIFIERS['KEY_CONTROL_L'],
                                                            modifier_state & MODIFIERS['KEY_ALT_L'],
                                                            modifier_state & MODIFIERS['KEY_META_L'],
                                                            modifier_state & MODIFIERS['KEY_SUPER_L']))


                if time.time() > self.nextsave:
                    print("Mouse:", self.mouse_distance_x, self.mouse_distance_y)
                    self.save()
            
                    if datetime.now().hour != self.thishour.hour:
                        self.set_thishour()
            
        except KeyboardInterrupt:
            events.stop_listening()
            self.save()