Search code examples
pythonpython-3.xwinapipywin32capslock

How to make a CAPS LOCK indicator using PYTHON on Windows


i wanted to make a caps-lock and num-lock indicator using python on windows, but i dont know how to start with and what all modules and library i need to implement my desired output.

THE OUTPUT i desire is that whenever i press CAPS-LOCK the borders of screen should go green and for *NUM-LOCK** blue.

also, does it require a GUI?

(but i want it to be transparent. i don't want minimise,close and maximise buttons on gui) and this all process should be done in background.

Please guide me on how should i approach this. THANKS A LOT.


Solution

  • There's the WinApi package for python here.

    Not very well documented, but after quick look at demos looks like this code works:

    import win32api
    import win32con
    print(win32api.GetKeyState(win32con.VK_CAPITAL))
    

    For green frame you'll need probably some GUI library (GTK+, Qt), but I don't know which one could achieve such effect.

    Edit: I figured you can actually use the same WinApi to draw on the screen.

    import win32api
    import win32gui
    import win32con
    
    pen = win32gui.CreatePen(win32con.PS_SOLID, 7, win32api.RGB(255, 0, 0))
    dc = win32gui.GetDC(0)
    win32gui.SelectObject(dc, pen)
    win32gui.MoveToEx(dc, 0, 0)
    win32gui.LineTo(dc, 1920, 0)