13

Just as the question asks, I know this is possible on Linux, but I couldn't find anything recent for Windows. Is it even possible?

Community
  • 1
  • 1
EnronEvolved
  • 159
  • 1
  • 8
  • If you're just wanting custom lock-on lock-off keys, you can always make a boolean in a class in such as a Tkinter program. Then you can have a certain key press change its state, and have things behave differently in different states. This effectively circumvents the need to worry about whether all the locks are officially on or off. Of course, this might sound overly simplistic and like a lot of work (without affecting the lights for the locks), but it's cross-platform, at least. The reason I didn't post this as an answer is because it's about creating your own state (not checking a state). – Brōtsyorfuzthrāx Apr 29 '14 at 20:16
  • But the problem with this approach is when the 'Caps' is already pressed i.e 'on' at the time of running of the program – Vivek Dec 21 '18 at 07:10

2 Answers2

15

You can use ctypes to load user32.dll and then call GetKeyState with nVirtKey = VK_CAPITAL (0x14)

def get_capslock_state():
    import ctypes
    hllDll = ctypes.WinDLL ("User32.dll")
    VK_CAPITAL = 0x14
    return hllDll.GetKeyState(VK_CAPITAL)
Abhijit
  • 62,056
  • 18
  • 131
  • 204
9

Install pywin32 for Python 3.x

Here is the example for checking capslock state.

from win32api import GetKeyState 
from win32con import VK_CAPITAL 
GetKeyState(VK_CAPITAL)
Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91