Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,19 @@ def add_abbreviation(source_text, replacement_text, match_suffix=False, timeout=
callback = lambda: write(replacement)
return add_word_listener(source_text, callback, match_suffix=match_suffix, timeout=timeout)

def lock_state(name):
"""
Valid options for `name`:

- caps
- num
- scroll

Checks the state of the system's caps/num/scroll lock and returns True if the
lock is enabled or False otherwise.
"""
return _os_keyboard.lock_state(name)

# Aliases.
register_word_listener = add_word_listener
register_abbreviation = add_abbreviation
Expand Down
16 changes: 14 additions & 2 deletions keyboard/_darwinkeyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class KeyMap(object):
0x37: 'command',
0x38: 'shift',
0x39: 'capslock',
0x3a: 'option',
0x3a: 'left alt',
0x3b: 'control',
0x3c: 'right shift',
0x3d: 'right option',
0x3d: 'right alt',
0x3e: 'right control',
0x3f: 'function',
0x40: 'f17',
Expand Down Expand Up @@ -430,6 +430,18 @@ def listen(callback):
raise OSError("Error 13 - Must be run as administrator")
KeyEventListener(callback).run()

def lock_state(name):
""" Given a locking key (`caps`, `num`, `scroll`) this returns True if the lock is
engaged and False otherwise. Note that OSX does not support num or scroll lock. """
if name.lower() == "caps":
return bool(Carbon.GetCurrentKeyModifiers() & 0x0400) # bit for Caps Lock
elif name.lower() == "num":
return False # OSX does not support num lock
elif name.lower() == "scroll":
return False # OSX does not support scroll lock
else:
raise ValueError("Unrecognized lock type (valid options are 'caps', 'num', 'scroll'): " + name)

def type_unicode(character):
OUTPUT_SOURCE = Quartz.CGEventSourceCreate(Quartz.kCGEventSourceStateHIDSystemState)
# Key down
Expand Down
19 changes: 18 additions & 1 deletion keyboard/_nixkeyboard.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
import struct
import traceback
from time import time as now
from collections import namedtuple
from ._keyboard_event import KeyboardEvent, KEY_DOWN, KEY_UP
Expand Down Expand Up @@ -177,6 +176,24 @@ def type_unicode(character):
scan_code, _ = next(map_name(key))
release(scan_code)

def lock_state(name):
""" Given a locking key (`caps`, `num`, `scroll`) this returns True if the lock is
engaged and False otherwise. """

try:
flag = int(check_output("xset q | grep LED", shell=True).strip()[-1])
except:
raise # Specific errors to be implemented later

if name.lower() == "caps":
return bool(flag & 0x01) # bitflag for Caps Lock
elif name.lower() == "num":
return bool(flag & 0x02) # bitflag for Num Lock
elif name.lower() == "scroll":
return bool(flag & 0x04) # bitflag for Scroll Lock
else:
raise ValueError("Unrecognized lock type (valid options are 'caps', 'num', 'scroll'): " + name)

if __name__ == '__main__':
def p(e):
print(e)
Expand Down
12 changes: 12 additions & 0 deletions keyboard/_winkeyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,18 @@ def type_unicode(character):
cbSize = c_int(ctypes.sizeof(INPUT))
SendInput(nInputs, pInputs, cbSize)

def lock_state(name):
""" Given a locking key (`caps`, `num`, `scroll`) this returns True if the lock is
engaged and False otherwise. """
if name.lower() == "caps":
return bool(user32.GetKeyState(0x14)) # vkcode for Caps Lock
elif name.lower() == "num":
return bool(user32.GetKeyState(0x90)) # vkcode for Num Lock
elif name.lower() == "scroll":
return bool(user32.GetKeyState(0x91)) # vkcode for Scroll Lock
else:
raise ValueError("Unrecognized lock type (valid options are 'caps', 'num', 'scroll'): " + name)

if __name__ == '__main__':
_setup_name_tables()
import pprint
Expand Down