<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random

import pyHook
import win32api
import win32con
import win32event
import win32gui
import win32gui_struct
import win32ui


class KeyCounter(object):

    def __init__(self):
        self.count = 0
        self.HWND = None
        self.hook = pyHook.HookManager()
        self.FPS = 60
        self.MSPF = int(round(1000.0 / self.FPS))
        self.font = None
        self.tooltip = 'Key Counter'
        # Private message to be used in THIS program ONLY
        self.MESSAGE = random.randint(win32con.WM_USER, 0x7FFF)
        self.__MESSAGE_TC = win32gui.RegisterWindowMessage('TaskbarCreated')
        self.__NOTIFY_ID = None
        self.MENU = None
        self.MENU_ITEMS = (
            ('Quit', self.stop),
            ('Reset', self.reset_count),
        )

    def reset_count(self):
        self.count = 0
        win32gui.RedrawWindow(self.HWND, None, None, win32con.RDW_INVALIDATE)

    def hook_keyboard(self):
        def Key_handler(evt):
            self.count += 1
            if self.HWND is not None:
                win32gui.RedrawWindow(
                    self.HWND, None, None, win32con.RDW_INVALIDATE
                )

        self.hook.KeyDown = Key_handler
        self.hook.HookKeyboard()

    def hook_mouse(self):
        pass

    def init_font(self, hdc, paintStruct):
        # http://msdn.microsoft.com/en-us/library/windows/desktop/dd145037(v=vs.85).aspx
        lf = win32gui.LOGFONT()
        lf.lfFaceName = "InputMono"
        fontSize = 36
        dpiScale = win32ui.GetDeviceCaps(
            hdc, win32con.LOGPIXELSX
        ) / 60.0
        lf.lfHeight = int(round(dpiScale * fontSize))
        # lf.lfWeight = 150
        # Use nonantialiased to remove the white edges around the text.
        # lf.lfQuality = win32con.NONANTIALIASED_QUALITY
        lf.lfQuality = win32con.NONANTIALIASED_QUALITY
        self.font = win32gui.CreateFontIndirect(lf)

    def create_menu(self):
        '''Create context menu'''
        if self.MENU is not None:
            return
        self.MENU = win32gui.CreatePopupMenu()
        # Add menu items
        for index, item in enumerate(self.MENU_ITEMS):
            menu_item, __ = win32gui_struct.PackMENUITEMINFO(
                text=item[0], wID=index
            )
            # https://msdn.microsoft.com/en-us/library/windows/desktop/ms647988(v=vs.85).aspx
            win32gui.InsertMenuItem(self.MENU, 0, 1, menu_item)

    def show_menu(self):
        if self.MENU is None:
            self.create_menu()
        position = win32gui.GetCursorPos()
        win32gui.SetForegroundWindow(self.HWND)
        win32gui.TrackPopupMenu(
            self.MENU,
            win32con.TPM_LEFTALIGN,
            position[0],
            position[1],
            0,
            self.HWND,
            None
        )
        win32gui.PostMessage(self.HWND, win32con.WM_NULL, None, None)

    def execute_menu_item(self, index):
        '''Execute menu item function'''
        func = self.MENU_ITEMS[index][-1]
        if callable(func):
            func()

    def create_window(self):
        def wndProc(hWnd, message, wParam, lParam):
            if message == self.MESSAGE:
                if lParam == win32con.WM_RBUTTONUP:
                    self.show_menu()
                return 0
            if message == win32con.WM_COMMAND:
                self.execute_menu_item(win32gui.LOWORD(wParam))
                return 0
            if message == self.__MESSAGE_TC:
                self.update_tray_icon()
                return 0
            if message == win32con.WM_PAINT:
                hdc, paintStruct = win32gui.BeginPaint(self.HWND)
                if self.font is None:
                    self.init_font(hdc, paintStruct)
                # Set the font
                win32gui.SelectObject(hdc, self.font)

                # rect = win32gui.GetClientRect(hWnd)
                rect = list(win32gui.GetClientRect(hWnd))
                # left, top, right, bottom
                rect[3] -= 40
                rect = tuple(rect)
                # http://msdn.microsoft.com/en-us/library/windows/desktop/dd162498(v=vs.85).aspx
                win32gui.DrawText(
                    hdc,
                    str(self.count),
                    len(str(self.count)),  # somehow -1 does not work
                    rect,
                    (win32con.DT_BOTTOM | win32con.DT_NOCLIP
                     | win32con.DT_SINGLELINE | win32con.DT_RIGHT)
                )
                win32gui.EndPaint(hWnd, paintStruct)
                return 0

            elif message == win32con.WM_DESTROY:
                print 'Window destroyed'
                win32gui.PostQuitMessage(0)
                return 0
            elif message == win32con.WM_CLOSE:
                print 'Closing the window.'
                return 0

            else:
                return win32gui.DefWindowProc(hWnd, message, wParam, lParam)

        hInstance = win32api.GetModuleHandle()
        className = 'TransparentWindow'

        # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633576(v=vs.85).aspx
        # win32gui does not support WNDCLASSEX.
        wndClass = win32gui.WNDCLASS()
        # http://msdn.microsoft.com/en-us/library/windows/desktop/ff729176(v=vs.85).aspx
        wndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW
        wndClass.lpfnWndProc = wndProc
        wndClass.hInstance = hInstance
        wndClass.hCursor = win32gui.LoadCursor(None, win32con.IDC_ARROW)
        wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH)
        wndClass.lpszClassName = className
        # win32gui does not support RegisterClassEx
        wndClassAtom = win32gui.RegisterClass(wndClass)

        # http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx
        # Consider using:
        #     WS_EX_COMPOSITED, WS_EX_LAYERED, WS_EX_NOACTIVATE,
        #     WS_EX_TOOLWINDOW, WS_EX_TOPMOST, WS_EX_TRANSPARENT
        # The WS_EX_TRANSPARENT flag makes events (like mouse clicks)
        # fall through the window.
        exStyle = (
            win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED
            | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST
            | win32con.WS_EX_TRANSPARENT
        )

        # http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600(v=vs.85).aspx
        # Consider using: WS_DISABLED, WS_POPUP, WS_VISIBLE
        style = win32con.WS_DISABLED | win32con.WS_POPUP | win32con.WS_VISIBLE

        # http://msdn.microsoft.com/en-us/library/windows/desktop/ms632680(v=vs.85).aspx
        hWindow = win32gui.CreateWindowEx(
            exStyle,
            wndClassAtom,
            None,  # WindowName
            style,
            0,  # x
            0,  # y
            win32api.GetSystemMetrics(win32con.SM_CXSCREEN),  # width<commit_msg>Remove taskbar icon (created as toolbar window)<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random

import pyHook
import win32api
import win32con
import win32event
import win32gui
import win32gui_struct
import win32ui


class KeyCounter(object):

    def __init__(self):
        self.count = 0
        self.HWND = None
        self.hook = pyHook.HookManager()
        self.FPS = 60
        self.MSPF = int(round(1000.0 / self.FPS))
        self.font = None
        self.tooltip = 'Key Counter'
        # Private message to be used in THIS program ONLY
        self.MESSAGE = random.randint(win32con.WM_USER, 0x7FFF)
        self.__MESSAGE_TC = win32gui.RegisterWindowMessage('TaskbarCreated')
        self.__NOTIFY_ID = None
        self.MENU = None
        self.MENU_ITEMS = (
            ('Quit', self.stop),
            ('Reset', self.reset_count),
        )

    def reset_count(self):
        self.count = 0
        win32gui.RedrawWindow(self.HWND, None, None, win32con.RDW_INVALIDATE)

    def hook_keyboard(self):
        def Key_handler(evt):
            self.count += 1
            if self.HWND is not None:
                win32gui.RedrawWindow(
                    self.HWND, None, None, win32con.RDW_INVALIDATE
                )

        self.hook.KeyDown = Key_handler
        self.hook.HookKeyboard()

    def hook_mouse(self):
        pass

    def init_font(self, hdc, paintStruct):
        # http://msdn.microsoft.com/en-us/library/windows/desktop/dd145037(v=vs.85).aspx
        lf = win32gui.LOGFONT()
        lf.lfFaceName = "InputMono"
        fontSize = 36
        dpiScale = win32ui.GetDeviceCaps(
            hdc, win32con.LOGPIXELSX
        ) / 60.0
        lf.lfHeight = int(round(dpiScale * fontSize))
        # lf.lfWeight = 150
        # Use nonantialiased to remove the white edges around the text.
        # lf.lfQuality = win32con.NONANTIALIASED_QUALITY
        lf.lfQuality = win32con.NONANTIALIASED_QUALITY
        self.font = win32gui.CreateFontIndirect(lf)

    def create_menu(self):
        '''Create context menu'''
        if self.MENU is not None:
            return
        self.MENU = win32gui.CreatePopupMenu()
        # Add menu items
        for index, item in enumerate(self.MENU_ITEMS):
            menu_item, __ = win32gui_struct.PackMENUITEMINFO(
                text=item[0], wID=index
            )
            # https://msdn.microsoft.com/en-us/library/windows/desktop/ms647988(v=vs.85).aspx
            win32gui.InsertMenuItem(self.MENU, 0, 1, menu_item)

    def show_menu(self):
        if self.MENU is None:
            self.create_menu()
        position = win32gui.GetCursorPos()
        win32gui.SetForegroundWindow(self.HWND)
        win32gui.TrackPopupMenu(
            self.MENU,
            win32con.TPM_LEFTALIGN,
            position[0],
            position[1],
            0,
            self.HWND,
            None
        )
        win32gui.PostMessage(self.HWND, win32con.WM_NULL, None, None)

    def execute_menu_item(self, index):
        '''Execute menu item function'''
        func = self.MENU_ITEMS[index][-1]
        if callable(func):
            func()

    def create_window(self):
        def wndProc(hWnd, message, wParam, lParam):
            if message == self.MESSAGE:
                if lParam == win32con.WM_RBUTTONUP:
                    self.show_menu()
                return 0
            if message == win32con.WM_COMMAND:
                self.execute_menu_item(win32gui.LOWORD(wParam))
                return 0
            if message == self.__MESSAGE_TC:
                self.update_tray_icon()
                return 0
            if message == win32con.WM_PAINT:
                hdc, paintStruct = win32gui.BeginPaint(self.HWND)
                if self.font is None:
                    self.init_font(hdc, paintStruct)
                # Set the font
                win32gui.SelectObject(hdc, self.font)

                # rect = win32gui.GetClientRect(hWnd)
                rect = list(win32gui.GetClientRect(hWnd))
                # left, top, right, bottom
                rect[3] -= 40
                rect = tuple(rect)
                # http://msdn.microsoft.com/en-us/library/windows/desktop/dd162498(v=vs.85).aspx
                win32gui.DrawText(
                    hdc,
                    str(self.count),
                    len(str(self.count)),  # somehow -1 does not work
                    rect,
                    (win32con.DT_BOTTOM | win32con.DT_NOCLIP
                     | win32con.DT_SINGLELINE | win32con.DT_RIGHT)
                )
                win32gui.EndPaint(hWnd, paintStruct)
                return 0

            elif message == win32con.WM_DESTROY:
                print 'Window destroyed'
                win32gui.PostQuitMessage(0)
                return 0
            elif message == win32con.WM_CLOSE:
                print 'Closing the window.'
                return 0

            else:
                return win32gui.DefWindowProc(hWnd, message, wParam, lParam)

        hInstance = win32api.GetModuleHandle()
        className = 'TransparentWindow'

        # http://msdn.microsoft.com/en-us/library/windows/desktop/ms633576(v=vs.85).aspx
        # win32gui does not support WNDCLASSEX.
        wndClass = win32gui.WNDCLASS()
        # http://msdn.microsoft.com/en-us/library/windows/desktop/ff729176(v=vs.85).aspx
        wndClass.style = win32con.CS_HREDRAW | win32con.CS_VREDRAW
        wndClass.lpfnWndProc = wndProc
        wndClass.hInstance = hInstance
        wndClass.hCursor = win32gui.LoadCursor(None, win32con.IDC_ARROW)
        wndClass.hbrBackground = win32gui.GetStockObject(win32con.WHITE_BRUSH)
        wndClass.lpszClassName = className
        # win32gui does not support RegisterClassEx
        wndClassAtom = win32gui.RegisterClass(wndClass)

        # http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx
        # Consider using:
        #     WS_EX_COMPOSITED, WS_EX_LAYERED, WS_EX_NOACTIVATE,
        #     WS_EX_TOOLWINDOW, WS_EX_TOPMOST, WS_EX_TRANSPARENT
        # The WS_EX_TRANSPARENT flag makes events (like mouse clicks)
        # fall through the window.
        exStyle = (
            win32con.WS_EX_COMPOSITED | win32con.WS_EX_LAYERED
            | win32con.WS_EX_NOACTIVATE | win32con.WS_EX_TOPMOST
            | win32con.WS_EX_TRANSPARENT | win32con.WS_EX_TOOLWINDOW
        )

        # http://msdn.microsoft.com/en-us/library/windows/desktop/ms632600(v=vs.85).aspx
        # Consider using: WS_DISABLED, WS_POPUP, WS_VISIBLE
        style = win32con.WS_DISABLED | win32con.WS_POPUP | win32con.WS_VISIBLE

        # http://msdn.microsoft.com/en-us/library/windows/desktop/ms632680(v=vs.85).aspx
        hWindow = win32gui.CreateWindowEx(
            exStyle,
            wndClassAtom,
            None,  # WindowName
            style,
            0,  # x
            0,  # y
            win32api.GetSystemMetrics(win32con.SM_CXSCREEN),  # width
{'fraction_non_alphanumeric': 0.09253129946754929, 'fraction_numerical': 0.03453734350266225, 'mean_word_length': 1.8204139610389611, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4244187', 'n_tokens_mistral': 4839, 'n_tokens_neox': 4336, 'n_words': 764}