Skip to content

Commit

Permalink
Allow non-root users of appropriate groups to use module in Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
boppreh committed Sep 29, 2021
1 parent 830b4bb commit 7f03a3d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
7 changes: 1 addition & 6 deletions keyboard/_nixcommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def input_file(self):
self._input_file = open(self.path, 'rb')
except IOError as e:
if e.strerror == 'Permission denied':
print('Permission denied ({}). You must be sudo to access global events.'.format(self.path))
print('Permission denied ({}). You must be in the "input" to access global events. Use "sudo usermod -a -G input USER" to add user to the required group.'.format(self.path))
exit()

def try_close():
Expand Down Expand Up @@ -167,8 +167,3 @@ def aggregate_devices(type_name):
# If no keyboards were found we can only use the fake device to send keys.
assert fake_device
return fake_device


def ensure_root():
if os.geteuid() != 0:
raise ImportError('You must be root to use this library on linux.')
16 changes: 11 additions & 5 deletions keyboard/_nixkeyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections import namedtuple
from ._keyboard_event import KeyboardEvent, KEY_DOWN, KEY_UP
from ._canonical_names import all_modifiers, normalize_name
from ._nixcommon import EV_KEY, aggregate_devices, ensure_root
from ._nixcommon import EV_KEY, aggregate_devices

# TODO: start by reading current keyboard state, as to not missing any already pressed keys.
# See: http://stackoverflow.com/questions/3649874/how-to-get-keyboard-state-in-linux
Expand Down Expand Up @@ -45,7 +45,7 @@ def cleanup_modifier(modifier):
then parse the output and built a table. For each scan code and modifiers we
have a list of names and vice-versa.
"""
from subprocess import check_output
from subprocess import check_output, CalledProcessError, PIPE
from collections import defaultdict
import re

Expand All @@ -61,7 +61,6 @@ def register_key(key_and_modifiers, name):

def build_tables():
if to_name and from_name: return
ensure_root()

modifiers_bits = {
'shift': 1,
Expand All @@ -70,7 +69,15 @@ def build_tables():
'alt': 8,
}
keycode_template = r'^keycode\s+(\d+)\s+=(.*?)$'
dump = check_output(['dumpkeys', '--keys-only'], universal_newlines=True)
try:
dump = check_output(['dumpkeys', '--keys-only'], universal_newlines=True)
except CalledProcessError as e:
if e.returncode == 1:
raise ValueError('Failed to run dumpkeys to get key names. Check if your user is part of the "tty" group, and if not, add it with "sudo useradd -a -G tty USER".')
else:
raise


for str_scan_code, str_names in re.findall(keycode_template, dump, re.MULTILINE):
scan_code = int(str_scan_code)
for i, str_name in enumerate(str_names.strip().split()):
Expand Down Expand Up @@ -106,7 +113,6 @@ def build_tables():
def build_device():
global device
if device: return
ensure_root()
device = aggregate_devices('kbd')

def init():
Expand Down
3 changes: 1 addition & 2 deletions keyboard/_nixmouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import struct
from subprocess import check_output
import re
from ._nixcommon import EV_KEY, EV_REL, EV_MSC, EV_SYN, EV_ABS, aggregate_devices, ensure_root
from ._nixcommon import EV_KEY, EV_REL, EV_MSC, EV_SYN, EV_ABS, aggregate_devices
from ._mouse_event import ButtonEvent, WheelEvent, MoveEvent, LEFT, RIGHT, MIDDLE, X, X2, UP, DOWN

import ctypes
Expand Down Expand Up @@ -68,7 +68,6 @@ def move_to(x, y):
def build_device():
global device
if device: return
ensure_root()
device = aggregate_devices('mouse')
init = build_device

Expand Down

0 comments on commit 7f03a3d

Please sign in to comment.