Uno script grafico per rilevare se caps lock è premuto

Autore: Fabio Di Matteo
Ultima revisione: 07/10/2019 - 12:07

Dipendenze python : wxPython, pynput Dipendenze extra: xset

#!/usr/bin/env python3
import wx
import wx.adv
import os
 
import threading
import subprocess
from pynput import keyboard
 
#Dipendenze: wxPython, pynput
# xset (bash command)
 
 
 
VERSION="0.1"
TRAY_TOOLTIP = 'CapsLock Status'
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
 
TRAY_ICON_OFF = os.path.join(SCRIPT_DIR,'off.png')
TRAY_ICON_ON = os.path.join(SCRIPT_DIR,'on.png')
 
def create_menu_item(menu, label, func):
    item = wx.MenuItem(menu, -1, label)
    menu.Bind(wx.EVT_MENU, func, id=item.GetId())
    menu.Append(item)
    return item
 
 
class TaskBarIcon(wx.adv.TaskBarIcon):
    def __init__(self,frame):
        self.frame=frame
        super(TaskBarIcon, self).__init__()
        self.set_icon(TRAY_ICON_OFF)
        self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
        if self.getCapslockStatus():
            print("CapsLock ON")
            wx.CallAfter(self.set_icon, TRAY_ICON_ON)
        else:
            print("CapsLock OFF")
            wx.CallAfter(self.set_icon, TRAY_ICON_OFF)
        self.update()
 
    def update(self):
        cmdt = threading.Thread(target=self.updateCapsLockStatus)
        cmdt.daemon = True
        cmdt.start()
 
 
    def CreatePopupMenu(self):
        menu = wx.Menu()
        create_menu_item(menu, 'Info...', self.on_hello)
        menu.AppendSeparator()
        create_menu_item(menu, 'Exit', self.on_exit)
        return menu
 
    def set_icon(self, path):
        icon = wx.Icon(path)
        self.SetIcon(icon, TRAY_TOOLTIP)
 
    def on_left_down(self, event):
        print ('Tray icon was left-clicked.')
 
    def on_hello(self, event):
        self.onAbout(event)
 
    def on_exit(self, event):
        wx.CallAfter(self.frame.Close)
        wx.CallAfter(self.Destroy)
 
    # keylogger
    def getCapslockStatus(self):
    	if subprocess.check_output('xset q | grep LED', shell=True)[65] == 50 :
    		return False
    	if subprocess.check_output('xset q | grep LED', shell=True)[65] == 51 :
    		return True
 
    def on_press(self, key):
        pass
 
    def on_release(self, key):
        try:
            print('alphanumeric key {0} pressed'.format(key.char))
        except AttributeError:
            if (key== keyboard.Key.caps_lock):
                if self.getCapslockStatus():
                    print("CapsLock ON")
                    wx.CallAfter(self.set_icon, TRAY_ICON_ON)
                else:
                    print("CapsLock OFF")
                    wx.CallAfter(self.set_icon, TRAY_ICON_OFF)
 
 
    def updateCapsLockStatus(self):
        with keyboard.Listener(on_press=self.on_press, on_release=self.on_release) as listener:
            listener.join()
 
    def onAbout(self, evt):
        aboutInfo = wx.adv.AboutDialogInfo()
        aboutInfo.SetName("PobCapsDetect")
        aboutInfo.SetVersion(VERSION)
        aboutInfo.SetIcon(wx.Icon(TRAY_ICON_OFF, wx.BITMAP_TYPE_PNG))
        aboutInfo.SetDescription("A simple gui for detect CapsLock")
        aboutInfo.SetCopyright("Released under GNU/GPL v3 License \n\n Author: Fabio Di Matteo - fadimatteo@gmail.com")
        #aboutInfo.SetWebSite("https://github.com/pobfdm/pobshare")
        aboutInfo.AddDeveloper("Fabio Di Matteo - fadimatteo@gmail.com")
        #aboutInfo.AddArtist("http://www.iconarchive.com/show/rounded-social-media-icons-by-graphicloads/share-icon.html")
        wx.adv.AboutBox(aboutInfo)
 
def main():
    app = wx.App(False)
    window = wx.Frame(None, title = "Caps Lock", size = (300,200))
    TaskBarIcon(window)
    #window.Show()
    app.MainLoop()
 
 
if __name__ == '__main__':
    main()